OGeek|极客世界-中国程序员成长平台

标题: Android:如何让键盘输入按钮说 "Search"并处理它的点击? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:20
标题: Android:如何让键盘输入按钮说 "Search"并处理它的点击?

我想不通。一些应用程序有一个 EditText(文本框),当您触摸它并调出屏幕键盘时,键盘有一个“搜索”按钮而不是输入键。

我想实现这一点。如何实现该搜索按钮并检测搜索按钮的按下?

编辑 :找到了如何实现搜索按钮;在 XML 中,android:imeOptions="actionSearch"或在 Java 中,EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH); .但是如何处理用户按下该搜索按钮?跟android:imeActionId有关系吗? ?



Best Answer-推荐答案


在布局中将您的输入法选项设置为搜索。

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

在 java 中添加编辑器 Action 监听器。
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

关于Android:如何让键盘输入按钮说 "Search"并处理它的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3205339/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4