Android 将软键盘Enter变为搜索

来源:互联网 发布:数据魔方为什么下线 编辑:程序博客网 时间:2024/06/10 05:34

主要用到的是EditText的一个内部属性:android:imeOptions , 不同属性值对应的含义如下

1.actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.2.actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE3.actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 4.actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH5.actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND6.actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT7.actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE

如果要显示为搜索的话,你可以下面这么做:

  • 在布局中的EditText添加属性:
android:imeOptions="actionSearch"
  • 在代码中设置监听事件
 //键盘中显示搜索按钮        etKeyWord.setOnEditorActionListener(new TextView.OnEditorActionListener() {            @Override            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == KeyEvent.KEYCODE_ENTER) {                    //先隐藏键盘                    ((InputMethodManager) etKeyWord.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))                            .hideSoftInputFromWindow(                                    InputTagActivity.this                                            .getCurrentFocus()                                            .getWindowToken(),                                    InputMethodManager.HIDE_NOT_ALWAYS);                    //实现搜索逻辑                    gotoSearch();                    return true;                }                return false;            }        });
0 0
原创粉丝点击