【问题标题】:Android numberpicker negative valuesAndroid numberpicker负值
【发布时间】:2014-01-24 22:54:29
【问题描述】:

我有一个包含负值和正值的数组,但是当我启动应用程序时,我只能在选择器中看到最小值。

int min = -25;
int max = 100;
int jumpBy = 5;

        String [] refl_Str = new String[((max-min)/jumpBy)+1];


        for (int i = 0; i < refl_Str.length; i++) 
        {
            refl_Str[i] = Integer.toString(min+(i*jumpBy));


        }

        np.setDisplayedValues(refl_Str);

当我添加时:

np.setMaxValue(100);
np.setMinValue(-25);

我在数字选择器中收到负值错误。

【问题讨论】:

标签: android numberpicker


【解决方案1】:

您可以使用以下代码来做到这一点:

final int minValue = -25
final int maxValue = 100
NumberPicker numberPicker = new NumberPicker(myActivity);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(maxValue - minValue);
numberPicker.setValue(myCurrentValue - minValue);
numberPicker.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int index) {
        return Integer.toString(index - minValue);
    }
});

然后取回选中的值:

int myNewValue = numberPicker.getValue() + minValue

【讨论】:

  • 跳5个呢?
  • 这只是一个示例,你必须将MinValue设置为0并将MaxValue设置为maxValue - minValue,其他的你可以像你的代码一样处理
  • 在 Nexus5 和 Lollipop 上,这不起作用。只要我移动选择轮,-minvalue 的数字就会跳跃。
  • 正如@ARLabs 提到的,现在 NumberPicker 在滚动时将当前值解析为文本,并将值设置为解析后的值,而不是我们想要的 myCurrentValue - minValue 类型的值。
猜你喜欢
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 2013-05-11
  • 2015-02-05
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多