2013-11-24 11 views
5

Ho due selettori di numeri, voglio ottenere il valore che l'utente ha scelto tra i selezionatori di numeri. quindi convertirli in stringa.Come ottenere il numero selezionato da NumberPicker?

Qualche idea?

+0

Alcuni documenti su NumberPicker per Android. http://developer.android.com/reference/android/widget/NumberPicker.html Inoltre, questo potrebbe aiutare: http://stackoverflow.com/questions/9279785/how-to-use-numberpicker-widget-android-4 -0 – Radnyx

risposta

6

È possibile ottenere il numero selezionato corrente chiamando getValue(), ad es. se avete myPicker, si può fare questo:

String value = "" + myPicker.getValue(); 

Se si desidera ottenere il valore quando è selezionato dall'utente, è necessario implementare l'interfaccia NumberPicker.OnValueChangeListener:

private class MyListener implements NumberPicker.OnValueChangeListener { 
    @Override 
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 
     //get new value and convert it to String 
     //if you want to use variable value elsewhere, declare it as a field 
     //of your main function 
     String value = "" + newVal; 
    } 
} 

Ricordate di impostare le ascoltatore, ad esempio:

myPicker.setOnValueChangedListener(new MyListener()); 
Problemi correlati