有幾天有一些問題。 我需要顯示簡單的ProgressBar
(不是對話),而在主線程中做一些東西... 我認爲它是一個非常簡單的問題,但我不能這樣做,請幫助我。在另一個線程中顯示ProgressBar
首先我試過簡單的setVisibility(View.VISIBLE)
之前和setVisibility(View.GONE)
之後。
但是,這是在同一個線程,並ProgressBar
凍結,而我的功能工作。
現在我有這樣的代碼,但我有一些錯誤,我不知道什麼是錯..
我簡單的佈局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
我有一個基本活動:
public class BaseActivity extends Activity {
public ProgressBar loading;
public class ProgressBarShow extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... unused) {
return(null);
}
protected void onProgressUpdate() {
}
protected void onPreExecute() {
loading.setVisibility(View.VISIBLE);
}
protected void onPostExecute() {
}
}
}
最後是我的工作活動,它延伸了BaseActivity
public class SearchActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loading = (ProgressBar) findViewById(R.id.loading);
new ProgressBarShow().execute();
//doing long stuff
//new ProgressBarHide().execute(); there isnt, but sense the same
}
}
我有很多活動,這需要進度條,這就是爲什麼我創建了BaseActivity
, 不公開代碼。
我需要在主線程中做長時間的工作(stuff函數),因爲我想凍結主窗口並且不允許用戶做任何事情(點擊按鈕等),只是顯示工作進度條。
我的例子有什麼問題?或者給我一些意見,我怎麼能做到這一點更好
做的,如果你想阻止與應用程序,爲什麼用戶交互你不只是使用進度對話框?我建議你使用進度對話框,因爲正在做你想要的。 – Cata 2012-04-06 08:48:06