How to remove focus from SearchView?

前端 未结 10 1468
野趣味
野趣味 2021-02-06 22:20

I want to remove focus and text from SearchView in onResume().

I tried searchView.clearFocus() but it is not working.

Thi

相关标签:
10条回答
  • 2021-02-06 22:49

    Use this line of code in your onResume method

    if (searchView != null) {
        searchView.setQuery("", false);
        searchView.clearFocus();
        searchView.onActionViewCollapsed();
    }
    
    0 讨论(0)
  • 2021-02-06 22:52

    when in activity go to parent layout ,and in fragment too also do the same thing in framelayout. Just add this attribute:

    android:focusableInTouchMode="true"
    
    0 讨论(0)
  • 2021-02-06 22:54

    In your SearchView widget, just add

      android:focusable="false"
    

    Full widget code

        <android.support.v7.widget.SearchView
            android:id="@+id/your_search_view_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:iconifiedByDefault="false"
            app:queryHint="Search something..." 
            android:focusable="false" />
    
    0 讨论(0)
  • 2021-02-06 22:56

    Write this code in onResume()

      searchView.setText("");    
      this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     
    
    0 讨论(0)
  • 2021-02-06 22:59

    You can manually hide the keyboard in onResume() function as:

    InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
            View view = context.getCurrentFocus();
    
            if (view == null) {
                view = new View(context);
            }
            if (view != null) {
    
                IBinder iBinder = view.getWindowToken();
    
                 imm.hideSoftInputFromWindow(iBinder, 0);
             }
    
    0 讨论(0)
  • 2021-02-06 23:03

    I want to remove focus and text from SearchView in onResume()

    To achieve this you could set your root layout focusable with the android:focusableInTouchMode attribute and request focus on that View in onResume().

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:focusableInTouchMode="true">
    
        <SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false"/>
    
    </LinearLayout>
    

    And then in your Activity:

    private View rootView;
    private SearchView searchView;
    
    // ...
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        // ...
    
        rootView = findViewById(R.id.root_layout);
        searchView = (SearchView) findViewById(R.id.search_view);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        searchView.setQuery("", false);
        rootView.requestFocus();
    }
    
    0 讨论(0)
提交回复
热议问题