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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…