如何在Android中的NumberPicker中自定义数字的值? [英] How to customize value for numbers in NumberPicker in android?

查看:470
本文介绍了如何在Android中的NumberPicker中自定义数字的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a number picker for setting a data limit in MB. right now, I have numberPicker, contains numeric values in sequence like [1,2,3,....., 2000 MB].

但是我想要一个数字选择器,其中应该包含[100,200,300,....,2000MB]之类的数值. 我该怎么办?

But I want a numberPicker that should contain numeric values like [100,200,300,...., 2000MB]. How can I do this?

推荐答案

为选择器显示的数组值

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:

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

这篇关于如何在Android中的NumberPicker中自定义数字的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆