Android EditText listens for an instance of user input completion
- 2021-08-21 21:18:13
- OfStack
As we all know, the Android EditText input box does not have the function of listening to user input, so we need to implement it ourselves. The following is the implementation method for reference only:
EditText editText = (EditText) findViewById(R.id.edit);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
// Call on input
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d(TAG, "onTextChanged() returned: ");
mHandler.removeCallbacks(mRunnable);
//800 No input in milliseconds is considered as finished
mHandler.postDelayed(mRunnable, 800);
}
@Override
public void afterTextChanged(Editable editable) {
Log.d(TAG, "onTextChanged() returned: 2222");
}
});
For use with Handler:
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (EDIT_OK == msg.what) {
Log.d(TAG, "handleMessage() returned: Enter complete " );
}
}
};
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
mHandler.sendEmptyMessage(EDIT_OK);
}
};