Android SeekBar set progress value

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

I want the seekbar progress to change whenever i click a button.Using setProgress() works only for initial value, it throws error if i use after some changes.

回答1:

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


回答2:

You can also use :

mSeekBar.refreshDrawableState(); 

after you set progress .



回答3:

You need to call setOnSeekbarChangeListener() on your seekbar object and let for example your activity implement OnSeekbarChangeListener() and in that function you can do whatever you want.



回答4:

The solution that works for me:

mSeekbaBar.setProgress(progress); mSeekbaBar.post(new Runnable() {             @Override             public void run() {                 mSeekbaBar.setProgress(progress);             }         }); 

It removes the blinking of the thumb



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