Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

user interface - Is there a view for inputing integers in Android?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

There are two ways to use it:

  1. Copy the code from android source
  2. Use reflection to access the widget

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"/>

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());
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...