Android数字选择器-NumberPicker

实现

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

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

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

Formatter事件:

public String format(int value) {
       String tmpStr = String.valueOf(value);
       if (value < 10) {
           tmpStr = "0" + tmpStr;
       }
       return tmpStr;
 }

OnValueChangeListener事件:

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:滑动中

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

初始化:

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

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

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:

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

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

valuepicker = (NumberPicker) findViewById(R.id.valuepicker);
String[] city = {"立水桥","霍营","回龙观","龙泽","西二旗","上地"};
valuepicker.setDisplayedValues(city);
valuepicker.setMinValue(0);
valuepicker.setMaxValue(city.length - 1);
valuepicker.setValue(4);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值