OnEditorActionListener not working(OnEditorActionListener 不工作)
问题描述
我只是想在用户在 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 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OnEditorActionListener 不工作
基础教程推荐
- 我的 UIImageView 的任务 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
