【问题标题】:SearchView setIconified(false) automatically calls the focus for the searchview. How can I disable this?SearchView setIconified(false) 自动调用 searchview 的焦点。我怎样才能禁用它?
【发布时间】:2015-10-23 07:29:39
【问题描述】:

我有以下代码:

  if(mSearchView != null){
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setIconified(false);
        mSearchView.setOnQueryTextListener(this);
        int searchPlateId = mSearchView.getContext().getResources()
                .getIdentifier("android:id/search_plate", null, null);
        View searchPlateView = mSearchView.findViewById(searchPlateId);
        if (searchPlateView != null) {
            searchPlateView.setBackgroundColor(getResources().getColor(R.color.white));
        }
    }

问题是我在serachview上setIconified(false)的那一刻,键盘弹出来了,我不希望这种情况发生。有可能以某种方式防止这种情况吗? PS:我在清单中有这个:

 android:windowSoftInputMode="adjustNothing|stateHidden"

也可以在 onCreate 中以编程方式执行此操作,但没有运气

【问题讨论】:

  • mSearchView.clearFocus() 对你有用吗?
  • 不,也试过 listview.requestFocus() 问题是,即使我说 clearFocus,我设置 setIconified(false) 的那一刻它会调用键盘。现在我可以清除焦点之后。它适用于新的现代手机。但是在速度慢的手机上,键盘会出现,然后消失(即使在新手机上,在LG G3上测试,即使键盘没有出现,图标仍然会出现在状态栏上并消失
  • 上面的代码,你是在activity的onCreate中调用这个吗?
  • 是的,我在 onCreate 中有一个“init()”函数,它调用了这段代码

标签: android keyboard android-softkeyboard searchview soft-keyboard


【解决方案1】:

试试这个:

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();

【讨论】:

  • 非常好的解决方案。
【解决方案2】:

以编程方式删除所有字段,如

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();

并通过xml属性设置搜索视图:

<!-- Dummy item to prevent Search view from receiving focus -->

    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->

    <android.support.v7.widget.SearchView android:id="@+id/serach_view"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:nextFocusUp="@id/serach_view" 
        android:nextFocusLeft="@id/serach_view"/>

它会起作用的。

查看此链接以供参考 enter link description here

【讨论】:

    【解决方案3】:

    我也遇到了同样的问题。我花了很多时间来解决这个问题,最后我创建了一个隐藏在 xml 中的虚拟 EditText,并请求关注 java 代码中的编辑文本

        mSearchView.setFocusable(false);
        mSearchView.setIconified(false);
        mSearchView.clearFocus();
        EditText editText = (EditText) findViewById(R.id.dummy);
        editText.requestFocus();
    

    希望对某人有所帮助

    【讨论】:

      【解决方案4】:

      我改用了edittext,并自己制作了搜索图标和关闭图标的功能。这样,我不必将“setIconifiedByDefault”设置为编辑文本。它解决了我所有的问题

      【讨论】:

        【解决方案5】:
            searchView.setFocusable(false);
            searchView.setIconified(false);
            searchView.clearFocus();
        

        【讨论】:

          【解决方案6】:

          只需将 Search View Iconified 设置为 true。

          mSearchView.setIconified(true);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-07-27
            • 2015-11-06
            • 1970-01-01
            • 2016-01-22
            • 1970-01-01
            • 1970-01-01
            • 2016-06-08
            • 1970-01-01
            相关资源
            最近更新 更多