Detect touch on EditText, but not interrupt it?
I need to know when the user touche开发者_如何学JAVAs/taps/clicks the edittext in my activity.
How can I do this without interrupting the events, so the keypad still displays properly?
(And I need to know about it before the OS displays the keypad...if possible)
txtEdit.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent motionEvent) {
// your code here....
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
});
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
...
}
You should be able to do this by attaching an OnClickListener to your EditText. If you're concerned about blocking the UI thread in your OnClickListener, you can spawn a new Thread and do your work in there - though, if you do that, there's no guarantee the work will be done before the keypad shows up.
Less verbosity
The same Mathias Conradt's approach, but using kotlin:
txtEdit.setOnTouchListener({ view, motionEvent ->
// your code here....
false
})
This line getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
is unnecessary since the keyboard shown when you tap the editText view.
OnTouchListener
was getting called twice for me (on Pixel 2 running Pie). So I used
OnFocusChangeListener
instead:
txtEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.d(TAG,"Focused Now!");
}
}
});
精彩评论