Android SeekBar set progress value

后端 未结 6 972
旧时难觅i
旧时难觅i 2020-12-06 16:22

I want the SeekBar progress to change whenever I click a button. Using setProgress() works only for initial value. It throws an error if I use it

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 16:28

    Perhaps you should try to use a handler? I use this in an app of mine and works fine.

    1) When creating your SeekBar:

    // ...
    seekBarHandler = new Handler(); // must be created in the same thread that created the SeekBar
    seekBar = (SeekBar) findViewById(R.id.my_seekbar);
    // you should define max in xml, but if you need to do this by code, you must set max as 0 and then your desired value. this is because a bug in SeekBar (issue 12945) (don't really checked if it was corrected)
    seekBar.setMax(0);
    seekBar.setMax(max);
    seekBar.setProgress(progress);
    // ...
    

    2) When your button is clicked

    // ...
    seekBarHandler.post(new Runnable() {
        @Override
        public void run() {
            if (seekBar != null) {
                seekBar.setMax(0);
                seekBar.setMax(max);
                seekBar.setProgress(newProgress);
            }
        }
    });
    // ...
    

提交回复
热议问题