Android開(kāi)發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法示例
本文實(shí)例講述了Android開(kāi)發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法。分享給大家供大家參考,具體如下:
簡(jiǎn)介:
NumberPicker: 用戶(hù)既可以從鍵盤(pán)輸值,也可以拖動(dòng)來(lái)選擇值
實(shí)際效果:

常用方法:
1. setMinValue() 設(shè)置組件支持的最小值
2. setMaxValue() 設(shè)置組建支持的最大值
3. setValue() 設(shè)置該組件的當(dāng)前值
在布局文件中調(diào)用:
<?xml version="1.0" encoding="utf-8" ?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:text="選擇時(shí)鐘"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<NumberPicker
android:id="@+id/np1"
android:solidColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:text="選擇分鐘"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<NumberPicker
android:id="@+id/np2"
android:solidColor="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" />
</TableRow>
</TableLayout>
關(guān)于監(jiān)聽(tīng)事件:
1. setOnValueChangedListener 調(diào)用監(jiān)聽(tīng)事件
2. onValueChange 具體執(zhí)行( int oldVal :之前詳實(shí)的數(shù)值 , int newVal 改變或現(xiàn)時(shí)的數(shù)值)
具體實(shí)現(xiàn)方法:
public class MainActivity extends Activity {
private NumberPicker np1,np2;
//定義上下限具體值
private int min = 10,max = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
np1 = (NumberPicker) findViewById(R.id.np1);
//設(shè)置np1的最大值只和最小值
np1.setMinValue(0);
np1.setMaxValue(23);
//設(shè)置哪怕的當(dāng)前值
np1.setValue(min);
np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
min = newVal;
showSelectedPrice();
}
});
np2 = (NumberPicker) findViewById(R.id.np2);
//設(shè)置np1的最大值只和最小值
np2.setMinValue(0);
np2.setMaxValue(23);
//設(shè)置哪怕的當(dāng)前值
np2.setValue(max);
np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
min = newVal;
showSelectedPrice();
}
});
}
private void showSelectedPrice(){
Toast.makeText(MainActivity.this,"設(shè)定鬧鐘時(shí)間為:" + min + " : " + max,Toast.LENGTH_SHORT).show();
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義標(biāo)尺滑動(dòng)選擇值效果
- android view實(shí)現(xiàn)橫向滑動(dòng)選擇
- Android實(shí)現(xiàn)滑動(dòng)選擇控件實(shí)例代碼
- Android自定義View實(shí)現(xiàn)左右滑動(dòng)選擇出生年份
- 輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件
- Android高仿IOS 滾輪選擇控件
- Android自定義控件之日期選擇控件使用詳解
- android實(shí)現(xiàn)雙日期選擇控件(可隱藏日,只顯示年月)
- Android滾輪選擇時(shí)間控件使用詳解
- Android?PickerScrollView滑動(dòng)選擇控件使用方法詳解
相關(guān)文章
Android 畫(huà)一個(gè)太極圖實(shí)例代碼
這篇文章主要介紹了Android 畫(huà)一個(gè)太極圖實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析
這篇文章主要介紹了Android中懸浮窗口的實(shí)現(xiàn)原理,以實(shí)例形式較為詳細(xì)的分析了Android懸浮窗口的原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android開(kāi)發(fā)中線(xiàn)程池源碼解析
這篇文章主要為大家詳細(xì)剖析了Android開(kāi)發(fā)中線(xiàn)程池源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Flutter快速制作一個(gè)水印組件實(shí)例詳解
這篇文章主要為大家介紹了Flutter快速制作一個(gè)水印組件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
實(shí)例探究Android應(yīng)用編寫(xiě)時(shí)Fragment的生命周期問(wèn)題
這篇文章主要介紹了Android應(yīng)用編寫(xiě)時(shí)Fragment的生命周期問(wèn)題探究,resumed和paused以及stoped三種狀態(tài)的控制需要熟練掌握,需要的朋友可以參考下2016-02-02
Android編程之界面實(shí)現(xiàn)全屏顯示的方法(2種方法)
這篇文章主要介紹了Android編程之界面實(shí)現(xiàn)全屏顯示的方法,結(jié)合實(shí)例分析了Java代碼中設(shè)置與Manifest文件設(shè)置2種實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01
Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表
本文詳細(xì)介紹了Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01

