Android侦听器,其EditText上的返回键不适用于所有设备 [英] Android listener for return key on EditText not working for every device
问题描述
我想用返回键的侦听器初始化一个compose字段.在文档中说actionId
将是EditorInfo.IME_NULL
,如果由于按下回车键而被调用的话.我比较了actionId
的值.它还说,如果由返回键触发,我们会收到KeyEvent
对象,因此我将对其进行测试以测试KeyEvent.ACTION_UP
,该值对应于释放键所对应的值.
当我运行代码时,它对于运行KitKat的设备正常工作,但是在其他运行Lollilop的设备中,它不消耗返回键,并且不调用onEditorAction()
.它只是插入一个新行.这是代码:
I want to initialize a compose field with a listener for the return key. In the documentation it says that actionId
will be EditorInfo.IME_NULL
if it is being called due to the enter key being pressed. I compared actionId
for that value. It also says that if triggered by a returnkey we receive KeyEvent
object so i test it for KeyEvent.ACTION_UP
which is the value corresponding for the release of a key.
When i run the code it works for fine for a device running KitKat but in the other running Lollilop it doesn't consume return key and it doesn't call onEditorAction()
. It just inserts a new line. Here's the code:
public void setupChat() {
Log.i(LOG_TAG, "setupChat()");
oETConversation.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i(LOG_TAG, "onEditorAction()");
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
String s = v.getText().toString();
sendMessage(s);
}
return true;
}
});
}
public void sendMessage(String s) {
Log.i(LOG_TAG, "sendMessage()");
Log.i(LOG_TAG, s);
}
推荐答案
尝试一下,这是我使用keyEvent
来完成的方法:
Try this, this is how I do it using keyEvent
instead:
oETConversation.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
String s = oETConversation.getText().toString();
sendMessage(s);
return true;
}
return false;
}
});
希望这会有所帮助;)
更新
确保在您的xml中您的editText具有android:singleLine="true"
字段.
Make sure in your xml your editText has android:singleLine="true"
field.
这篇关于Android侦听器,其EditText上的返回键不适用于所有设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!