2025-05-23

當要進行耗時的操作的時候,往往會看見“請稍候”字樣的對話框。例如,用戶正在登入服務器,此時並不允許用戶使用這個軟件,或者應用程序把結果返回給用戶之前,要進行某些耗時的計算。在這些情況下,顯示一個“進度條”對話框,能友好地讓用戶等待,同時也能阻止用戶進行某些不必要的操作。

    1.創建一個名為Dialog的工程。

    2.main.xml中的代碼。

[java] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <Button 
        android:id="@+id/btn_dialog2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:onClick="onClick2" 
        android:text="Click to display a progress dialog" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_dialog2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick2"
        android:text="Click to display a progress dialog" />

</LinearLayout>    3.DialogActivity.java中的代碼。

[java] package net.horsttnann.Dialog; 
 
import net.horsttnann.Dialog.R; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.view.View; 
 
public class DialogActivity extends Activity { 
    ProgressDialog progressDialog; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public void onClick2(View v) { 
        // —show the dialog—  
        final ProgressDialog dialog = ProgressDialog.show(this, 
                "Doing something", "Please wait…", true); 
        new Thread(new Runnable() { 
            public void run() { 
                try { 
                    // —simulate doing something lengthy—  
                    Thread.sleep(5000); 
                    // —dismiss the dialog—  
                    dialog.dismiss(); 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
            } 
        }).start(); 
    } 

package net.horsttnann.Dialog;

import net.horsttnann.Dialog.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;

public class DialogActivity extends Activity {
    ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClick2(View v) {
        // —show the dialog—
        final ProgressDialog dialog = ProgressDialog.show(this,
                "Doing something", "Please wait…", true);
        new Thread(new Runnable() {
            public void run() {
                try {
                    // —simulate doing something lengthy—
                    Thread.sleep(5000);
                    // —dismiss the dialog—
                    dialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
    4.按F11調試,點擊按鈕,彈出“進度條”對話框。

 

效果圖:

  

 

提示:

    基本上,想要創建一個“進度條”對話框,隻需要創建一個ProgressDialog類的實例,然後調用show()方法:

[java] // —show the dialog—  
final ProgressDialog dialog = ProgressDialog.show(this, 
        "Doing something", "Please wait…", true); 
        // —show the dialog—
        final ProgressDialog dialog = ProgressDialog.show(this,
                "Doing something", "Please wait…", true);    因為它是一個“模態”的對話框,所以它就會把其他UI組件給遮蓋住,直到它被解除。如果想要在後臺執行一個“長期運行”的任務,可以創建一個線程。run()方法裡面的代碼將會在一個獨立的線程裡面執行。下面的代碼使用sleep()方法,模擬瞭一個需要5秒執行的後臺任務:

[java] new Thread(new Runnable() { 
    public void run() { 
        try { 
            // —simulate doing something lengthy—  
            Thread.sleep(5000); 
            // —dismiss the dialog—  
            dialog.dismiss(); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } 
    } 
}).start(); 
        new Thread(new Runnable() {
            public void run() {
                try {
                    // —simulate doing something lengthy—
                    Thread.sleep(5000);
                    // —dismiss the dialog—
                    dialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();    5秒鐘之後,執行dismiss()方法,對話框就被解除瞭。
 

摘自  horsttnann的專欄
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *