溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中滑動(dòng)數(shù)值選擇器NumberPicker的用法分析

發(fā)布時(shí)間:2020-08-10 09:50:42 來(lái)源:億速云 閱讀:540 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)Android中滑動(dòng)數(shù)值選擇器NumberPicker的用法分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

簡(jiǎn)介:

NumberPicker: 用戶既可以從鍵盤輸值,也可以拖動(dòng)來(lái)選擇值

實(shí)際效果:

Android中滑動(dòng)數(shù)值選擇器NumberPicker的用法分析

常用方法:

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

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

3. setValue() 設(shè)置該組件的當(dāng)前值

在布局文件中調(diào)用:

<&#63;xml version="1.0" encoding="utf-8" &#63;>
<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)聽事件:

1. setOnValueChangedListener 調(diào)用監(jiān)聽事件

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中滑動(dòng)數(shù)值選擇器NumberPicker的用法分析就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI