顯示進度對話框
在程序中調用showDialog的時候,系統會回調函數onCreateDialog,此時可以根據傳遞過來的id進行相應的顯示
如果需要顯示對話框,則需要覆寫onCreateDialog函數,在顯示選項中new一個progressDialog對象,然後返回就可以了
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DIALOG1_KEY: { //有標題欄的進度對話框
- ProgressDialog dialog = new ProgressDialog(this);
- dialog.setTitle("Indeterminate");
- dialog.setMessage("Please wait while loading...");
- dialog.setIndeterminate(true);
- dialog.setCancelable(true);
- return dialog;
- }
- case DIALOG2_KEY: { //沒有標題欄的進度對話框
- ProgressDialog dialog = new ProgressDialog(this);
- dialog.setMessage("Please wait while loading...");
- dialog.setIndeterminate(true);
- dialog.setCancelable(true);
- return dialog;
- }
- }
- return null;
- }
而下面的程序則演示了 如何在窗口的標題欄顯示進度對話框
- private boolean mToggleIndeterminate = false;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- // Request progress bar
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- setContentView(R.layout.progressbar_4);
- setProgressBarIndeterminateVisibility(mToggleIndeterminate);
-
- Button button = (Button) findViewById(R.id.toggle);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- mToggleIndeterminate = !mToggleIndeterminate;
- setProgressBarIndeterminateVisibility(mToggleIndeterminate);
- }
- });
- }