문제

I am using the Android SearchManager. I defined all the needed attributes in the searchable.xml.

Now, I would like to set a limit for minimum numer of characters before the user can make the search. By default, the min is 1.

How can this be changed?

Thank you.

도움이 되었습니까?

해결책

Its the following attribute in searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  ...
  android:searchSuggestThreshold="2"
  >
</searchable>

다른 팁

if onQueryTextSubmit return false then only search action is performing , we can limit chars like this

    @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_home, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        //searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return query.length()<4; //number of char to limit
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top