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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
NumberPicker
小部件可能就是您想要的。 不幸的是,它位于 com.android.internal.Widget.NumberPicker 中,我们无法通过正常方式访问它。有两种使用方法:
这是在布局中使用它的 xml:
这是设置 NumberPicker 设置的反射(我还没有测试过这个):
因为它是内部的小部件而不是在 SDK 中,如果您使用反射,未来的兼容性可能会被破坏。 从源头开始自己开发是最安全的。
此信息的原始来源在此 Google 群组中共享。
The
NumberPicker
widget is probably what you want. Unfortunately it's located incom.android.internal.Widget.NumberPicker
which we cannot get to through normal means.There are two ways to use it:
Here's the xml for using it in a layout:
Here's the reflection to set the NumberPicker settings (I have not tested this):
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.
The original source for this information is shared in this Google Group.
NumberPicker 内部小部件已从 Android 源代码中提取并打包供您使用,您可以在此处找到它。 效果很好!
编辑:原始链接已关闭,您可以找到该小部件的副本此处
The NumberPicker internal widget has been pulled from the Android source code and packaged for your use and you can find it here. Works great!
EDIT: Original link is down, you can find a copy of the widget here
正如其他地方提到的,自 API 11 (Android 3.0) 起,NumberPicker 现已在 Android SDK 中提供:
http://developer.android.com/reference/android/widget/NumberPicker.html
对于 Android < 3.0,您可以使用这里的代码:
https://github.com/novak/numpicker-demo
https://github.com/mrn/numberpicker
As has been mentioned elsewhere, NumberPicker is now available in the Android SDK as of API 11 (Android 3.0):
http://developer.android.com/reference/android/widget/NumberPicker.html
For Android < 3.0, you can use the code here:
https://github.com/novak/numpicker-demo
https://github.com/mrn/numberpicker
您可以与 EditText 使用
android:inputType="number"
You can with EditText use
android:inputType="number"
您可以简单地使用
EditText
并将inputType
定义为number
。 例如:要将最大值限制为 10,您可以通过编程方式执行此操作:
这应该可以满足您的目标。
You can simply use an
EditText
and defineinputType
asnumber
. E.g:To limit the maximum value to, say 10, you can do it programmatically as:
This should meet your goal.