How to get value of the NumberPicker instance from onValueChange when The NumberPicker not scrolling only(pressing plus or minus buttons) in Android

烈酒焚心 提交于 2019-12-08 03:24:45

问题


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


来源:https://stackoverflow.com/questions/35675682/how-to-get-value-of-the-numberpicker-instance-from-onvaluechange-when-the-number

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