Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中有输入整数的视图吗?_Android_User Interface_Integer - Fatal编程技术网

Android中有输入整数的视图吗?

Android中有输入整数的视图吗?,android,user-interface,integer,Android,User Interface,Integer,我正在寻找类似日期选择器对话框的各个部分。一种视图,允许您输入可以限制的整数(仅限整数)(例如,1到10之间),您可以在视图中使用键盘或箭头。它存在吗 这是一个对话。一个请求整数的现成对话框也会有所帮助。您可能需要NumberPicker小部件。不幸的是,它位于com.android.internal.Widget.NumberPicker中,我们无法通过正常方式访问它 有两种使用方法: 从android源代码复制代码 使用反射来访问小部件 以下是在布局中使用它的xml: <com.and

我正在寻找类似日期选择器对话框的各个部分。一种视图,允许您输入可以限制的整数(仅限整数)(例如,1到10之间),您可以在视图中使用键盘或箭头。它存在吗


这是一个对话。一个请求整数的现成对话框也会有所帮助。

您可能需要
NumberPicker
小部件。不幸的是,它位于com.android.internal.Widget.NumberPicker中,我们无法通过正常方式访问它

有两种使用方法:

  • 从android源代码复制代码
  • 使用反射来访问小部件
  • 以下是在布局中使用它的xml:

    <com.android.internal.widget.NumberPicker
        android:id="@+id/picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    因为它是一个内部小部件,不在SDK中,如果使用反射,将来的兼容性可能会被破坏。最安全的做法是从源头上推出自己的产品


    此信息的原始来源在此共享。

    NumberPicker内部小部件已从Android源代码中提取并打包供您使用,您可以找到它。很好

    编辑:原始链接已关闭,您可以找到小部件的副本

    ,从API 11(Android 3.0)开始,NumberPicker现在在Android SDK中可用:

    对于Android<3.0,您可以在此处使用代码:


    您可以使用EditText使用
    android:inputType=“number”


    您只需使用
    编辑文本
    并将
    输入类型
    定义为
    数字
    。例如:

            <EditText
                android:id="@+id/etNumberInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:inputType="number" />
    
    

    这应该符合你的目标。

    @Alanmore似乎。。。。下面是我在开源应用程序中使用的源代码:第二个链接显示为well@ForrestBice我重新安排了源代码以使用Android库项目。该文件的新位置在此处:
    <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:inputType="number" android:layout_width="wrap_content"></EditText>
    
            <EditText
                android:id="@+id/etNumberInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:inputType="number" />
    
    
            final EditText et = findViewById(R.id.etNumberInput);
    
            et.addTextChangedListener(new TextWatcher() {
              
                public void afterTextChanged(Editable s) {}
                
                public void beforeTextChanged(CharSequence s, int start,
                                              int count, int after) {}
                
                public void onTextChanged(CharSequence s, int start,
                                          int before, int count) {
                    if (Integer.parseInt(et.getText().toString()) > 10) {
                        et.setError("***Your error here***");
                        // your logic here; to limit the user from inputting
                        // a value greater than specified limit
                    }
                }
            });