2012-11-30 4 views
7

우리 사용자는 부동 소수점 숫자를 조정할 수 있어야합니다. 지금은 가능한 모든 값을 ArrayAdapter에 채우고 스피너에 연결했습니다.부동 소수점 숫자에 대한 android numberpicker

이 솔루션은 스피너 드롭 다운 상자가 너무 길기 때문에 우리의 기대치를 실제로 충족시키지 못합니다. 더 좋은 방법이 있습니까? Numberpicker를 보았습니다. 그러나 이것은 Integer 값으로 만 작동하는 것 같습니다.

아이디어가 있으십니까?

감사합니다.

+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 []가있을 때 값을 프로그래밍 방식으로 어떻게 설정할 수 있습니까? –

+0

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

0

은 두 개의 숫자 필드을 하나의 구성 요소로 결합합니다. 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. 추가 사용자 정의 더블 선택기 클래스 :

    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" /> 
    
관련 문제