How to set seekbar min and max value

前端 未结 14 825
栀梦
栀梦 2020-11-28 09:17

I have a seekbar and trying to set the value from 60 to 180 for one and 40 to 190 for the second one in step of 1.

sb1 = (SeekBar) findViewById(R.id.progress         


        
相关标签:
14条回答
  • 2020-11-28 09:34

    There is no option to set a min or max value in seekbar , so you can use a formula here to scale your value.

    Desired_value = ( progress * ( Max_value - Min_value) / 100 ) + Min_value
    

    I have tested this formula in many examples. In your example, if the progressBar is the middle(i.e. progress = 50 ) and your Min_val and Max_val are 60 and 180 respectively, then this formula will give you the Desired_value '120'.

    0 讨论(0)
  • 2020-11-28 09:35
        private static final int MIN_METERS = 100;
        private static final int JUMP_BY = 50;
    
        metersText.setText(meters+"");
        metersBar.setProgress((meters-MIN_METERS));
        metersBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
                progress = progress + MIN_METERS;
                progress = progress / JUMP_BY;
                progress = progress * JUMP_BY;
                metersText.setText((progress)+"");
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-28 09:37

    If you are using the AndroidX libraries (import androidx.preference.*), this functionality exists without any hacky workarounds!

        val seekbar = findPreference("your_seekbar") as SeekBarPreference
        seekbar.min = 1
        seekbar.max = 10
        seekbar.seekBarIncrement = 1
    
    0 讨论(0)
  • 2020-11-28 09:40
    seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
    
                    int MIN = 5;
                    if (progress < MIN) {
    
                        value.setText(" Time Interval (" + seektime + " sec)");
                    } else {
                        seektime = progress;
                    }
                    value.setText(" Time Interval (" + seektime + " sec)");
    
                }
            });
    
    0 讨论(0)
  • 2020-11-28 09:44

    For requirements like this I have created Utility to customize Seekbar progress like below code:

    SeekBarUtil.class

       import android.widget.SeekBar;
    import android.widget.TextView;
    
    public class SeekBarUtil {
    
        public static void setSeekBar(SeekBar mSeekbar, int minVal, int maxVal, int intervalVal, final TextView mTextView, String startPrefix, String endSuffix) {
    
            int totalCount = (maxVal - minVal) / intervalVal;
            mSeekbar.setMax(totalCount);
            mSeekbar.setOnSeekBarChangeListener(new CustomSeekBarListener(minVal, maxVal, intervalVal) {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    //progress = ((int)Math.round(progress/interval))*interval;
                    int val = min;
                    if (interval == totalCount) {
                        val = max;
                    } else {
                        val = min + (progress * interval);
                    }
                    seekBar.setProgress(progress);
                    mTextView.setText(startPrefix + val + endSuffix);
                }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {  }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {   }
            });
    
        }
    }
    

    and CustomSeekBarListener.class

    import android.widget.SeekBar;
    
    class CustomSeekBarListener implements SeekBar.OnSeekBarChangeListener {
        int min=0,max=0,interval=1;
        int totalCount;
        public CustomSeekBarListener(int min, int max, int interval) {
            this.min = min;
            this.max = max;
            this.interval = interval;
            totalCount= (max - min) / interval;
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) { }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) { }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) { }
    }
    

    and you can use it like below code snippet

    SeekBarUtil.setSeekBar(seekbarAmountNeeded,10000,200000,5000,textAmount,"$"," PA");
    
    0 讨论(0)
  • 2020-11-28 09:44

    Copy this class and use custom Seek Bar :

    public class MinMaxSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
    private OnMinMaxSeekBarChangeListener onMinMaxSeekBarChangeListener = null;
    private int intMaxValue = 100;
    private int intPrgress = 0;
    private int minPrgress = 0;
    
    
    public int getIntMaxValue() {
        return intMaxValue;
    }
    
    public void setIntMaxValue(int intMaxValue) {
        this.intMaxValue = intMaxValue;
        int middle = getMiddle(intMaxValue, minPrgress);
        super.setMax(middle);
    }
    
    public int getIntPrgress() {
        return intPrgress;
    }
    
    public void setIntPrgress(int intPrgress) {
        this.intPrgress = intPrgress;
    }
    
    public int getMinPrgress() {
        return minPrgress;
    }
    
    public void setMinPrgress(int minPrgress) {
        this.minPrgress = minPrgress;
        int middle = getMiddle(intMaxValue, minPrgress);
        super.setMax(middle);
    }
    
    private int getMiddle(int floatMaxValue, int minPrgress) {
        int v = floatMaxValue - minPrgress;
        return v;
    }
    
    public MinMaxSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnSeekBarChangeListener(this);
    }
    
    public MinMaxSeekBar(Context context) {
        super(context);
        this.setOnSeekBarChangeListener(this);
    }
    
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        intPrgress = minPrgress + i;
        onMinMaxSeekBarChangeListener.onMinMaxSeekProgressChanged(seekBar, intPrgress, b);
    }
    
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        onMinMaxSeekBarChangeListener.onStartTrackingTouch(seekBar);
    
    }
    
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        onMinMaxSeekBarChangeListener.onStopTrackingTouch(seekBar);
    }
    
    public static interface OnMinMaxSeekBarChangeListener {
        public void onMinMaxSeekProgressChanged(SeekBar seekBar, int i, boolean b);
    
        public void onStartTrackingTouch(SeekBar seekBar);
    
        public void onStopTrackingTouch(SeekBar seekBar);
    }
    
    public void setOnIntegerSeekBarChangeListener(OnMinMaxSeekBarChangeListener floatListener) {
        this.onMinMaxSeekBarChangeListener = floatListener;
    }
    }
    

    This class contin method public void setMin(int minPrgress) for setting minimum value of Seek Bar This class contin method public void setMax(int maxPrgress) for setting maximum value of Seek Bar

    0 讨论(0)
提交回复
热议问题