2017-10-17 39 views

回答

0

一個簡單onCreate方法1 ...

@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>