国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之滑動數值選擇器NumberPicker用法示例

2019-12-12 00:16:44
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發之滑動數值選擇器NumberPicker用法。分享給大家供大家參考,具體如下:

簡介:

NumberPicker: 用戶既可以從鍵盤輸值,也可以拖動來選擇值

實際效果:

常用方法:

1. setMinValue() 設置組件支持的最小值

2. setMaxValue() 設置組建支持的最大值

3. setValue() 設置該組件的當前值

在布局文件中調用:

<?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="選擇時鐘"      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>

關于監聽事件:

1. setOnValueChangedListener 調用監聽事件

2. onValueChange 具體執行( int oldVal :之前詳實的數值 , int newVal 改變或現時的數值)

具體實現方法:

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);    //設置np1的最大值只和最小值    np1.setMinValue(0);    np1.setMaxValue(23);    //設置哪怕的當前值    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);    //設置np1的最大值只和最小值    np2.setMinValue(0);    np2.setMaxValue(23);    //設置哪怕的當前值    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,"設定鬧鐘時間為:" + min + " : " + max,Toast.LENGTH_SHORT).show();  }}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漯河市| 盐边县| 寿光市| 瑞金市| 清镇市| 金川县| 宜兰县| 富民县| 广德县| 丰台区| 巨鹿县| 漠河县| 东港市| 喀什市| 盐津县| 珲春市| 南陵县| 包头市| 扬州市| 左贡县| 江永县| 化隆| 池州市| 永福县| 哈巴河县| 乳山市| 都兰县| 久治县| 遂川县| 禹城市| 阳谷县| 盐城市| 师宗县| 滨州市| 府谷县| 永定县| 巢湖市| 武穴市| 儋州市| 资阳市| 将乐县|