首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:如何让键盘进入按钮说“搜索”并处理它的点击?

Android:如何让键盘进入按钮说“搜索”并处理它的点击?
EN

Stack Overflow用户
提问于 2010-07-08 15:36:41
回答 5查看 204.5K关注 0票数 421

我搞不懂这个。有些应用程序在EditText (textbox)的帮助下,当你触摸它并打开屏幕键盘时,键盘上有一个“搜索”按钮,而不是一个enter键。

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

编辑:找到了如何实现搜索按钮;在XML、android:imeOptions="actionSearch"EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);中。但是如何处理用户按下搜索按钮呢?这和android:imeActionId有关吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-07-08 15:44:26

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

代码语言:javascript
运行
复制
<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

在java中添加编辑器操作侦听器。

代码语言:javascript
运行
复制
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;
    }
});
票数 1K
EN

Stack Overflow用户

发布于 2017-03-16 08:50:23

当用户单击搜索时隐藏键盘。除了Robby Pond的答案

代码语言:javascript
运行
复制
private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    //...perform search
}
票数 29
EN

Stack Overflow用户

发布于 2016-11-29 13:15:40

xml文件中,放置imeOptions="actionSearch"inputType="text"maxLines="1"

代码语言:javascript
运行
复制
<EditText
    android:id="@+id/search_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:maxLines="1" />
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3205339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档