How to disable searchview?

南楼画角 提交于 2019-11-28 08:25:32

问题


Can you help me on how to disable searchview when button is pressed? I'm trying this code:

searchView.setEnabled(false);
                searchView.setFocusableInTouchMode(false);
                searchView.clearFocus();

but it seems not working. I can still input text in searchview.

Thanks.. :))


回答1:


You can use:

searchView.clearFocus();

and if you want to hide it using:

searchView.setVisibility(View.GONE);



回答2:


All above questions don't work for me. Becase SearchView is a ViewGroup, so we have to disable all its child views.

private void enableSearchView(View view, boolean enabled) {
    view.setEnabled(enabled);
    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            enableSearchView(child, enabled);
        }
    }
}

In other place, call this:

enableSearchView(searchView, true/false);



回答3:


Try the following:

searchview.setInputType(InputType.TYPE_NULL);



回答4:


searchView.findViewById(R.id.search_button).setEnabled(false);

But you must check it for null and hide searchView:

ImageView searchBtn = (ImageView) searchView.findViewById(R.id.search_button);
if (searchBtn != null) searchBtn.setEnabled(false);
searchView.setVisibility(View.GONE);

I think this will work




回答5:


SearchView isn't the actual EditText you type into, but rather is a LinearLayout which holds a bunch of views, including the EditText.

To get the view you actually want for disabling it:

EditText searchViewEditText = (EditText) searchView.findViewById(R.id.search_src_text);

Note, this only works if you're using support v7 SearchView, since the particular resource id is internal if you use the framework SearchView.




回答6:


Did you try

searchView.setVisibility(View.GONE); ?




回答7:


This worked for me

ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // Don't perform any action.
        }
    });



回答8:


searchView.setOnQueryTextFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus && !v.isEnabled()) v.clearFocus();
            }
        }



回答9:


Try below code:

searchView.setIconified(true);



回答10:


If you want to clear it (SearchView), do:

searchView.clearFocus();

and if you want to hide it temporarily, do:

searchView.setVisibility(View.GONE);



来源:https://stackoverflow.com/questions/20482849/how-to-disable-searchview

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