數值選擇器用於讓用戶輸入數值,用戶既可以通過鍵盤輸入數值,也可以通過拖動來選擇數值。使用該組件常用如下三個方法。
- setMinValue(int minVal):設置該組件支持的最小值。
- setMaxValue(int maxVal):設置該組件支持的最大值。
- setValue(int value):設置該組件的當前值。
下面通過一個實例來介紹NumberPicker的功能與用法。
實例:選擇您意向的價格范圍
在該實例中,程序將使用兩個NumberPicker來讓用戶選擇價格,第一個NumberPicker用於選擇低價,第二個NumberPicker用於選擇高價。下面是該實例的布局文件。
布局文件如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:text="選擇低價:" android:layout_width="120dp" android:layout_height="wrap_content"/> <NumberPicker android:id="@+id/np1" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true"/> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="選擇高價:" android:layout_width="120dp" android:layout_height="wrap_content"/> <NumberPicker android:id="@+id/np2" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true"/> </TableRow> </TableLayout>
上面的布局文件中定義了兩個NumberPicker,接下來Activity代碼需要為這兩個NumberPicker設置最小值、最大值,並為他們綁定事件監聽器。下面是該Activity的后台代碼。
package org.crazyit.helloworld; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.NumberPicker; import android.widget.NumberPicker.OnValueChangeListener; import android.widget.Toast; public class NumberPickerTest extends Activity { NumberPicker np1,np2; //定義最低價格、最高價格的初始值 int minPrice=25,maxPrice=75; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.number_picker_test); np1=(NumberPicker)findViewById(R.id.np1); //設置np1的最小值和最大值 np1.setMinValue(10); np1.setMaxValue(50); //設置np1的當前值 np1.setValue(minPrice); np1.setOnValueChangedListener(new OnValueChangeListener(){ @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { // TODO Auto-generated method stub minPrice=newVal; showSelectedPrice(); } }); np2=(NumberPicker)findViewById(R.id.np2); //設置np2的最小值和最大值 np2.setMinValue(60); np2.setMaxValue(100); //設置np2的當前值 np2.setValue(maxPrice); np2.setOnValueChangedListener(new OnValueChangeListener(){ @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { // TODO Auto-generated method stub maxPrice=newVal; showSelectedPrice(); } }); } private void showSelectedPrice() { Toast.makeText(this, "您選擇的價格為:"+minPrice+",最高價格為:"+maxPrice , Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.number_picker_test, menu); return true; } }
上面兩段粗體字代碼的控制邏輯基本是相似的,它們都調用了NumberPicker的setMinValue()、setMaxValue()來設置該數值選擇器的最小值、最大值和當前值。除此之外,程序還為兩個日期選擇器綁定了事件監聽器:當它們的值發生改變時,將會激發相應的事件處理方法。
運行該程序,並通過NumberPicker選擇數值,將可以看到如下效果: