How to get value of the NumberPicker instance from

2019-06-07 05:10发布

问题:

I have a NumberPicker instance and it works as expected but I need to get value form it when scrolling stops Or when the user only press increments / decrements Button is pressed. the value that i should get it are displayed in the text view.

i managed to get the value when scrolling stops/ became idle and displayed correctly(this done in onScrollChanged() listener).

but the onScrollChanged() listener won't be fired when user press on increments or decrements buttons and if I use onValueChanged() listener the text updated but scrolling became very laggy and slow( as result of updating all the time when value changed).

So I need help how to display the value of NumberPicker all the time without causing lagging or slowing of NumberPicker scrolling.

Code snipped as follow:

    NumberPicker milisecoundsNumberPicker = (NumberPicker) findViewById(R.id.numberPicker);
    milisecNumberPicker.setFocusable(false);
    milisecNumberPicker.setFocusableInTouchMode(false);
    TextView textNumber = (TextView) findViewById(R.id.selectednum);



    milisecNumberPicker.setMaxValue(10);
    milisecNumberPicker.setMinValue(0);
    milisecNumberPicker.setWrapSelectorWheel(false);
    String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
    milisecNumberPicker.setDisplayedValues(nums);// array of numbers
    milisecNumberPicker.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChange(NumberPicker view, int scrollState) {

            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                    || scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                view.getValue();

                textNumber.setText(view.getValue() + "");
 //this is ok no lagging but will not work on pressing increments and decrements buttons    
            }

        }
    }); 
// If I use this
    milisecNumberPicker.setOnValueChangedListener(new OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 
                    // set Text value here causing lagging on scrolling                                                             
        }
    });

Sorry bout bad formatting I'm new in this but can any one help me in this topic.

回答1:

HI

import android.os.Handler;

...............

milisecoundsNumberPicker.setOnValueChangedListener(new android.widget.NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(android.widget.NumberPicker picker, final int oldVal, final int newVal) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    if (newVal == milisecoundsNumberPicker.getValue()) {
                        textNumber.setText(newVal + "");
                    }
                }
            }, 500);//set time 
        }
    });