Detailed explanation of Android digital selector numberpicker
The digital selector numberpicker is a control introduced after Android 3.0. It is commonly used. For example, the alarm clock commonly used on mobile phones can select hours and minutes. If you need to be compatible with versions before 3.0, there is an open source project on GitHub, and the specific download address. I didn't use the open source project, so I simply used numberpicker to show the effect and get to the point:
Basic maintenance
Let's look at the effect of developing things first:
Numberpicker and textview show the time and linear layout. Take a look at the layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context="com.example.googlenumberpicker.MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginLeft="50dp" android:layout_gravity="center_horizontal" > <NumberPicker android:id="@+id/hourpicker" android:layout_width="40dp" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="时" /> <NumberPicker android:id="@+id/minuteicker" android:layout_width="40dp" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="分" /> </LinearLayout> </LinearLayout>
Demo implementation
Word selection can slide, so you need to define an onvaluechangelistener event, onscrolllistener sliding event and formatter event:
Formatter event:
public String format(int value) { String tmpStr = String.valueOf(value); if (value < 10) { tmpStr = "0" + tmpStr; } return tmpStr; }
Onvaluechangelistener event:
public void onValueChange(NumberPicker picker,int oldVal,int newVal) { Toast.makeText( this,"原来的值 " + oldVal + "--新值: " + newVal,Toast.LENGTH_SHORT).show(); }
Onscrolllistener sliding event. The sliding event has three states:
SCROLL_ STATE_ Fling: the hand is still sliding after leaving
SCROLL_ STATE_ Idle: no sliding
SCROLL_ STATE_ TOUCH_ Scroll: sliding
public void onScrollStateChange(NumberPicker view,int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_FLING: Toast.makeText(this,"后续滑动(飞呀飞,根本停下来)",Toast.LENGTH_LONG) .show(); break; case OnScrollListener.SCROLL_STATE_IDLE: Toast.makeText(this,"不滑动",Toast.LENGTH_LONG).show(); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: Toast.makeText(this,"滑动中",Toast.LENGTH_LONG) .show(); break; } }
initialization:
hourPicker=(NumberPicker) findViewById(R.id.hourpicker); minutePicker=(NumberPicker) findViewById(R.id.minuteicker); init();
In the init method, set the maximum value, minimum value and sliding event of the number:
private void init() { hourPicker.setFormatter(this); hourPicker.setOnValueChangedListener(this); hourPicker.setOnScrollListener(this); hourPicker.setMaxValue(24); hourPicker.setMinValue(0); hourPicker.setValue(9); minutePicker.setFormatter(this); minutePicker.setOnValueChangedListener(this); minutePicker.setOnScrollListener(this); minutePicker.setMaxValue(60); minutePicker.setMinValue(0); minutePicker.setValue(49); }
The activity needs to inherit onvaluechangelistener, onscrolllistener and formatter:
public class MainActivity extends Activity implements OnValuechangelistener,Formatter{...}
Finally, numberpicker can also display text. Redefine a numberpicker and load it:
valuepicker = (NumberPicker) findViewById(R.id.valuepicker); String[] city = {"立水桥","霍营","回龙观","龙泽","西二旗","上地"}; valuepicker.setDisplayedValues(city); valuepicker.setMinValue(0); valuepicker.setMaxValue(city.length - 1); valuepicker.setValue(4);
Last displayed effect:
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.