自定義 Dialog

        Android Dev-Guide 推薦重寫Activity.onCreateDialog()方法來創建Dialog,這樣Dialog就歸屬於這個Activity了。使用方法是這樣的,Activity.showDialog()激發Activity.onCreateDialog()創建Dialog,然後顯示之,便於多個Dialog的統一管理。注意,以後再用Activity.showDialog()顯示同一個Dialog時,則不會調用Activity.onCreateDialog(),而是調用Activity.onPrepareDialog(),使用上一次顯示Dialog時的狀態。即
    第一次:showDialog() -> onCreatedialog()
        以後: showDialog() -> onPrepareDialog()

        在示例代碼中,分別用createExitDialog(),createListDialog(),createRadioDialog(),createCheckboxDialog(),創建4種Dialog,並在Activity中顯示。示例代碼如下:

package com.ipjmc.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ShowDialogActivity extends Activity {
    /** Called when the activity is first created. */
	public static final String TAG = "ShowDialog";
	public static final int ID_EXIT_DIALOG = 1;
	public static final int ID_LIST_DIALOG = 2;
	public static final int ID_RADIO_DIALOG = 3;
	public static final int ID_CHECKBOX_DIALOG = 4;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        showDialog(ID_EXIT_DIALOG);
        showDialog(ID_LIST_DIALOG);
        showDialog(ID_RADIO_DIALOG);
        showDialog(ID_CHECKBOX_DIALOG);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	// TODO Auto-generated method stub
    	Dialog dialog = null;
    	switch(id) {
    	case ID_EXIT_DIALOG :
    		dialog = createExitDialog();
    		break;
    	case ID_LIST_DIALOG :
    		dialog = createListDialog();
    		break;
    	case ID_RADIO_DIALOG :
    		dialog = createRadioDialog();
    		break;
    	case ID_CHECKBOX_DIALOG :
    		dialog = createCheckboxDialog();
    		break;
    	default :
    		break;
    	}
    	if (dialog != null) {
    		Log.i(TAG, dialog.toString());
    	} else {
    		Log.i(TAG, "dialog = null");
    	}
    	return dialog;
    }
    
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
    	// TODO Auto-generated method stub
    	super.onPrepareDialog(id, dialog);
    }

    //創建簡單Dialog
    public Dialog createExitDialog() {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage("Are you sure you want to exit?")
    	       .setCancelable(false)
    	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                ShowDialogActivity.this.finish();
    	           }
    	       })
    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });
    	return builder.create();
    }

    //創建ListDialog
    public Dialog createListDialog() {
    	final CharSequence[] items = {"Red", "Green", "Blue"};

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Pick a color");
    	builder.setItems(items, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    	    }
    	});
    	return builder.create();
    }

    //創建單選Dialog
    public Dialog createRadioDialog() {
    	final CharSequence[] items = {"Red", "Green", "Blue"};

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Pick a color");
    	builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int position) {
    	        Toast.makeText(getApplicationContext(), position + " -> " + items[position], Toast.LENGTH_SHORT).show();
    	        dialog.dismiss();
    	    }
    	});
    	return builder.create();
    }

    //創建多選Dialog
    public Dialog createCheckboxDialog() {
    	final CharSequence[] items = {"Red", "Green", "Blue"};
    	final boolean [] checked = new boolean [] {false, false, false};
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Pick a color");
    	builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// TODO Auto-generated method stub
				
			}
		});
    	
    	builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	                ShowDialogActivity.this.finish();
	           }
	       })
	       .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	                dialog.cancel();
	           }
	       });
    	
    	return builder.create();
   
    }
}


發佈了10 篇原創文章 · 獲贊 8 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章