2014-01-07 27 views
1

我有一個具有負值和正值的數組,但是當我啓動應用程序時,我只能看到選取器中的最小值。Android數字選擇器負值

int min = -25; 
int max = 100; 
int jumpBy = 5; 

     String [] refl_Str = new String[((max-min)/jumpBy)+1]; 


     for (int i = 0; i < refl_Str.length; i++) 
     { 
      refl_Str[i] = Integer.toString(min+(i*jumpBy)); 


     } 

     np.setDisplayedValues(refl_Str); 

當我添加:

np.setMaxValue(100); 
np.setMinValue(-25); 

我就在numberpicker負值出現錯誤。

+0

的可能的複製[Android的NumberPicker - 負數(HTTP://計算器.com/questions/14357520/android-numberpicker-negative-numbers) – user276648

回答

2

你可以用下面的代碼來做到這一點:

final int minValue = -25 
final int maxValue = 100 
NumberPicker numberPicker = new NumberPicker(myActivity); 
numberPicker.setMinValue(0); 
numberPicker.setMaxValue(maxValue - minValue); 
numberPicker.setValue(myCurrentValue - minValue); 
numberPicker.setFormatter(new NumberPicker.Formatter() { 
    @Override 
    public String format(int index) { 
     return Integer.toString(index - minValue); 
    } 
}); 

然後取回所選擇的值:

int myNewValue = numberPicker.getValue() + minValue 
+0

跳過5是怎麼回事? – Dim

+0

這只是想要說的示例,您必須將MinValue設置爲'0'並將setMaxValue設置爲'maxValue - minValue',您可以像處理代碼一樣處理其他事情 –

+0

在Nexus5和Lollipop上,這不起作用。只是我移動選擇器輪,數字跳躍的最小值。 – ARLabs