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

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>
Demo實(shí)現(xiàn)
字選擇是可以滑動,所以需要定義一個OnValueChangeListener事件,OnScrollListener滑動事件,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,
"原來的值 " + oldVal + "--新值: "
+ newVal, Toast.LENGTH_SHORT).show();
}
OnScrollListener滑動事件,滑動事件有三個狀態(tài):
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, "后續(xù)滑動(飛呀飛,根本停下來)", 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方法中,設(shè)置數(shù)字的最大值,最小值,以及滑動事件:
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{...}
最后說一點(diǎn)就是NumberPicker也是可以顯示文字的,重新定義一個NumberPicker,加載一下:
valuepicker = (NumberPicker) findViewById(R.id.valuepicker);
String[] city = {"立水橋","霍營","回龍觀","龍澤","西二旗","上地"};
valuepicker.setDisplayedValues(city);
valuepicker.setMinValue(0);
valuepicker.setMaxValue(city.length - 1);
valuepicker.setValue(4);
最后顯示的效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)獲取簽名及公鑰的方法
這篇文章主要介紹了Android實(shí)現(xiàn)獲取簽名及公鑰的方法,可實(shí)現(xiàn)Android通過包名獲取相關(guān)簽名及公鑰的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android使用http協(xié)議與服務(wù)器通信的實(shí)例
本篇文章主要介紹了Android使用http協(xié)議與服務(wù)器通信,Android與服務(wù)器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post兩種方式。感興趣的小伙伴們可以參考一下。2016-12-12
Android?shape與selector標(biāo)簽使用詳解
Android中提供一種xml的方式,讓我們可以自由地定義背景,比較常用的就是shape標(biāo)簽和selector標(biāo)簽,這篇文章主要介紹了Android?shape與selector標(biāo)簽使用,需要的朋友可以參考下2022-05-05
Android Compose狀態(tài)實(shí)例詳解
這篇文章主要介紹了Android Compose狀態(tài),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Android中使用TextView實(shí)現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點(diǎn)點(diǎn),需要用到<img>標(biāo)簽。接下來通過本文給大家介紹Android中使用TextView實(shí)現(xiàn)圖文混排的方法,希望對大家有所幫助2016-02-02
Android 處理OnItemClickListener時關(guān)于焦點(diǎn)顏色的設(shè)置問題
這篇文章主要介紹了Android 處理OnItemClickListener時關(guān)于焦點(diǎn)顏色的設(shè)置問題的相關(guān)資料,需要的朋友可以參考下2017-02-02

