java – Android setOnEditorActionListener()不会触发
作者:互联网
当我按下输入按钮时,我正试图设置一个侦听器到EditText.但它根本没有触发.我使用Android 4.2.2在LG Nexus 4上进行了测试. setOnEditorActionListener适用于安卓2.3的亚马逊Kindle Fire,而setImeActionLabel无处可行!我也无法为Enter按钮设置文本.这是代码:
mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return true;
}
});
我究竟做错了什么?我怎样才能解决这个问题?
解决方法:
你可以使用TextWatcher.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.charAt(s.length() - 1) == '\n') {
Log.d("TEST RESPONSE", "Enter was pressed");
}
}
});
标签:ime,android,java 来源: https://codeday.me/bug/20191001/1840567.html