Android數(shù)字選擇器NumberPicker使用詳解
數(shù)字選擇器NumberPicker是Android3.0之后引入的一個(gè)控件,比較常用,比如說(shuō)手機(jī)常用的鬧鐘,可以選擇小時(shí)和分鐘,如果你需要兼容3.0之前版本,GitHub上有開(kāi)源的項(xiàng)目,具體的下載地址。本人就沒(méi)有使用開(kāi)源的項(xiàng)目,就簡(jiǎn)單的使用了NumberPicker顯示一下效果,開(kāi)始正題吧:
基礎(chǔ)維護(hù)
開(kāi)發(fā)東西先看下效果吧:

NumberPicker和TextView顯示一下時(shí)間,線性布局,看下布局文件吧:
<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="時(shí)" />
<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實(shí)現(xiàn)
字選擇是可以滑動(dòng),所以需要定義一個(gè)OnValueChangeListener事件,OnScrollListener滑動(dòng)事件,F(xiàn)ormatter事件:
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,
"原來(lái)的值 " + oldVal + "--新值: "
+ newVal, Toast.LENGTH_SHORT).show();
}
OnScrollListener滑動(dòng)事件,滑動(dòng)事件有三個(gè)狀態(tài):
SCROLL_STATE_FLING:手離開(kāi)之后還在滑動(dòng)
SCROLL_STATE_IDLE:不滑動(dòng)
SCROLL_STATE_TOUCH_SCROLL:滑動(dòng)中
public void onScrollStateChange(NumberPicker view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_FLING:
Toast.makeText(this, "后續(xù)滑動(dòng)(飛呀飛,根本停下來(lái))", Toast.LENGTH_LONG)
.show();
break;
case OnScrollListener.SCROLL_STATE_IDLE:
Toast.makeText(this, "不滑動(dòng)", Toast.LENGTH_LONG).show();
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
Toast.makeText(this, "滑動(dòng)中", Toast.LENGTH_LONG)
.show();
break;
}
}
初始化:
hourPicker=(NumberPicker) findViewById(R.id.hourpicker); minutePicker=(NumberPicker) findViewById(R.id.minuteicker); init();
init方法中,設(shè)置數(shù)字的最大值,最小值,以及滑動(dòng)事件:
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{...}
最后說(shuō)一點(diǎn)就是NumberPicker也是可以顯示文字的,重新定義一個(gè)NumberPicker,加載一下:
valuepicker = (NumberPicker) findViewById(R.id.valuepicker);
String[] city = {"立水橋","霍營(yíng)","回龍觀","龍澤","西二旗","上地"};
valuepicker.setDisplayedValues(city);
valuepicker.setMinValue(0);
valuepicker.setMaxValue(city.length - 1);
valuepicker.setValue(4);
最后顯示的效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能
這篇文章主要介紹了使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能,對(duì)android指紋驗(yàn)證相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2021-07-07
Moshi?完美解決Gson在kotlin中默認(rèn)值空的問(wèn)題詳解
這篇文章主要為大家介紹了Moshi?完美解決Gson在kotlin中默認(rèn)值空的問(wèn)題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android Activity Results API代替onActivityResul
說(shuō)到onActivityResult,我們已經(jīng)非常熟悉來(lái),通過(guò)在A activity啟動(dòng)B activity并且傳入數(shù)據(jù)到B中,然后在A中通過(guò)onActivityResult來(lái)接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09
kotlin實(shí)現(xiàn)五子棋單機(jī)游戲
這篇文章主要為大家詳細(xì)介紹了kotlin實(shí)現(xiàn)五子棋單機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Android應(yīng)用中通過(guò)Layout_weight屬性用ListView實(shí)現(xiàn)表格
這篇文章主要介紹了Android應(yīng)用中通過(guò)Layout_weight屬性用ListView實(shí)現(xiàn)表格的方法,文中對(duì)Layout_weight屬性先有一個(gè)較為詳細(xì)的解釋,需要的朋友可以參考下2016-04-04
Android中post請(qǐng)求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例
下面小編就為大家分享一篇Android中post請(qǐng)求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽(tīng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
解決Android使用Handler造成內(nèi)存泄露問(wèn)題
內(nèi)存泄露的危害就是會(huì)使虛擬機(jī)占用內(nèi)存過(guò)高,導(dǎo)致OOM(內(nèi)存溢出),程序出錯(cuò)。接下來(lái)通過(guò)本文給大家分享Android使用Handler造成內(nèi)存泄露問(wèn)題及解決方法,一起看看吧2017-08-08
Android中ProgressBar用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中ProgressBar用法,以簡(jiǎn)單實(shí)例形式分析了Android中ProgressBar進(jìn)度條控件的功能與布局相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)編程修改手機(jī)靜態(tài)IP的方法
這篇文章主要介紹了Android實(shí)現(xiàn)編程修改手機(jī)靜態(tài)IP的方法,涉及Android編程實(shí)現(xiàn)對(duì)系統(tǒng)底層信息修改的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

