2012-11-30 65 views
7

我們的應用程序的用戶應該能夠調整浮點數。此刻,我用一個ArrayAdapter填充了所有可能的值並將其附加到一個微調器上。android numberpicker浮點數

該解決方案並不能真正滿足我們的期望,因爲微調框下拉框太高。有沒有更好的辦法?我在看Numberpicker - 但這似乎只適用於整數值。

任何想法?

謝謝!

+0

讓我建議另一個解決方案[DecimalPicker](https://stackoverflow.com/a/46047964/753575) – isabsent

回答

17

NumberPicker不僅僅是爲整數。即使你可以用FloatsString

看到this和了解它。

的教程:

而且我用了NumberPicker早就喜歡這一點,它可能是一些用在這裏發帖:

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); 

可以使ArrayList任何數據類型並分配它。

+0

這是一個玩笑嗎? – user544262772

+0

您的解決方案非常棒!謝謝,但我有一個問題:當我有'String nums []'時,我怎麼能設置值programmaticaly? –

+0

另請參閱https://stackoverflow.com/a/13590660/1112963 – Zon

0

另一種解決方案是將兩個號碼字段組合成一個組件。請參閱Money Picker Example。它的優點是你不必爲你的微調器初始化所有可能的double值。

您可能還需要附加特殊數字格式,像「00」「0123」或其他使用java.lang.String.format()功能。示例是here

  1. 將雙選擇器的XML佈局:

    <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. 添加自定義雙重選擇器類:

    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. 使用新的雙重選擇器組件在活動的xml:

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