Android数字选择器-NumberPicker

数字选择器NumberPicker是Android3.0之后引入的一个控件,比较常用,比如说手机常用的闹钟,可以选择小时和分钟,如果你需要兼容3.0之前版本,GitHub上有开源的项目,具体的下载地址https://github.com/SimonVT/android-numberpicker。本人就没有使用开源的项目,就简单的使用了NumberPicker显示一下效果,开始正题吧:

基础维护

开发东西先看下效果吧:

NumberPicker和TextView显示一下时间,线性布局,看下布局文件吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<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实现

数字选择是可以滑动,所以需要定义一个OnValueChangeListener事件,OnScrollListener滑动事件,Formatter事件:

Formatter事件:

1
2
3
4
5
6
7
public String format(int value) {
       String tmpStr = String.valueOf(value);
       if (value < 10) {
           tmpStr = "0" + tmpStr;
       }
       return tmpStr;
   }

 OnValueChangeListener事件:

1
2
3
4
5
6
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
      Toast.makeText(
              this,
              "原来的值 " + oldVal + "--新值: "
                      + newVal, Toast.LENGTH_SHORT).show();
  }

OnScrollListener滑动事件,滑动事件有三个状态:

SCROLL_STATE_FLING:手离开之后还在滑动

SCROLL_STATE_IDLE:不滑动

SCROLL_STATE_TOUCH_SCROLL:滑动中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
      }
  }

 初始化:

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

 init方法中,设置数字的最大值,最小值,以及滑动事件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
 }

  还差一步,Activity需要继承一下OnValueChangeListener,OnScrollListener,Formatter:

1
public class MainActivity extends Activity implements OnValueChangeListener,OnScrollListener,Formatter{...}

 最后说一点就是NumberPicker也是可以显示文字的,重新定义一个NumberPicker,加载一下:

1
2
3
4
5
6
valuepicker = (NumberPicker) findViewById(R.id.valuepicker);
        String[] city = {"立水桥","霍营","回龙观","龙泽","西二旗","上地"};
        valuepicker.setDisplayedValues(city);
        valuepicker.setMinValue(0);
        valuepicker.setMaxValue(city.length - 1);
        valuepicker.setValue(4);

  最后显示的效果:

posted @   Fly_Elephant  阅读(21975)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示