Mar*_*tin 11 android progress-bar
我在将新Drawable设置为ProgressBar时遇到问题.
如果我在onCreate()方法中使用setProgressDrawable()它可以很好地工作.但是当我尝试在Handler post回调中调用相同的方法时,它不起作用,并且进度条消失了.
有人可以解释这种行为吗?我怎么解决这个问题?
per*_*rry 14
downloadingBar.setProgress(0);
Drawable progressDrawable = getResources().getDrawable(R.drawable.download_progressbar_pause_bg);
progressDrawable.setBounds(downloadingBar.getProgressDrawable().getBounds());
downloadingBar.setProgressDrawable(progressDrawable);
downloadingBar.setProgress(mCurrentPercent);
Run Code Online (Sandbox Code Playgroud)
我自己也遇到了这个问题,并且成功解决了:)
我用它AsyncTask来处理后台任务/线程,但这个想法应该与使用相同Runnable/Handler(尽管AsyncTask在我看来确实感觉更好)。
所以,这就是我所做的...放入方法setContentView(R.layout.my_screen);中onPostExecute!(即代替onCreate方法)
所以代码看起来像这样:
public class MyScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)
new MySpecialTask().execute();
}
private int somethingThatTakesALongTime() {
int result;
// blah blah blah
return result;
}
private void updateTheUiWithResult(int result) {
// Some code that changes the UI
// For exampe:
TextView myTextView = (TextView) findViewById(R.id.result_text);
myTextView.setText("Result is: " + result);
ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
anyProgressBar.setMax(100);
anyProgressBar.setProgress(result);
}
private class MySpecialTask extends AsyncTask<String, Void, Integer> {
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
}
@Override
protected Integer doInBackground(String... strings) {
return somethingThatTakesALongTime();
}
@Override
protected void onPostExecute(Integer result) {
mProgressDialog.dismiss();
setContentView(R.layout.my_screen); // setContent view here... then it works...
updateTheUiWithResult(result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
老实说,为什么你需要调用setContentView我onPostExecute不知道......但这样做意味着你可以为进度条设置自定义样式(并且它们不会在你身上消失!)
| 归档时间: |
|
| 查看次数: |
12843 次 |
| 最近记录: |