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

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

布局文件如下:

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还可用于字符串的滚动,类似于城市滚动选择等

你可能感兴趣的:(数值选择器(NumberPicker)的功能与用法)