Android SearchView onclick

前端 未结 5 1822
迷失自我
迷失自我 2021-02-19 09:03

I have a searchView which looks like this:

private void setupSearchView() {
    mSearchView = (SearchView) getActivity().findViewById(
            R.id.search_vi         


        
5条回答
  •  暖寄归人
    2021-02-19 09:40

    I must suggest to use setOnSearchClickListener listner to detect SearchView click as According to Android docs :

    setOnSearchClickListener

    void setOnSearchClickListener (View.OnClickListener listener)

    Sets a listener to inform when the search button is pressed.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.orders_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    
        if (searchView != null) {
            searchView = (SearchView) searchItem.getActionView();
            searchView.setOnSearchClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  //perform your click operation here 
                }
            });
      return true;
    }
    

提交回复
热议问题