1. 程式人生 > >Android-滑動數值選擇器NumberPicker

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();
    }
}