Android SearchView onclick

前端 未结 5 1820
迷失自我
迷失自我 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:29

    I don't see why you need to to set OnClickListener on it. By clicking on a SearchView you will update text cursor. To perform search you have a keyboard IME action. If clicking on it would perform search it would be impossible to change cursor etc. It is the generally wrong behaviour to make something else than focus / update cursor in text fields.

    As far as I remember - you don't need to hide keyboard in OnQueryTextListener. It should hite automatically. Handle your search in OnQueryTextSubmit

    search with Sherlock ActionBar SearchView setOnKeyListener

    0 讨论(0)
  • 2021-02-19 09:31

    I managed to do this in the following way:

    Setup the search view on click listener

    mSearchView.setOnClickListener(this);
    

    Catch the onClick event:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.searchView:
                mSearchView.onActionViewExpanded();
                break;
        }
    }
    

    In this way the keyboard and search are activated if you click antwhere on the search bar.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-19 09:42

    A search widget that intercepts all touches on its children and does a callback on touch down:

    class InterceptTouchSearchView(context: Context, attrs: AttributeSet) : SearchView(context, attrs) {
    
      var onActionDownIntercepted: (() -> (Unit))? = null
    
      override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
      }
    
      @SuppressLint("ClickableViewAccessibility")
      override fun onTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
          onActionDownIntercepted?.invoke()
        }
        return false
      }
    }
    

    Usage: search_view.onActionDownIntercepted = { onSearchFieldClicked() }

    0 讨论(0)
  • 2021-02-19 09:43

    This will do exactly what you're trying to achieve. setIconified(false) will keep the cross icon at this end of the SearchView so that the user can still cancel the same way as if the magnifying glass was clicked.

    searchView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchView.setIconified(false);
        }
    });
    
    0 讨论(0)
提交回复
热议问题