Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

android - How to create a negative NumberPicker

Does anyone know of an easy way to allow negative numbers with Android's default numberpicker? I understand that it's the InputFilter that disallows this, but is there any easy way to override without rewriting the whole widget?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A more generic and elegant solution is to use NumberPicker.Formatter and use only positive numbers in the NumberPicker.

Example if I want to select a number in [-50, 50]:

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);
    }
});

then to get back the selected value:

int myNewValue = numberPicker.getValue() + minValue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...