2012-11-30 13 views
7

L'utente della nostra app dovrebbe essere in grado di regolare un numero in virgola mobile. Al momento, ho riempito un ArrayAdapter con tutti i valori possibili e l'ho allegato a uno spinner.numberpicker android per numeri in virgola mobile

Questa soluzione non soddisfa le nostre aspettative, dal momento che la casella di selezione a discesa è troppo alta. C'è un modo migliore? Stavo guardando Numberpicker - ma questo sembra funzionare solo con valori Integer.

Qualche idea?

Grazie!

+0

Lasciatemi suggerire un'altra soluzione [DecimalPicker] (https://stackoverflow.com/a/46047964/753575) – isabsent

risposta

17

NumberPicker non è solo per gli interi .. Anche è possibile utilizzare FloatsString ecc

vedere this e leggere su di esso.

di tutorial:

E io avevo usato NumberPicker molto tempo fa in questo modo e che potrebbe essere un certo uso distacco qui:

NumberPicker np; 
    String nums[]= {"Select Fraction","1/64","1/32","3/64","1/16","5/64","3/32","7/64","1/8","9/64","5/32","11/64","3/16","13/64","7/32","15/64","1/4","17/64","9/32","19/64","5/16","21/64","11/32","23/64","3/8","25/64","13/32","27/64","7/16","29/64"}; 

      np = (NumberPicker) findViewById(R.id.np); 

      np.setMaxValue(nums.length-1); 
      np.setMinValue(0); 
      np.setWrapSelectorWheel(false); 
      np.setDisplayedValues(nums); 
      np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 

Si può fare ArrayList di qualsiasi tipo di dati e assegnarlo.

+0

è uno scherzo? – user544262772

+0

La tua soluzione è fantastica! Grazie, ma ho una domanda: come posso impostare il valore programmaticamente, quando ho 'String nums []'? –

+0

Vedere anche https://stackoverflow.com/a/13590660/1112963 – Zon

0

Un'altra soluzione è quella di combinare due campi numerici in un unico componente. Vedi lo Money Picker Example. Il suo vantaggio è che non devi inizializzare tutti i possibili valori doppi per il tuo spinner.

Potrebbe anche essere necessario allegare cifre speciali formattazione, come "00", "0123" o altro, utilizzando la funzione java.lang.String.format(). L'esempio è here.

  1. Aggiungi doppio Picker xml-schema:

    <LinearLayout 
        xmlns:android = "http://schemas.android.com/apk/res/android" 
        android:layout_width = "fill_parent" 
        android:layout_height = "wrap_content" 
        android:gravity = "center"> 
    
        <NumberPicker 
        android:id = "@+id/integer_picker" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:layout_gravity = "center"/> 
    
        <TextView 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:layout_gravity = "center" 
        android:padding = "5dp" 
        android:text = "@string/local_separator" 
        android:textSize = "20sp"/> 
    
        <NumberPicker 
        android:id = "@+id/fraction_picker" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:layout_gravity = "center"/> 
    
        <TextView 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:layout_gravity = "center" 
        android:padding = "5dp" 
        android:text = "@string/measure_unit_sign" 
        android:textSize = "20sp"/> 
    
    </LinearLayout> 
    
  2. Aggiungi doppio personalizzato classe Picker:

    import android.content.Context; 
    import android.view.LayoutInflater; 
    import android.widget.NumberPicker; 
    
    /** 
    * UI component for double (floating point) values. Can be used 
    * for money and other measures. 
    */ 
    public class DoublePicker 
        extends NumberPicker { 
    
        private int decimals; 
        private NumberPicker integerPicker; 
        private NumberPicker fractionPicker; 
    
        public DoublePicker(
        Context context) { 
    
        super(
         context); 
    
         LayoutInflater 
         .from(
          context) 
         .inflate(
          R.layout.double_picker, 
          this); 
    
         integerPicker = 
         (NumberPicker) findViewById(
          R.id.integer_picker); 
    
         fractionPicker = 
         (NumberPicker) findViewById(
          R.id.fraction_picker); 
        } 
    
        public int getDecimals() { 
    
        return decimals; 
        } 
    
        /** 
        * Sets the amount of digits after separator. 
        * 
        * @param decimals Anount of decimals. 
        */ 
        public void setDecimals(final int decimals) { 
    
        this.decimals = decimals; 
    
        this.setFormatter(new DoublePicker.Formatter() { 
    
         @Override 
         public String format(int i) { 
    
         return String.format(
          "%." + decimals + "f", 
          i); 
         } 
        }); 
        } 
    
        public void setValue(double value) { 
    
        integerPicker.setValue((int) value); 
    
        fractionPicker.setValue(
         Integer.valueOf(
         String 
          .valueOf(value) 
          .split(".") 
          [1])); 
        } 
    } 
    
  3. Usa nuovo componente Doppia Picker nella vostra attività xml:

    <com.example.DoublePicker 
        android:id ="@+id/double_picker" 
        android:layout_width ="match_parent" 
        android:layout_height ="wrap_content" /> 
    
Problemi correlati