Android手机软键盘enter改为搜索

作者: Anwfly | 来源:发表于2019-03-06 13:48 被阅读11次

需求

Android 搜索 把软键盘上的回车键改为搜索。当用户聚焦EditText时弹出键盘,键盘右下角示意为“搜索”按钮;当用户输入内容后,点击搜索按钮将进行关键词搜索,当用户没有输入内容点击搜索按钮,将收起键盘。

实现(EditText)

1、设置属性
设置属性①:android:imeOptions="actionSearch",在该Editview获得焦点的时候将“回车”键改为“搜索”
设置属性②android:singleLine="true" 不然回车【搜索】会换行

2、代码中操作

etSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    //先隐藏键盘
                    ((InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getActivity().getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                    //其次再做相应操作
                    inputContent = etSearch.getText().toString();
                    if (StringUtils.isBlank(inputContent)) {
                    } else {
                        //做相应的操作
                    }
                }
                return false;
            }
        });

相关文章

网友评论

    本文标题:Android手机软键盘enter改为搜索

    本文链接:https://www.haomeiwen.com/subject/yfmtpqtx.html