Reputation: 240
So I have added these lines inside onCreate
such that when I click or touch the EditText it performs the action specified in doSomething
. The problem is that with the code as presented below, after executing doSomething
the keyboard doesn't show up. If I remove the return true
inside the if block, then it works, but I don't understand why it fails to work if this line is included. So why does this happen?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
doSomething(editText);
return true;
}
return false;
}
});
}
Upvotes: 1
Views: 401
Reputation: 19273
it happens because keyboard will show when MotionEvent.ACTION_UP
will be handled by EditText
itself (not your custom OnTouchListener
), so your onTouch
should return false
then
in general flag returned by onTouch
informs framework that you've handled event - when you returning true
then this event won't be propagated further (as already handled, "consumed"). if false
returned then event still will be propagated to this View
(and handled under the hood, check out particular sources how) or even to its parent if default implementation of View
doesn't handle this action and also return false
this isn't the case of EditText
- it is handling all events and like in first paragraph - ACTION_UP
will show keyboard. but returning true
in your first-in-order custom OnTouchListener
prevent that
Upvotes: 2