带有Formatter的Android NumberPicker不会在首次渲染时进行格式化 [英] Android NumberPicker with Formatter doesn't format on first rendering

查看:302
本文介绍了带有Formatter的Android NumberPicker不会在首次渲染时进行格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NumberPicker,它具有一个格式化程序,该格式化程序可在NumberPicker旋转或手动输入值时对显示的数字进行格式化.这可以正常工作,但是当首次显示NumberPicker并用setValue(0)对其进行初始化时,不会格式化0(它应显示为-"而不是0).从那一刻起,我旋转NumberPicker就可以了.

I have a NumberPicker that has a formatter that formats the displayed numbers either when the NumberPicker spins or when a value is entered manually. This works fine, but when the NumberPicker is first shown and I initialize it with setValue(0) the 0 does not get formatted (it should display as "-" instead of 0). As soon as I spin the NumberPicker from that point on everything works.

如何强制NumberPicker始终设置格式-在第一次渲染时以及在我使用键盘手动输入数字时都是如此?

How can I force the NumberPicker to format always - Both on first rendering and also when I enter a number manually with the keyboard?

这是我的格式化程序

public class PickerFormatter implements Formatter {

 private String mSingle;
 private String mMultiple;

 public PickerFormatter(String single, String multiple) {
    mSingle = single;
    mMultiple = multiple;
 }

 @Override
 public String format(int num) {
    if (num == 0) {
        return "-";
    }
    if (num == 1) {
        return num + " " + mSingle;
    }
    return num + " " + mMultiple;
 }

}

我使用setFormatter()将格式化程序添加到选择器中,这就是我对选择器所做的全部操作.

I add my formatter to the picker with setFormatter(), this is all I do to the picker.

    picker.setMaxValue(max);
    picker.setMinValue(min);
    picker.setFormatter(new PickerFormatter(single, multiple));
    picker.setWrapSelectorWheel(wrap);

推荐答案

我也遇到了这个烦人的小错误.使用了此答案中的一种技术,得出了一个令人讨厌但有效的解决方法.

I also encountered this annoying little bug. Used a technique from this answer to come up with a nasty but effective fix.

NumberPicker picker = (NumberPicker)view.findViewById(id.picker);
picker.setMinValue(1);
picker.setMaxValue(5);
picker.setWrapSelectorWheel(false);
picker.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int value) {
        return my_formatter(value);
    }
});

try {
    Method method = picker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
    method.setAccessible(true);
    method.invoke(picker, true);
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

实例化我的数字选择器后立即调用该私有changeValueByOne方法似乎足以使格式化程序正常运行.数字选择器显示得很好并且干净,第一个值的格式正确.就像我说的那样,讨厌但有效.

Calling that private changeValueByOne method immediately after instantiating my number picker seems to kick the formatter enough to behave how it should. The number picker comes up nice and clean with the first value formatted correctly. Like I said, nasty but effective.

这篇关于带有Formatter的Android NumberPicker不会在首次渲染时进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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