android - 具有负值和正值的 NumberPicker

标签 android

<分区>

我正在尝试为我的 NumberPicker 使用一组数字,其中包括 (-50 -> 50),NumberPicker 的 MinValue/MaxValue 设置为 0/100,然后将默认起点设置为 50 ( 0), any idea why it displays the numbers properly except for -1 through -5... when not selected they display fine... when selected they display as -19, -29, -39, -49, -50.

我已经测试了我提供给它的 int 数组,其中的所有值都很好。

这是我用来填充 NumberPicker 的代码 fragment ...

String[] currentScoreArray = new String[100];
final NumberPicker currentScore = (NumberPicker) findViewById(R.id.currentScore);
for (int i = 0; i <= 100; i++)
    currentScoreArray[i] = Integer.toString(i - 50);
currentScore.setMaxValue(100);
currentScore.setMinValue(0);
currentScore.setWrapSelectorWheel(false);
currentScore.setDisplayedValues(currentScoreArray);
currentScore.setValue(50);

在与 baba tenor 交谈后,我仍然无法解决这个问题,我制作了一个关于该问题的 YouTube 视频,并将代码和 logcat 包含在视频评论的链接中。

http://www.youtube.com/watch?v=bF39THktVY4

最佳答案

老问题,但我为其他用户发布了一个想法:

也许你可以使用 NumberPicker.Formatter .

例子:

final int minValue = -50
final int maxValue = 50
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

关于android - 具有负值和正值的 NumberPicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11303100/

相关文章:

android - node js和appium有什么关系?如何在两个不同的端口上运行到多个 appium 服务器实例?

android - Android Project如何具有不同的构建配置?

android admob 关闭了屏幕,但在 ScrollView 中隐藏了其他按钮

Android facebook sdk 第二次登录问题

android - 是否可以通过禁用它们的包装布局来禁用所有小部件?

java - 在“选项”菜单上切换 Wifi 状态

java - 在单独的 WebView 上进行多次登录? (安卓)

android - 在现有 View 上显示透明 View

java - 如何获得物理世界的精确高度和宽度(以米为单位)?

android - WakeLock under-locked exception 使用 cwac-locpoll