How to set seekbar min and max value

前端 未结 14 899
栀梦
栀梦 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:54

    The easiest way to set a min and max value to a seekbar for me: if you want values min=60 to max=180, this is equal to min=0 max=120. So in your seekbar xml set property:

    android:max="120"
    

    min will be always 0.

    Now you only need to do what your are doing, add the amount to get your translated value in any change, in this case +60.

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int translatedProgress = progress + 60;
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    

    Be careful with the seekbar property android:progress, if you change the range you must recalculate your initial progress. If you want 50%, max/2, in my example 120/2 = 60;

提交回复
热议问题