edittext 点击软键盘回车键进行搜索

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mr___Xu/article/details/81116902

第一步在xml中:

android:imeOptions="actionSearch"
android:singleLine="true"

第二步代码中实现:
normal_title_blue_center_tv.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
normal_title_blue_center_tv.setInputType(EditorInfo.TYPE_CLASS_TEXT);
normal_title_blue_center_tv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH){
            ToastShowUtils.show(getActivity(), "搜索一下~~~", 2);
//下边的代码是在点击软件盘回车搜索的时候收起软键盘 
            InputMethodManager imm = (InputMethodManager)        getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
            return true;
        }
        return false;
    }
});

猜你喜欢

转载自blog.csdn.net/Mr___Xu/article/details/81116902