Android 6種 常用對話框Dialog封裝
包括:
消息對話框、警示(含確認、取消)對話框、單選對話框、
復選對話框、列表對話框、自定義視圖(含確認、取消)對話框
分別如下圖所示:
封裝后代碼:
package dialog; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.view.View; /** * 對話框封裝類 * * @author Z * */ public class DialogTool { public static final int NO_ICON = -1; //無圖標 /** * 創建消息對話框 * * @param context 上下文 必填 * @param iconId 圖標,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 標題 必填 * @param message 顯示內容 必填 * @param btnName 按鈕名稱 必填 * @param listener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @return */ public static Dialog createMessageDialog(Context context, String title, String message, String btnName, OnClickListener listener, int iconId) { Dialog dialog = null; AlertDialog.Builder builder = new AlertDialog.Builder(context); if (iconId != NO_ICON) { //設置對話框圖標 builder.setIcon(iconId); } //設置對話框標題 builder.setTitle(title); //設置對話框消息 builder.setMessage(message); //設置按鈕 builder.setPositiveButton(btnName, listener); //創建一個消息對話框 dialog = builder.create(); return dialog; } /** * 創建警示(確認、取消)對話框 * * @param context 上下文 必填 * @param iconId 圖標,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 標題 必填 * @param message 顯示內容 必填 * @param positiveBtnName 確定按鈕名稱 必填 * @param negativeBtnName 取消按鈕名稱 必填 * @param positiveBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param negativeBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @return */ public static Dialog createConfirmDialog(Context context, String title, String message, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener, int iconId) { Dialog dialog = null; AlertDialog.Builder builder = new AlertDialog.Builder(context); if (iconId != NO_ICON) { //設置對話框圖標 builder.setIcon(iconId); } //設置對話框標題 builder.setTitle(title); //設置對話框消息 builder.setMessage(message); //設置確定按鈕 builder.setPositiveButton(positiveBtnName, positiveBtnListener); //設置取消按鈕 builder.setNegativeButton(negativeBtnName, negativeBtnListener); //創建一個消息對話框 dialog = builder.create(); return dialog; } /** * 創建單選對話框 * * @param context 上下文 必填 * @param iconId 圖標,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 標題 必填 * @param itemsString 選擇項 必填 * @param positiveBtnName 確定按鈕名稱 必填 * @param negativeBtnName 取消按鈕名稱 必填 * @param positiveBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param negativeBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param itemClickListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @return */ public static Dialog createSingleChoiceDialog(Context context, String title, String[] itemsString, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener, OnClickListener itemClickListener, int iconId) { Dialog dialog = null; AlertDialog.Builder builder = new AlertDialog.Builder(context); if (iconId != NO_ICON) { //設置對話框圖標 builder.setIcon(iconId); } //設置對話框標題 builder.setTitle(title); //設置單選選項, 參數0: 默認第一個單選按鈕被選中 builder.setSingleChoiceItems(itemsString, 0, itemClickListener); //設置確定按鈕 builder.setPositiveButton(positiveBtnName, positiveBtnListener); //設置確定按鈕 builder.setNegativeButton(negativeBtnName, negativeBtnListener); //創建一個消息對話框 dialog = builder.create(); return dialog; } /** * 創建復選對話框 * * @param context 上下文 必填 * @param iconId 圖標,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 標題 必填 * @param itemsString 選擇項 必填 * @param positiveBtnName 確定按鈕名稱 必填 * @param negativeBtnName 取消按鈕名稱 必填 * @param positiveBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param negativeBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param itemClickListener 監聽器,需實現android.content.DialogInterface.OnMultiChoiceClickListener;接口 必填 * @return */ public static Dialog createMultiChoiceDialog(Context context, String title, String[] itemsString, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener, OnMultiChoiceClickListener itemClickListener, int iconId) { Dialog dialog = null; AlertDialog.Builder builder = new AlertDialog.Builder(context); if (iconId != NO_ICON) { //設置對話框圖標 builder.setIcon(iconId); } //設置對話框標題 builder.setTitle(title); //設置選項 builder.setMultiChoiceItems(itemsString, null, itemClickListener); //設置確定按鈕 builder.setPositiveButton(positiveBtnName, positiveBtnListener); //設置確定按鈕 builder.setNegativeButton(negativeBtnName, negativeBtnListener); //創建一個消息對話框 dialog = builder.create(); return dialog; } /** * 創建列表對話框 * * @param context 上下文 必填 * @param iconId 圖標,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 標題 必填 * @param itemsString 列表項 必填 * @param negativeBtnName 取消按鈕名稱 必填 * @param negativeBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @return */ public static Dialog createListDialog(Context context, String title, String[] itemsString, String negativeBtnName, OnClickListener negativeBtnListener, OnClickListener itemClickListener, int iconId) { Dialog dialog = null; AlertDialog.Builder builder = new AlertDialog.Builder(context); if (iconId != NO_ICON) { //設置對話框圖標 builder.setIcon(iconId); } //設置對話框標題 builder.setTitle(title); //設置列表選項 builder.setItems(itemsString, itemClickListener); //設置確定按鈕 builder.setNegativeButton(negativeBtnName, negativeBtnListener); //創建一個消息對話框 dialog = builder.create(); return dialog; } /** * 創建自定義(含確認、取消)對話框 * * @param context 上下文 必填 * @param iconId 圖標,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 標題 必填 * @param positiveBtnName 確定按鈕名稱 必填 * @param negativeBtnName 取消按鈕名稱 必填 * @param positiveBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param negativeBtnListener 監聽器,需實現android.content.DialogInterface.OnClickListener接口 必填 * @param view 對話框中自定義視圖 必填 * @return */ public static Dialog createRandomDialog(Context context, String title, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener,View view, int iconId) { Dialog dialog = null; AlertDialog.Builder builder = new AlertDialog.Builder(context); if (iconId != NO_ICON) { //設置對話框圖標 builder.setIcon(iconId); } //設置對話框標題 builder.setTitle(title); builder.setView(view); //設置確定按鈕 builder.setPositiveButton(positiveBtnName, positiveBtnListener); //設置確定按鈕 builder.setNegativeButton(negativeBtnName, negativeBtnListener); //創建一個消息對話框 dialog = builder.create(); return dialog; } }
使用示例:
package com.example.encapsulation; import java.util.ArrayList; import dialog.DialogTool; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import application.CcsApplication; public class MainActivity extends Activity { Dialog dialog = null; String[] contents = {"第一項", "第二項", "第三項", "第四項", "第五項"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setDialog(); CcsApplication ccsApplication = (CcsApplication)getApplicationContext(); Log.v("serverIp", ccsApplication.getServerIp()); } public void setDialog() { dialog = DialogTool.createMessageDialog(MainActivity.this, "標題", "內容", "按鈕", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, DialogTool.NO_ICON); dialog.show(); /* dialog = DialogTool.createConfirmDialog(MainActivity.this, "標題", "內容", "確定按鈕", "取消按鈕", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, DialogTool.NO_ICON); dialog.show(); */ /* dialog = DialogTool.createSingleChoiceDialog(MainActivity.this, "標題", contents, "確定按鈕", "取消按鈕", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, DialogTool.NO_ICON); dialog.show(); */ /* dialog = DialogTool.createMultiChoiceDialog(MainActivity.this, "標題", contents, "確定按鈕", "取消按鈕", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub } }, DialogTool.NO_ICON); dialog.show(); */ /* dialog = DialogTool.createListDialog(MainActivity.this, "標題", contents, "取消按鈕", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, DialogTool.NO_ICON); dialog.show(); */ /* EditText editText = new EditText(MainActivity.this); ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(R.drawable.ic_launcher); // View view = new View(MainActivity.this); // ArrayList<View> childViews = new ArrayList<View>(); // childViews.add(imageView); // childViews.add(editText); // view.addChildrenForAccessibility(childViews); dialog = DialogTool.createRandomDialog(MainActivity.this, "標題", "確定按鈕", "取消按鈕", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }, imageView, DialogTool.NO_ICON); dialog.show(); */ } }
THE END