Setting iconified to false on android SearchView cause view to not be searchable

爱⌒轻易说出口 提交于 2019-12-11 13:48:15

问题


This question is a follow up to Show android SearchView EditText by default. My SearchView used to work fine until I added android:iconifiedByDefault="false" to the layout. Now, when I click on the icon or type in the search field, no search takes place. Does anyone know how to fix this problem? here is my SearchView as it exists now.

<SearchView
    android:id="@+id/search"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:queryHint="@string/search_hint"
    android:iconifiedByDefault="false"
    android:textSize="16sp" />

I already tried the following:

mSearch.setOnSearchClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            getActivity().onSearchRequested();
        //other stuff happen here
        }
    });

回答1:


I solved the problem with

public void onFocusChange(View v, boolean hasFocus) {
  if (hasFocus) { ...}
}



回答2:


i solve this problem by simply adding requestFocus tag in SearchView widget.

    <SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/search"
    android:clickable="true"
    android:queryHint="Enter Song name.."
    android:iconifiedByDefault="false">

    <requestFocus/>

</SearchView>


来源:https://stackoverflow.com/questions/20981613/setting-iconified-to-false-on-android-searchview-cause-view-to-not-be-searchable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!