Android SearchView onclick

2019-04-21 06:49发布

问题:

I have a searchView which looks like this:

private void setupSearchView() {
    mSearchView = (SearchView) getActivity().findViewById(
            R.id.search_view_neue);
    setSearchViewBackground();
    mSearchView.setOnClickListener(this);
    mSearchView.setOnQueryTextListener(this);
}




    public boolean onQueryTextSubmit(String query) {
    searchcounter = searchcounter + 1;

    setSearchViewBackground();
    ArrayList<WissensdokumenteRecord> documents = settingListContent(new ArrayList<WissensdokumenteRecord>());

    setListAdapter(new NeueWissensdokumentItemAdapter(
            inflater.getContext(), R.layout.row_example, documents));
    InputMethodManager im = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    im.hideSoftInputFromWindow(getActivity().getCurrentFocus()
            .getWindowToken(), 0);

    return false;
}




<SearchView
    android:id="@+id/search_view_neue"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:background="@drawable/border_searchview"
    android:maxWidth="540dp"
    android:queryHint="Neue Dokumente suchen" >
</SearchView>

So, the behaviour of the search view is, that the keyboard opens by clicking the search button. Now I can search for something. Is it possible to do a search by clicking anywhere in the searchview? Does anyone has an example for me?

回答1:

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.



回答2:

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


回答3:

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


回答4:

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



回答5:

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