NumberPicker
NumberPicker 的使用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<NumberPicker android:id="@+id/np"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
主程式
public class NumberPickerExampleActivity extends Activity {
/** Called when the activity is first created. */
public NumberPicker np;
public int currentNumber;
public TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv);
np=(NumberPicker) findViewById(R.id.np);
np.setBackgroundColor(Color.YELLOW);
np.setMaxValue(50); //設定最大值
np.setMinValue(0); //設定最小值
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); //設定鍵盤不要出現
np.setValue(0); //設定初始值
//設定數值改變時的動作
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener (){
public void onValueChange(NumberPicker view, int oldValue, int newValue) {
currentNumber = np.getValue(); //得到 NumberPicker之數值
tv.setText(String.valueOf(currentNumber));
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<NumberPicker android:id="@+id/np"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
主程式
public class NumberPickerExampleActivity extends Activity {
/** Called when the activity is first created. */
public NumberPicker np;
public int currentNumber;
public TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv);
np=(NumberPicker) findViewById(R.id.np);
np.setBackgroundColor(Color.YELLOW);
np.setMaxValue(50); //設定最大值
np.setMinValue(0); //設定最小值
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); //設定鍵盤不要出現
np.setValue(0); //設定初始值
//設定數值改變時的動作
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener (){
public void onValueChange(NumberPicker view, int oldValue, int newValue) {
currentNumber = np.getValue(); //得到 NumberPicker之數值
tv.setText(String.valueOf(currentNumber));
}
});
}
}
留言
張貼留言