2025-05-23

有的時候,可能需要彈出一個對話框,以便從用戶輸入來獲取某些確認信息。這種情況下,可以重寫Activity基類中的受保護方法(protected)onCreateDialog()。

    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_dialog" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:text="Click to display a dialog" 
    android:onClick="onClick" /> 
  
</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_dialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click to display a dialog"
    android:onClick="onClick" />
 
</LinearLayout>    3.DialogActivity.java中的代碼。

[java]
package net.horsttnann.Dialog; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 
 
public class DialogActivity extends Activity { 
    CharSequence[] items = { "Google", "Apple", "Microsoft" }; 
    boolean[] itemsChecked = new boolean[items.length]; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public void onClick(View v) { 
        showDialog(0); 
    } 
 
    @Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
            return new AlertDialog.Builder(this) 
                    .setIcon(R.drawable.ic_launcher) 
                    .setTitle("This is a dialog with some simple text…") 
                    .setPositiveButton("OK", 
                            new DialogInterface.OnClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "OK clicked!", Toast.LENGTH_SHORT) 
                                            .show(); 
                                } 
                            }) 
                    .setNegativeButton("Cancel", 
                            new DialogInterface.OnClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int whichButton) { 
                                    Toast.makeText(getBaseContext(), 
                                            "Cancel clicked!", 
                                            Toast.LENGTH_SHORT).show(); 
                                } 
                            }) 
                    .setMultiChoiceItems(items, itemsChecked, 
                            new DialogInterface.OnMultiChoiceClickListener() { 
                                public void onClick(DialogInterface dialog, 
                                        int which, boolean isChecked) { 
                                    Toast.makeText( 
                                            getBaseContext(), 
                                            items[which] 
                                                    + (isChecked ? " checked!" 
                                                            : " unchecked!"), 
                                            Toast.LENGTH_SHORT).show(); 
 
                                } 
                            }).create(); 
        } 
        return null; 
    } 

package net.horsttnann.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class DialogActivity extends Activity {
    CharSequence[] items = { "Google", "Apple", "Microsoft" };
    boolean[] itemsChecked = new boolean[items.length];

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

    public void onClick(View v) {
        showDialog(0);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            return new AlertDialog.Builder(this)
                    .setIcon(R.drawable.ic_launcher)
                    .setTitle("This is a dialog with some simple text…")
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    Toast.makeText(getBaseContext(),
                                            "OK clicked!", Toast.LENGTH_SHORT)
                                            .show();
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    Toast.makeText(getBaseContext(),
                                            "Cancel clicked!",
                                            Toast.LENGTH_SHORT).show();
                                }
                            })
                    .setMultiChoiceItems(items, itemsChecked,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which, boolean isChecked) {
                                    Toast.makeText(
                                            getBaseContext(),
                                            items[which]
                                                    + (isChecked ? " checked!"
                                                            : " unchecked!"),
                                            Toast.LENGTH_SHORT).show();

                                }
                            }).create();
        }
        return null;
    }
}
 
    4.按F11調試。點擊按鈕顯示對話框,在CheckBox上面打勾,就是彈出一個Toast提示,顯示選中物件的文本信息。點擊“OK”或“Cancel”按鈕會使對話框消失。

效果圖:

  

 

提示:

    想要顯示對話框,首先要重寫Activity基類中的onCreateDialog()方法:

[java]
@Override 
protected Dialog onCreateDialog(int id) { 
    // …  

    @Override
    protected Dialog onCreateDialog(int id) {
        // …
    }
    當調用showDialog()的時候,上面被重寫的方法就被調用瞭:

[java]
public void onClick(View v) { 
    showDialog(0); 

    public void onClick(View v) {
        showDialog(0);
    }    這個創建對話框的onCreateDialog()方法是一個被Activity控制的回調函數,當調用showDialog()時,onCreateDialog()回調函數就被觸發瞭。showDialog()方法接受一個Integer參數,用來識別到底要顯示哪個對話框。一般情況下,使用switch語句去判斷顯示不同的對話框。

    想要創建一個對話框,還需要使用AlertDialog類的Builder構造器,設置不同的屬性,比如圖標、標題、按鈕、單選框等等:

[java]
    @Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
            Builder builder = new AlertDialog.Builder(this); 
            builder.setIcon(R.drawable.ic_launcher); 
            builder.setTitle("This is a dialog with some simple text…"); 
            builder.setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "OK clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setNegativeButton("Cancel", 
                    new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "Cancel clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setMultiChoiceItems(items, itemsChecked, 
                    new DialogInterface.OnMultiChoiceClickListener() { 
                        public void onClick(DialogInterface dialog, int which, 
                                boolean isChecked) { 
                            Toast.makeText( 
                                    getBaseContext(), 
                                    items[which] 
                                            + (isChecked ? " checked!" 
                                                    : " unchecked!"), 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
            return builder.create(); 
        } 
        return null; 
    } 
 

摘自  horsttnann
 

發佈留言

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