問題描述
我只是想在用戶在 editText 上按 enter 時捕捉事件.
I simply want to catch the event when the user press enter on an editText.
我沒有收到 Toast 消息,不是按下 Enter",也不是按下了某個鍵!"要么.
I did not get the Toast message, not the "Enter pressed" and not the "Some key pressed!" either.
我做錯了什么?
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;
}
});
它在 Android 2.3.3 上運行,在 4.1.2 上運行不正常有什么想法可以讓我在任何安卓設備上使用它嗎?
Well it is working on Android 2.3.3 and not working on 4.1.2 Any ideas how can i make this work on any android device?
推薦答案
請嘗試將EditText
設置為單行模式.
Please try to set the EditText
in single line mode.
editText.setSingleLine();
或在您的 xml 布局文件中:
or in your xml layout file:
android:singleLine="true"
更新
已棄用.從現在開始,使用 android:singleLine="true"
android:maxLines="1"
和 android:inputType="text"
來實現這個行為
is deprecated. From now on, use android:singleLine="true"
android:maxLines="1"
with android:inputType="text"
for achieving this behaviour
或
editText.setMaxLines(1);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
用于以編程方式設置.
這篇關于OnEditorActionListener 不工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!