數(shù)值選擇器(NumberPicker)的功能與用法

安卓原生自帶控件
setMinValue(int minVal):設(shè)置該組件支持的最小值。
setMaxValue(int maxVal):設(shè)置該組件支持的最大值。
setValue(int value):設(shè)置該組件的當(dāng)前值。

布局文件如下:

<?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一個(gè)控件對(duì)象:
protected NumberPicker hourPicker;

對(duì)布局中的控件進(jìn)行初始化操作
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");

對(duì)其監(jiān)聽:
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還可用于字符串的滾動(dòng),類似于城市滾動(dòng)選擇等

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容