是否有在 Android 中输入整数的视图? [英] Is there a view for inputing integers in Android?

查看:25
本文介绍了是否有在 Android 中输入整数的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找类似于日期选择器对话框的各个部分的内容.允许您输入可以限制的整数(并且仅整数)的视图(例如,介于 1 和 10 之间),您可以在其中使用键盘或视图本身中的箭头.存在吗?

I'm looking for something like the individual parts of the date picker dialog. A view that allows you to input integers (and only integers) that you can limit (between 1 and 10 for example), where you can use the keyboard or the arrows in the view itself. Does it exists?

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

It is for a dialog. A ready-made dialog to request an integer would also help.

推荐答案

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

The NumberPicker widget is probably what you want. Unfortunately it's located in com.android.internal.Widget.NumberPicker which we cannot get to through normal means.

有两种使用方式:

  1. 从android源复制代码
  2. 使用反射访问小部件

这是在布局中使用它的 xml:

Here's the xml for using it in a layout:

<com.android.internal.widget.NumberPicker
    android:id="@+id/picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

这是设置 NumberPicker 设置的反射(我没有测试过):

Here's the reflection to set the NumberPicker settings (I have not tested this):

Object o = findViewById(R.id.picker);
Class c = o.getClass();
try 
{
    Method m = c.getMethod("setRange", int.class, int.class);
    m.invoke(o, 0, 9);
} 
catch (Exception e) 
{
    Log.e("", e.getMessage());
}

由于它是一个内部小部件而不是在 SDK 中,如果您使用反射,可能会破坏未来的兼容性.从源头上推出自己的产品是最安全的.

Since it's an internal widget and not in the SDK, future compatibility could be broken if you use reflection. It would be safest to roll your own from the source.

此信息的原始来源在此 Google 群组.

The original source for this information is shared in this Google Group.

这篇关于是否有在 Android 中输入整数的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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