我有一个数字选择器,用于设置以MB为单位的数据限制。现在,我有numberPicker,它包含按顺序排列的数字值,例如[1,2,3,.....,2000 MB]。

但是我想要一个numberPicker,其中应该包含[100,200,300,....,2000MB]之类的数值。
我怎样才能做到这一点?

最佳答案

显示选择器的数组值

int NUMBER_OF_VALUES = 20; //num of values in the picker
int PICKER_RANGE = 100;
...
String[] displayedValues  = new String[NUMBER_OF_VALUES];
//Populate the array
for(int i=0; i<NUMBER_OF_VALUES; i++)
    displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1));
/* OR: if the array is easy to be hard-coded, then just hard-code it:
   String[] displayedValues = {"100", "200", "300", .....}; */


在选择器中设置arr:

numPicker.setMinValue(0);
numPicker.setMaxValue(displayedValues.size()-1);
numPicker.setDisplayedValues(displayedValues);


获取/设置选择器的值:

//To get the current value in the picker
choosenValue = displayedValues[numPicker.getValue()];
//To set a new value (let's say 150)
for( int i=0; i<displayedValues.length ; i++ )
    if( displayedValues[i].equals("300") )
         numPicker.setValue(i);

10-08 18:17