6 android android-layout android-activity
我创建了一个自定义数字选择器,并使用布局inflater添加到布局中.我想为数字选择器2,5,10设置三个值.通过使用setMinValue(1),setMaxValue(3),它只设置1到3个默认数.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) RelativeLayout.LayoutParams.WRAP_CONTENT, (int) RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
np.setLayoutParams(params);
np.setMinValue(1);
np.setMaxValue(3);
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
np.setValue(2);
Run Code Online (Sandbox Code Playgroud)
而不是设置1到3,我想设置2,5,10.任何人都可以告诉我如何在数字选择器中设置自定义值
提前致谢 :)
感谢Mustanser指向正确的方向.重要的是要注意setDisplayedValues改为使用String数组,因此功能性答案是:
NumberPicker picker = new NumberPicker(this);
picker.setMinValue(1);
picker.setMaxValue(3);
picker.setDisplayedValues( new String[] { "2", "5", "10" } );
Run Code Online (Sandbox Code Playgroud)
此外,确保正确解释picker.getValue()的结果非常重要,因为基础int值仍然从1到3运行:
int pick;
switch (picker.getValue()) {
case 1:
pick = 2;
break;
case 2:
pick = 5;
break;
case 3:
pick = 10;
break;
default:
//...
}
Run Code Online (Sandbox Code Playgroud)
试试这样的事情,希望它会对你有所帮助
NumberPicker picker = new NumberPicker(this);
picker.setMinValue(1);
picker.setMaxValue(3);
picker.setDisplayedValues( new int[] { 2, 5, 10 } );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4544 次 |
| 最近记录: |