为数字选择器设置监听器,当选中位置为2时,将其改为1或3。代码示例:
NumberPicker numberPicker = findViewById(R.id.number_picker);
numberPicker.setMinValue(1);
numberPicker.setMaxValue(3);
numberPicker.setWrapSelectorWheel(false); // 禁用滚轮的循环效果
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal == 2) {
numberPicker.setValue(oldVal == 1 ? 3 : 1);
}
}
});