set numeric input type to SearchView in ActionBar

2019-06-26 11:13发布

I don't manage to set the numeric input type to the SearchView in my ActionBar. I have the following searchable.xml file in my res/xml folder:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:inputType="number"
    android:label="@string/app_name"
    android:hint="@string/search_hint">
</searchable>  

And I set the searchable configuration to the SearchView as follows:

 // Get the SearchView and set the searchable configuration
 SearchManager searchManager = (SearchManager)    getSystemService(Context.SEARCH_SERVICE);
 SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

The search_hint is set correctly, but the line android:inputType="number" seems to have no effect, i.e. the numeric keypad doesn't appear.

Does anyone have a solution for this?

3条回答
ら.Afraid
2楼-- · 2019-06-26 11:52

This should work :

SearchView searchView = new SearchView(getContext());
searchView.setInputType(InputType.TYPE_CLASS_NUMBER);
查看更多
一夜七次
3楼-- · 2019-06-26 11:55

Add this in your searchable.xml layout file:

android:numeric="decimal"

or If not useful then try,

android:inputType="phone"

And let me know what happen.

查看更多
Viruses.
4楼-- · 2019-06-26 12:00

try this

android:inputType="phone"
查看更多
登录 后发表回答