how to limit seekbar

前端 未结 6 594
别那么骄傲
别那么骄傲 2020-12-29 02:10

I\'d like to set max and minimum limits of SeekBar to 50 and 20 respectively.

SeekBar has a direct option top provide max value, but how to set its

6条回答
  •  春和景丽
    2020-12-29 02:57

    The simplest way to do this is to just limit it in the SeekBar.OnSeekBarChangeListener() for your SeekBar.

    First, you need to create field with min or default value. Then set this value when creating SeekBar:

    private int fillDefault;
    

    In onCreateView() of a Fragment or onCreate of an Activity:

    fillDefault = 20;
    fillSeekBar.setProgress(fillDefault);
    

    Second, implement the listener for it:

    fillSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // Notification that the progress level has changed.
                if (progress < fillDefault){
                    seekBar.setProgress(fillDefault); // magic solution, ha
                }
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // Notification that the user has started a touch gesture.
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // Notification that the user has finished a touch gesture.
            }
        });
    

    It works. Simple and awesome:D

提交回复
热议问题