Android OnEditorActionListener不工作
我只是想捕捉用户在editText上按enter键时发生的事件 我没有收到祝酒词,也没有收到“按下回车键”和“按下某个键!” 我做错了什么Android OnEditorActionListener不工作,android,android-edittext,Android,Android Edittext,我只是想捕捉用户在editText上按enter键时发生的事件 我没有收到祝酒词,也没有收到“按下回车键”和“按下某个键!” 我做错了什么 myEditText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { Toast.makeText(
myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(getApplicationContext(), "Some key pressed!", Toast.LENGTH_LONG).show();
if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(getApplicationContext(), "Enter pressed", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
电子数据交换:
它在安卓2.3.3上工作,而在4.1.2上不工作
有什么想法吗?我如何在任何安卓设备上实现这一点
myEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
Toast.makeText(getApplicationContext(), "some key pressed", Toast.LENGTH_LONG)
.show();
return false;
}
});
在我的编辑文本中键入内容后按enter键时,此代码显示我按下的某个键。但是我不知道您的代码有什么问题。请尝试将
编辑文本设置为单行模式
editText.setSingleLine();
或者在xml布局文件中:
android:singleLine="true"
更新
android:singleLine=“true”
已被弃用。从现在起,使用android:maxLines=“1”
和android:inputType=“text”
实现此行为
或
用于以编程方式进行设置。使用xml中的android:imeOptions=“actionGo”
actionDone
不再响应onEditorAction
我的问题是我在XMLandroid:inputType=“textMultiline”
中设置了。当我删除textMultiline
选项时,EditorListener
开始工作。android:inputType=“text”
和android:imeOptions=“actionGo”
我不知道为什么Atul Chatuverdi的答案被否决了,在浏览了很多次之后,这个答案最终给了我一个“奇迹”
我确认已完成的操作可能不起作用。它可以在我的三星手机上运行,但由于某些原因,它不能在Fly S上运行。事件只是没有启动。两者都是安卓7.1。我想知道,这是否是谷歌键盘的一个缺陷
我用来让它最终在所有设备上工作的代码是
<EditText
android:singleLine="true"
android:inputType="textUri"
android:maxLines="1"
android:imeOptions="actionGo"
...
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
以及处理该操作的嵌套类
private class onGo implements TextView.OnEditorActionListener {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_GO) {
// To do stuff
return true;
}
return false;
}
}
至于if(event!=null&&(event.getKeyCode()==KeyEvent.KEYCODE\u ENTER))
在多行文本中,我也有同样的问题。它可以与三星键盘(黑客的键盘)配合使用,但不能与谷歌键盘配合使用
由于各种原因,使用textwatcher的建议是不可取的(比如用enter符号粘贴文本,而不是按enter按钮等) 您可以将TextWatcher用于任何与EditText相关的事件,也可以使用其他输入类型(我认为如果它们阻止使用新行字符),例如“textPersonName”@CameronKetcham:没错。“InputType.TYPE\u TEXT\u VARIATION\u PASSWORD”也会起作用。你救了我一天!不确定这是旧信息,但它不适用于我的Nexus 6p 7.1
editText = view.findViewById(R.id.et_path);
...
editText.setOnEditorActionListener(new onGo());
private class onGo implements TextView.OnEditorActionListener {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_GO) {
// To do stuff
return true;
}
return false;
}
}