是否可以将NumberPicker的增减间隔设置为其他值?例如到 50(50、100、150、200、250 等)?还是需要一些定制/修改?
最佳答案
我认为你可以这样做:
初始化 displayed array values对于您的选择器:
int NUMBER_OF_VALUES = 10; //num of values in the picker int PICKER_RANGE = 50; ... 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 = {"50", "100", "150", .....}; */
将显示的数组设置为您的选择器:
/* NOTE: Your picker range has to be [0, displayedValues.size()-1] to be consistent with the array index (displayedValues index). */ //ToDo: Initialize the picker first. 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("150") ) numPicker.setValue(i);
最后,有一个很棒的易于定制的小部件,叫做 android-wheel .您可以查看并根据需要使用它。
关于android - 设置NumberPicker的增减间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925681/