How to use Android digital selector numberpicker

Time:2020-10-30

For example, if you need to download the number picker one hour before the introduction of Android 3.0, you can choose the address of the commonly used number picker one hour later. I didn’t use the open source project. I simply used numberpicker to show the effect. Let’s get down to business

Basic maintenance

Let’s take a look at the effects of developing things

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= "Shi" / >
 
    <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= "Points" / >
  </LinearLayout>
 
</LinearLayout>

Demo implementation

Word selection can slide, so you need to define an onvaluechangelistener event, onscrolllistener slide event, 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,
       "Old value" + oldval + "-- new value:
           + newVal, Toast.LENGTH_SHORT).show();
 }

Onscrolllistener sliding event. The sliding event has three states:

SCROLL_ STATE_ Flying: the hand is still sliding after it leaves

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, "follow up slide (fly, fly, stop at all)", Toast.LENGTH_ LONG)
         .show();
     break;
   case OnScrollListener.SCROLL_STATE_IDLE:
     Toast.makeText (this, "no sliding", Toast.LENGTH_ LONG).show();
     break;
   case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
     Toast.makeText (this, "sliding", Toast.LENGTH_ LONG)
         .show();
     break;
   }
 }

initialization:


hourPicker=(NumberPicker) findViewById(R.id.hourpicker);
  minutePicker=(NumberPicker) findViewById(R.id.minuteicker);
  init();

In 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,OnScrollListener,Formatter{...}

Finally, numberpicker can also display text. Redefine a numberpicker and load it:

valuepicker = (NumberPicker) findViewById(R.id.valuepicker);
    String [] city = {Lishuiqiao "," Huoying "," Huilongguan "," Longze "," Xierqi "," Shangdi "};
    valuepicker.setDisplayedValues(city);
    valuepicker.setMinValue(0);
    valuepicker.setMaxValue(city.length - 1);
    valuepicker.setValue(4);

The final result is shown as follows:

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.