【问题标题】:Android - How to hide keyboard when search button of the keyboard is pressed?Android - 按下键盘的搜索按钮时如何隐藏键盘?
【发布时间】:2015-01-25 02:15:33
【问题描述】:

当按下键盘的搜索按钮时如何隐藏键盘?

当用户在edittext中完成输入并按下键盘的“搜索按钮”时,键盘应该被隐藏..

【问题讨论】:

    标签: android keyboard


    【解决方案1】:

    在您的 xlm 中将 imeOptions 设置为“actionSearch”以编辑文本

    为你的EditText初始化监听器

    searchEditText.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;
        }
    });
    

    当用户点击搜索时隐藏键盘。

    private void performSearch() {
        searchEditText.clearFocus();
        InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
        ... perform search ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多