Android:当AutoCompleteTextView获得焦点时禁用(或改变)返回键

标签 android return

我的应用有一个用于搜索的 AutoCompleteTextView。当它处于焦点状态时,我想禁用或更改返回键的功能以进行特定的函数调用。我尝试在我的布局 xml 中将以下属性添加到 AutoCompleteTextView

    android:imeOptions="actionDone"

但它可以在我的模拟器上运行(当您单击回车时,键盘会消失)但它不能在我的设备上运行(运行 2.3.3 的 moto droidx)。

谁能告诉我如何使用 android:imeOptions="actionGo"将返回键链接到特定功能(在我的例子中是搜索功能)?

最佳答案

EditText 系列的 setOnEditorActionListener 事件中编写代码。喜欢

autoEditText.setOnEditorActionListener(new OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                   // in.hideSoftInputFromWindow(autoEditText.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                   //Commented line is for hide keyboard. Just make above code as comment and test your requirement
                   //It will work for your need. I just putted that line for your understanding only
                   //You can use own requirement here also.
                }
                return false;
            }
        });

快乐编码:)

关于Android:当AutoCompleteTextView获得焦点时禁用(或改变)返回键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7266143/

相关文章:

android - 如何允许第 3 方应用程序在 Android 上为我的应用程序设置主题?

javascript - Node js 没有正确返回函数的值

c - C语言中如何返回一个数组?

PHP:exit()、die() 和 return 之间有什么区别;在 "self"和包含的文件中?

android - 导航到 fragment 时应用程序崩溃

android - registerEntityModifier 完成

java - 需要了解我引用的代码中 Android Studio 检查 "condition is always true"的原因

javascript - 评估后的返回值

c - 不能有返回值

Java 短时间显示消息框并使其自行消失