【问题标题】:Hide keyboard on activity start on SearchView not working在 SearchView 上的活动开始时隐藏键盘不起作用
【发布时间】:2018-03-09 07:26:37
【问题描述】:

我有一种情况,我打开SearchActivity 并且我想将光标放在SearchView 中(保持焦点),而一开始不显示键盘。何时且仅当用户按下SearchView 我想显示键盘(光标应该完好无损)

【问题讨论】:

标签: android focus searchview


【解决方案1】:

activity.xml 文件的主布局中添加以下行

<LinearLayout
    ...
    android:focusableInTouchMode="true"
    ...
   >

【讨论】:

  • SearchView 有焦点,但键盘仍然出现。
【解决方案2】:

onCreateSearchActivity 中试试这个

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;

【讨论】:

    【解决方案3】:

    ManifestSearchActivity 中添加这一行

    <activity name=".SearchActivity"
    android:windowSoftInputMode="stateHidden" />
    

    或在活动中onCreate()添加

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    

    【讨论】:

    • SearchView 有焦点,但键盘仍然出现。
    • 我已经更新了答案,尝试使用 stateHidden 或代码方法
    【解决方案4】:

    将此添加到您的 onCreate 以隐藏 kayboard

    public void hideSoftKeyboard() {
        if(getCurrentFocus()!=null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }
    

    【讨论】:

      【解决方案5】:

      你使用下面的代码来显示键盘..

          InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);
      

      并关闭键盘使用了这个...

              imm.hideSoftInputFromWindow(searchView.getWindowToken(),0);
      

      【讨论】:

        【解决方案6】:

        你必须在 SearchView 获得焦点后隐藏软键盘:

        search.postDelayed({
                val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(view?.windowToken, 0)
            }, 100)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-05-19
          • 1970-01-01
          • 1970-01-01
          • 2011-02-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-02
          相关资源
          最近更新 更多