问题描述
我正在使用TextWatcher
,并且无法检测到TextWatcher.afterTextChange
事件中的键.我还想在textWatcher
事件中的某些情况下清除textView
.
I am using TextWatcher
and I am unable to detect key in TextWatcher.afterTextChange
event. I also want to clear textView
on some condition in textWatcher
event.
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// I want to detect backspace key here
}
推荐答案
KeyListener
可以满足您的两个条件.
A KeyListener
can fulfil both of your conditions.
mEditText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL){
//on backspace
}
return false
}
});
类似地,在onKey()
内,当您想清除textView
时,可以放置多个检查语句以检查条件.
Similarly inside the onKey()
, you can put multiple check statements to check for the condition, when you would want to clear the textView
.
由于 @RankoR 足以指出,请记住 onKeyListener()
仅适用于硬件键盘而不是软键盘.
EDIT : As @RankoR was kind enough to point out, please bear in mind that onKeyListener()
works only for the hardware keyboards and not the soft keyboards.
这篇关于在TextWatcher中检测退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!