美文网首页
Android软键盘回车键修改为搜索按键

Android软键盘回车键修改为搜索按键

作者: aldonk | 来源:发表于2018-04-22 20:00 被阅读0次

效果:

原回车键 修改后

一.设置显示搜索按钮

在xml文件的布局中给需要显示搜索按钮的控件(如EditText)加上

android:imeOptions="actionSearch"

android:singleLine="true"

如果android:singleLine="true"过时,可以替换为

android:imeOptions="actionSearch"

android:inputType="text"

二.给搜索按钮设置监听

et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {

    @Override

    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if (actionId == EditorInfo.IME_ACTION_SEARCH){

        // 先隐藏键盘  

        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))  

            .hideSoftInputFromWindow(SearchEnterprise.this  

                .getCurrentFocus().getWindowToken(),  

                    InputMethodManager.HIDE_NOT_ALWAYS);

        //接下来写点击搜索按钮之后的逻辑

        return true;

        }

        return false;

    }

});

相关文章

网友评论

      本文标题:Android软键盘回车键修改为搜索按键

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