How to disable searchview?

后端 未结 10 1761
别那么骄傲
别那么骄傲 2020-12-19 06:10

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

searchView.setEnabled(false);
                searchView.setFocu         


        
相关标签:
10条回答
  • 2020-12-19 07:08

    Did you try

    searchView.setVisibility(View.GONE); ?

    0 讨论(0)
  • 2020-12-19 07:09

    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);
    
    0 讨论(0)
  • 2020-12-19 07:10

    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.

    0 讨论(0)
  • searchView.setOnQueryTextFocusChangeListener( new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus && !v.isEnabled()) v.clearFocus();
                }
            }
    
    0 讨论(0)
提交回复
热议问题