2012-11-30 42 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

あなたのソリューションは素晴らしいです!ありがとうございます。しかし、私には1つの質問があります。「String nums []」があるとき、どうやってプログラマティックな価値を設定できますか? –

+0

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

0

もう1つの解決策は、に2つの数値フィールドを1つのコンポーネントに結合することです。 Money Picker Exampleを参照してください。その利点は、スピナーに可能なすべての倍精度値を初期化する必要がないことです。

またjava.lang.String.format()機能を使用して、"00""0123"または他の同様をフォーマット特別な数字を添付する必要があるかもしれません。例は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. カスタムダブルピッカークラスを追加します。あなたの活動のXMLで

    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. を使用して、新しいダブルピッカーコンポーネント:

    <com.example.DoublePicker 
        android:id ="@+id/double_picker" 
        android:layout_width ="match_parent" 
        android:layout_height ="wrap_content" /> 
    
関連する問題