当我在文本框中输入一些文本时,OnKey事件会发出两次。如何预防? [英] OnKey event dispatched twice when I type some text into a textbox. How to prevent?
问题描述
EditText
控件,我将当前活动定义为 OnKeyListener
以在用户类型时执行验证。 代码
txtPhoneNumber.setOnEditorActionListener(this);
txtPhoneNumber.setOnKeyListener(this);
txtPhoneNumber.setOnFocusChangeListener(this);
@Override
public boolean onEditorAction(TextView v,int actionId,KeyEvent event){
String phoneNumber =((TextView)v).getText()。toString();
if(phoneNumber!= null&&!。equals(phoneNumber))
setValidPhoneNumber(checkValidPhoneNumber(phoneNumber));
setForwardButtonEnabled(this.validPhoneNumber&& this.readyToGo);
if(actionId == EditorInfo.IME_ACTION_DONE){
InputMethodManager in =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(v.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
@Override
public boolean onKey(View v,int keyCode,KeyEvent event){
String phoneNumber =((TextView)v) .getText()的toString();
if(phoneNumber!= null&&!。equals(phoneNumber))
setValidPhoneNumber(checkValidPhoneNumber(phoneNumber));
setForwardButtonEnabled(this.validPhoneNumber&& this.readyToGo);
返回false;
}
确定我可以承认,当用户再次执行验证时,这是相当多余的正在按Enter键,而不仅仅是关闭软键盘。但是我发现OnKey事件是两次发布的。
例如我正在写 3551234567
,我已经键入 355
。当我按1时,一个事件被触发,具有 v.getText()
= 355
,另一个事件有 v.getText()
= 3551
。
我想知道这是否正常,如果这可以通过区分这是preOnKeyEvent还是postOnKeyEvent可以避免。 b
您可能会收到钥匙扣/钥匙扣事件。
尝试过滤您的方法通过检查KeyEvent对ACTION_UP操作的操作。
阅读此获取更多信息
during my debug sessions I found a strange thing occurring. I have an EditText
control for which I defined the current activity as OnKeyListener
to perform validation while user types.
Code
txtPhoneNumber.setOnEditorActionListener(this);
txtPhoneNumber.setOnKeyListener(this);
txtPhoneNumber.setOnFocusChangeListener(this);
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
String phoneNumber = ((TextView) v).getText().toString();
if (phoneNumber != null && !"".equals(phoneNumber))
setValidPhoneNumber(checkValidPhoneNumber(phoneNumber));
setForwardButtonEnabled(this.validPhoneNumber && this.readyToGo);
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(v.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String phoneNumber = ((TextView) v).getText().toString();
if (phoneNumber != null && !"".equals(phoneNumber))
setValidPhoneNumber(checkValidPhoneNumber(phoneNumber));
setForwardButtonEnabled(this.validPhoneNumber && this.readyToGo);
return false;
}
OK I can admit that this is quite redundant to perform validation again when the user is pressing the Enter key, more than just closing the soft keyboard. However I found that the OnKey event is dispatched twice.
For example I'm writing 3551234567
and I already typed 355
. When I press 1, one event is fired having v.getText()
= 355
and next another event has v.getText()
= 3551
.
I would like to know if this is normal and if this can be avoided by either distinguishing if this is a "preOnKeyEvent" or "postOnKeyEvent". I only need the string after the event, not before.
Thank you
You are probably getting both the key down/key up events.
Try filtering your method by checking the action of the KeyEvent for an ACTION_UP action.
Read this for more information
这篇关于当我在文本框中输入一些文本时,OnKey事件会发出两次。如何预防?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!