本文主要讨论七种形式的AlertDialog,及其编写方法。
1、退出
在用户退出的时候提示用户是否退出,含有“确定”和“退出”两个按键。
1 btnExit.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 final AlertDialog.Builder alertDlg = new AlertDialog.Builder(MainActivity.this); 5 alertDlg.setMessage("确定退出吗?"); 6 alertDlg.setTitle("退出"); 7 alertDlg.setPositiveButton("确定", new DialogInterface.OnClickListener() { 8 @Override 9 public void onClick(DialogInterface dialogInterface, int i) { 10 MainActivity.this.finish(); 11 } 12 }); 13 alertDlg.setNegativeButton("取消", null); 14 alertDlg.show(); 15 } 16 });
2、调查图表
弹出对话框,让用户选择相应的按钮来获得用户选项。含有相应的提示问题。
1 btnChart.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 AlertDialog.Builder alertDlg = new AlertDialog.Builder(MainActivity.this); 5 alertDlg.setIcon(android.R.drawable.btn_star); 6 alertDlg.setTitle("调查"); 7 alertDlg.setMessage("你喜欢的颜色是?"); 8 alertDlg.setPositiveButton("红色", new DialogInterface.OnClickListener() { 9 @Override 10 public void onClick(DialogInterface dialogInterface, int i) { 11 Toast.makeText(MainActivity.this, "你喜欢红色", Toast.LENGTH_SHORT); 12 } 13 }); 14 alertDlg.setNegativeButton("蓝色", new DialogInterface.OnClickListener() { 15 @Override 16 public void onClick(DialogInterface dialogInterface, int i) { 17 Toast.makeText(MainActivity.this, "你喜欢蓝色", Toast.LENGTH_SHORT); 18 } 19 }); 20 alertDlg.setNeutralButton("黄色", new DialogInterface.OnClickListener() { 21 @Override 22 public void onClick(DialogInterface dialogInterface, int i) { 23 Toast.makeText(MainActivity.this, "你喜欢黄色", Toast.LENGTH_SHORT); 24 } 25 }); 26 alertDlg.show(); 27 } 28 });
3、输入内容
弹出对话框,提示用户输入用户名或者密码,可以作登陆使用。
1 btnContent.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 AlertDialog.Builder alertDlg = new AlertDialog.Builder(MainActivity.this); 5 alertDlg.setTitle("请输入"); 6 alertDlg.setIcon(android.R.drawable.ic_dialog_info); 7 alertDlg.setView(new EditText(MainActivity.this)).setPositiveButton("确定", null); 8 alertDlg.setNegativeButton("取消", null); 9 alertDlg.show(); 10 } 11 });
4、单选按钮
对话框内含有一系列单选按钮让用户选择。
1 btnRadio.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 new AlertDialog.Builder(MainActivity.this).setTitle("单选框") 5 .setIcon(android.R.drawable.ic_dialog_info) 6 .setSingleChoiceItems(new String[]{"红色", "蓝色", "黄色"}, 0, null) 7 .setPositiveButton("确定", null) 8 .setNegativeButton("取消", null).show(); 9 } 10 });
5、多选按钮
对话框内含有一系列多选按钮让用户选择。
1 btnCheckBox.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 new AlertDialog.Builder(MainActivity.this).setTitle("复选框") 5 .setIcon(android.R.drawable.ic_dialog_info) 6 .setMultiChoiceItems(new String[]{"红色", "蓝色", "黄色"}, null, null) 7 .setPositiveButton("确定", null) 8 .setNegativeButton("取消", null).show(); 9 } 10 });
6、简单列表
含有一系列简单的列表,供用户点击,可以做菜单使用。
1 btnList.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 new AlertDialog.Builder(MainActivity.this).setTitle("列表框") 5 .setIcon(android.R.drawable.ic_dialog_info) 6 .setItems(new String[]{"红色", "蓝色", "黄色"}, null) 7 .setPositiveButton("确定", null) 8 .setNegativeButton("取消", null).show(); 9 } 10 });
7、自定义布局
可以自定义对话框显示内容,通过资源文件导入布局。
1 btnCostume.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View view) { 4 LayoutInflater inflater = getLayoutInflater(); 5 View layout = inflater.inflate(R.layout.activity_main, 6 (ViewGroup)findViewById(R.id.mainMenu)); 7 8 new AlertDialog.Builder(MainActivity.this).setTitle("自定义布局") 9 .setView(layout) 10 .setPositiveButton("确定", null) 11 .setNegativeButton("取消", null).show(); 12 } 13 });
8、完整代码:
1 import android.app.AlertDialog; 2 import android.app.Dialog; 3 import android.content.DialogInterface; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.Toast; 12 13 public class MainActivity extends AppCompatActivity { 14 15 private Button btnExit; 16 private Button btnChart; 17 private Button btnContent; 18 private Button btnRadio; 19 private Button btnCheckBox; 20 private Button btnList; 21 private Button btnCostume; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 btnExit = (Button)findViewById(R.id.btnExit); 29 btnChart = (Button)findViewById(R.id.btnChart); 30 btnContent = (Button)findViewById(R.id.btnContent); 31 btnRadio = (Button)findViewById(R.id.btnRadio); 32 btnCheckBox = (Button)findViewById(R.id.btnCheckBox); 33 btnList = (Button)findViewById(R.id.btnList); 34 btnCostume = (Button)findViewById(R.id.btnCostume); 35 36 37 btnExit.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View view) { 40 final AlertDialog.Builder alertDlg = new AlertDialog.Builder(MainActivity.this); 41 alertDlg.setMessage("确定退出吗?"); 42 alertDlg.setTitle("退出"); 43 alertDlg.setPositiveButton("确定", new DialogInterface.OnClickListener() { 44 @Override 45 public void onClick(DialogInterface dialogInterface, int i) { 46 MainActivity.this.finish(); 47 } 48 }); 49 alertDlg.setNegativeButton("取消", null); 50 alertDlg.show(); 51 } 52 }); 53 54 btnChart.setOnClickListener(new View.OnClickListener() { 55 @Override 56 public void onClick(View view) { 57 AlertDialog.Builder alertDlg = new AlertDialog.Builder(MainActivity.this); 58 alertDlg.setIcon(android.R.drawable.btn_star); 59 alertDlg.setTitle("调查"); 60 alertDlg.setMessage("你喜欢的颜色是?"); 61 alertDlg.setPositiveButton("红色", new DialogInterface.OnClickListener() { 62 @Override 63 public void onClick(DialogInterface dialogInterface, int i) { 64 Toast.makeText(MainActivity.this, "你喜欢红色", Toast.LENGTH_SHORT); 65 } 66 }); 67 alertDlg.setNegativeButton("蓝色", new DialogInterface.OnClickListener() { 68 @Override 69 public void onClick(DialogInterface dialogInterface, int i) { 70 Toast.makeText(MainActivity.this, "你喜欢蓝色", Toast.LENGTH_SHORT); 71 } 72 }); 73 alertDlg.setNeutralButton("黄色", new DialogInterface.OnClickListener() { 74 @Override 75 public void onClick(DialogInterface dialogInterface, int i) { 76 Toast.makeText(MainActivity.this, "你喜欢黄色", Toast.LENGTH_SHORT); 77 } 78 }); 79 alertDlg.show(); 80 } 81 }); 82 83 btnContent.setOnClickListener(new View.OnClickListener() { 84 @Override 85 public void onClick(View view) { 86 AlertDialog.Builder alertDlg = new AlertDialog.Builder(MainActivity.this); 87 alertDlg.setTitle("请输入"); 88 alertDlg.setIcon(android.R.drawable.ic_dialog_info); 89 alertDlg.setView(new EditText(MainActivity.this)).setPositiveButton("确定", null); 90 alertDlg.setNegativeButton("取消", null); 91 alertDlg.show(); 92 } 93 }); 94 95 btnRadio.setOnClickListener(new View.OnClickListener() { 96 @Override 97 public void onClick(View view) { 98 new AlertDialog.Builder(MainActivity.this).setTitle("单选框") 99 .setIcon(android.R.drawable.ic_dialog_info) 100 .setSingleChoiceItems(new String[]{"红色", "蓝色", "黄色"}, 0, null) 101 .setPositiveButton("确定", null) 102 .setNegativeButton("取消", null).show(); 103 } 104 }); 105 106 btnCheckBox.setOnClickListener(new View.OnClickListener() { 107 @Override 108 public void onClick(View view) { 109 new AlertDialog.Builder(MainActivity.this).setTitle("复选框") 110 .setIcon(android.R.drawable.ic_dialog_info) 111 .setMultiChoiceItems(new String[]{"红色", "蓝色", "黄色"}, null, null) 112 .setPositiveButton("确定", null) 113 .setNegativeButton("取消", null).show(); 114 } 115 }); 116 117 btnList.setOnClickListener(new View.OnClickListener() { 118 @Override 119 public void onClick(View view) { 120 new AlertDialog.Builder(MainActivity.this).setTitle("列表框") 121 .setIcon(android.R.drawable.ic_dialog_info) 122 .setItems(new String[]{"红色", "蓝色", "黄色"}, null) 123 .setPositiveButton("确定", null) 124 .setNegativeButton("取消", null).show(); 125 } 126 }); 127 128 btnCostume.setOnClickListener(new View.OnClickListener() { 129 @Override 130 public void onClick(View view) { 131 LayoutInflater inflater = getLayoutInflater(); 132 View layout = inflater.inflate(R.layout.activity_main, 133 (ViewGroup)findViewById(R.id.mainMenu)); 134 135 new AlertDialog.Builder(MainActivity.this).setTitle("自定义布局") 136 .setView(layout) 137 .setPositiveButton("确定", null) 138 .setNegativeButton("取消", null).show(); 139 } 140 }); 141 142 } 143 144 }