How to customize value for numbers in NumberPicker in android?

隐身守侯 提交于 2019-12-06 15:09:12
Saif

Displayed array values for your picker

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", .....}; */

Set arr in your picker :

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

get/set the value of the picker :

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

Try the code below

int start = 100;
        String[] numbers = new String[20];
        for(int i =0 ; i < 20 ; i++) {
            numbers[i] = start + "";
            start = start + 100;
        }
        NumberPicker numberPicker = (NumberPicker) findViewById(R.id.id_number_picker);
        numberPicker.setMaxValue(20);
        numberPicker.setMinValue(1);
        numberPicker.setDisplayedValues(numbers);
final String[] nums = new String[21];
        for(int i=0; i<nums.length; i++) {
            nums[i] = Integer.toString((i+1)*100);
        }

numberPicker.setMinValue(0);
numberPicker.setMinValue(19);
numberPicker.setDisplayedValues(nums);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!