How do you display a 2 digit NumberPicker in android?

感情迁移 提交于 2019-12-14 03:40:28

问题


I want to create a timer and since I couldn't create a digital interface that is editable for the user to set the time I want to use the NumberPicker. However the NumberPicker only displays 1 digit for the numbers between 0-9. How do you format the picker so that it will display two digits such as 01 02 03 and so forth.


回答1:


    numberPicker.setMaxValue(10);
    numberPicker.setMinValue(0);
    numberPicker.setFormatter(new NumberPicker.Formatter() {
        @Override
        public String format(int i) {
            return String.format("%02d", i);
        }
    });

This will do the trick!




回答2:


Implement a custom NumberPicker.Formatter that implements your value display padding and call setFormatter.




回答3:


 NumberPicker monthPicker = (NumberPicker) view.findViewById(R.id.np_month);
        monthPicker.setMinValue(1);
        monthPicker.setMaxValue(12);
        monthPicker.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int i) {
                return String.format("%02d", i);
            }
        });


来源:https://stackoverflow.com/questions/19737755/how-do-you-display-a-2-digit-numberpicker-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!