How to remove focus from SearchView?

前端 未结 10 1499
野趣味
野趣味 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 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:

    
    
    
        
    
    
    

    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();
    }
    

提交回复
热议问题