Is it possible to change the increment of NumberPicker when long pressing?

坚强是说给别人听的谎言 提交于 2019-12-11 02:29:57

问题


I have range of 3900 numbers which the user use the NumberPicker to pick one single number from. Now, when long pressing up/down arrows in the picker, the numbers are increased/decreased slowly. I have sat the setOnLongPressUpdateInterval() to 0 milliseconds, but it still increment slowly in this large range of numbers.

Is it possible to change the increment in the picker when long pressing the up/down arrows so it increases numbers by lets say 100, and turns back to increment of 1 when releasing the buttons?


回答1:


I took this approach, since the work to trap the long press seemed worrisome.

NumberPicker _picker;
long _tsLastChange;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.number_picker);

    _picker = (NumberPicker) findViewById(R.id.picker);
    _picker.setMinValue(1);
    _picker.setMaxValue(3900);
    _picker.setOnLongPressUpdateInterval(100);

    _picker.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            Log.d("NumberPickerActivity", "value changed detected");
            long tsChange = System.currentTimeMillis();
            if (tsChange - _tsLastChange < 200)
            {
                if (newVal > oldVal)
                    _picker.setValue(newVal + 100);
                else 
                    _picker.setValue(newVal - 100);
            }
            _tsLastChange = tsChange;
        }
    });
}

To get this to work IRL, you would probably want to (a) round "newVal" to the nearest 100 or something, and (b) worry about wraparound cases more than I do.

Somewhat surprising to me is that the programmatic setValue does not seem to cause infinite recursion in onValueChangedListener.

I tested this on a 4.1 emulator using the "Theme.Black" theme, to get the "old fashioned" NumberPicker with +/- buttons.



来源:https://stackoverflow.com/questions/14095656/is-it-possible-to-change-the-increment-of-numberpicker-when-long-pressing

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