如何使用Progressbar而不使用線程和處理程序? 我用下面的代碼,但這是行不通的。如何在不使用線程和處理程序的情況下使用進度條?
ProgressDialog prd=new ProgressDialog(this); prd.setTitle("Please Wait........"); prd.show();
如何使用Progressbar而不使用線程和處理程序? 我用下面的代碼,但這是行不通的。如何在不使用線程和處理程序的情況下使用進度條?
ProgressDialog prd=new ProgressDialog(this); prd.setTitle("Please Wait........"); prd.show();
private class SomeTask extends AsyncTask {
private ProgressDialog dialog = new ProgressDialog(SomeActivity.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage(getResources().getString(R.string.someMessage));
this.dialog.show();
}
@Override
protected List<Object> doInBackground(Object... objects) {
....
try {
// some logic here
} catch (Exception e) {
e.printStackTrace();
if (this.dialog.isShowing()) this.dialog.dismiss();
}
return list;
}
@Override
protected void onPostExecute(Object result) {
if (this.dialog.isShowing()) this.dialog.dismiss();
}
}
進度對話框總是用來顯示在顯示主題對話框時有一些後臺任務正在進行。沒有這個,沒有進度對話的目的。
但是,您可以使用AsyncTask類作爲線程和處理程序的替代品來顯示android中的進度對話框。
請參閱this for AsyncTask類。
進度條和進度對話框不同。
您也可以通過xml創建進度條。請看下面的代碼,以瞭解創建進度條
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FIL_Main_RelativeLayout"
android:background="#ffffff">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="@android:style/Widget.ProgressBar.Inverse"
android:id="@+id/FIL_Middle_ProgressBar"></ProgressBar>
</RelativeLayout>
感謝 迪帕克
,你可以將可見性設置爲去當你想隱藏一段時間:); progressBar.setVisibility(View.GONE); – Houcine 2011-05-25 10:12:04
什麼叫不工作呢?請更具體地說。 – 2011-05-25 09:58:58