答案 0 :(得分:0)
一种简单的onCreate
方法......
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.something);
final TextView tv = (TextView) findViewById(R.id.tv);
NumberPicker np = (NumberPicker) findViewById(R.id.np);
final String[] values = {"1", "0"};
np.setMinValue(0);
np.setMaxValue(values.length - 1);
np.setDisplayedValues(values);
np.setWrapSelectorWheel(true);
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
String result = "Currently selected value:" + values[newVal];
tv.setText(result);
}
});
}
...还有一个简单的something.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="@+id/np"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/np" />
</RelativeLayout>