Android SearchView empty string

前端 未结 2 682
别跟我提以往
别跟我提以往 2021-02-06 12:26

I\'m trying to use a SearchView and I got everything to work, except when I want to search an empty string.
The onQueryTextChange does react when I remove the last character

2条回答
  •  心在旅途
    2021-02-06 13:02

    I had same issue with SearchView but I did not want to use ActionBarSherlock so I came up with solution which consists of styling your own SearchView as shown in guide http://novoda.com/blog/styling-the-actionbar-searchview/ and setting OnEditorActionListener for EditText( How do I trigger an action when the user has hit enter?) which is part of the SearchView.

    final SearchView searchView = (SearchView)findViewById(R.id.search);
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
    searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //Do something
            }
            return false;
    
        });
    

提交回复
热议问题