如何设置一个搜索查看的输入类型为数字 [英] How to set a SearchView's input type to numeric

查看:172
本文介绍了如何设置一个搜索查看的输入类型为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个搜索查看 动作条在我的应用程序的(其中我从一个XML布局文件填充)我试图迫使其只需要数字输入。我试着设置机器人:inputType =号的XML属性,但没有任何效果。

有谁知道这个问题的根源?

XML的菜单资源:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目
        机器人:ID =@ + ID /搜索
        机器人:标题=@字符串/ menu_search
        机器人:actionViewClass =android.widget.SearchView
        机器人:图标=@可绘制/ ic_menu_search_holo_light
        机器人:inputType =号/>
< /菜单>
 

解决方案

如果你设置它在兼容性库的片段,你需要使用<一个href="http://developer.android.com/reference/android/support/v4/widget/SearchViewCompat.html#setInputType%28android.view.View,%20int%29"相对=nofollow> SearchViewCompat.setInputType(查看搜索查看,INT inputType)

  @覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
    inflater.inflate(R.menu.searchable,菜单);
    菜单项searchMenuItem = menu.findItem(R.id.action_search);
    如果(searchMenuItem!= NULL){
        搜索查看搜索查看=(搜索查看)searchMenuItem.getActionView();
        如果(搜索查看!= NULL){
            SearchViewCompat.setInputType(搜索查看,InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
        }
    }
}
 

I currently have a SearchView in my app's ActionBar (of which I am populating from an XML layout file) and I am trying to force it to only take numeric input. I've tried setting the android:inputType="number" attribute in XML, but it has no effect.

Does anyone know the source of this problem?

The XML menu resource:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/search"
        android:title="@string/menu_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_menu_search_holo_light"
        android:inputType="number" />       
</menu>

解决方案

If you're setting it in a compatibility library fragment, you need to use SearchViewCompat.setInputType(View searchView, int inputType):

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.searchable, menu);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    if (searchMenuItem != null) {
        SearchView searchView = (SearchView) searchMenuItem.getActionView();
        if (searchView != null) {
            SearchViewCompat.setInputType(searchView, InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
        }
    }
}

这篇关于如何设置一个搜索查看的输入类型为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆