数值选择器(NumberPicker)的功能与用法

2016-06-29  本文已影响960人  babybus_hentai

安卓原生自带控件
setMinValue(int minVal):设置该组件支持的最小值。
setMaxValue(int maxVal):设置该组件支持的最大值。
setValue(int value):设置该组件的当前值。

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><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" android:background="@color/red_dark" > <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="80dp" 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:visibility="invisible" android:id="@+id/minuteicker" android:layout_width="40dp" android:layout_height="wrap_content" /> <TextView android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="秒" /> </LinearLayout></LinearLayout>

java代码

new一个控件对象:
protected NumberPicker hourPicker;

对布局中的控件进行初始化操作
hourPicker =(NumberPicker) findViewById(R.id.hourpicker);hourPickerinit = hourPicker;hourPicker.setFormatter(this);hourPicker.setOnValueChangedListener(this);hourPicker.setOnScrollListener(this);hourPicker.setMaxValue(60);int timenum = SpUtil.getInt( Constant.INIT_MIN,5);hourPicker.setMinValue(1);hourPicker.setValue(timenum);Log.e("TimeSetInitActivity","setValue--onResume");

对其监听:
public void onScrollStateChange(NumberPicker view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_FLING: break; case OnScrollListener.SCROLL_STATE_IDLE: hourPickerinit.setValue(hourPickerinit.getValue()); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: break; }}

public void onValueChange(NumberPicker picker, int oldVal, int newVal) { ToastUtil.toastShort(hourPickerinit.getValue()+"---onValueChange"); ToastUtil.toastShort("---onValueChange");}

整合即可使用,numberpicker还可用于字符串的滚动,类似于城市滚动选择等

上一篇 下一篇

猜你喜欢

热点阅读