时间:2021-06-29 08:27:58 | 栏目:Android代码 | 点击:次
我们都知道, Android EditText输入框,并没有监听用户输入完成的功能,需要我们自己实现。 下面是实现的方法,仅供参考:
EditText editText = (EditText) findViewById(R.id.edit); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } //输入时的调用 @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { Log.d(TAG, "onTextChanged() returned: "); mHandler.removeCallbacks(mRunnable); //800毫秒没有输入认为输入完毕 mHandler.postDelayed(mRunnable, 800); } @Override public void afterTextChanged(Editable editable) { Log.d(TAG, "onTextChanged() returned: 2222"); } });
配合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:输入完成 " ); } } }; private Runnable mRunnable = new Runnable() { @Override public void run() { mHandler.sendEmptyMessage(EDIT_OK); } };