Just started to learn android. In my program I want to use the dialog box of such a plan as below. It looks like AlertDialog, but I can't figure out how to display such a scroll wheel. Is this some kind of standard widget? Knowledgeable people, please send in the right direction.
1 answer
Yes, this is a standard component called NumberPicker .
The NumberPicker component from the Expert section allows you to select the desired number from a specified range. The principle of operation is similar to a revolving drum - you can scroll the numbers in one direction or the other. When the specified limit is reached, the numbers will continue to change in the specified range.
Here is a brief description of this component and how to use it.
And if you want to use the dialogue with this component, here is an example implementation: https://stackoverflow.com/a/17806895/2506123
Code from the example:
public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener { private static TextView tv; static Dialog d ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.textView1); Button b = (Button) findViewById(R.id.button11); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { show(); } }); } @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { Log.i("value is",""+newVal); } public void show() { final Dialog d = new Dialog(MainActivity.this); d.setTitle("NumberPicker"); d.setContentView(R.layout.dialog); Button b1 = (Button) d.findViewById(R.id.button1); Button b2 = (Button) d.findViewById(R.id.button2); final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1); np.setMaxValue(100); np.setMinValue(0); np.setWrapSelectorWheel(false); np.setOnValueChangedListener(this); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tv.setText(String.valueOf(np.getValue())); d.dismiss(); } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { d.dismiss(); } }); d.show(); } } activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Open" /> </RelativeLayout> dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <NumberPicker android:id="@+id/numberPicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="64dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/numberPicker1" android:layout_marginLeft="20dp" android:layout_marginTop="98dp" android:layout_toRightOf="@+id/numberPicker1" android:text="Cancel" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_marginRight="16dp" android:layout_toLeftOf="@+id/numberPicker1" android:text="Set" /> </RelativeLayout> res / values / dimens.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources> - Thank you very much! NumberPicker is exactly what I was looking for) - Alexander
|
