1 /**
2 * 默认对话框事件响应方法
3 *
4 * @param v
5 */
6 public void showDefaultDialog(View v) {
7 //创建一个Builder
8 AlertDialog.Builder builder = new AlertDialog.Builder(this);
9 设置对话框标题
10 builder.setTitle("提示"11 设置对话框内容
12 builder.setMessage("这是一个对话框的提示"13 积极按钮?
14 builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
15 @Override
16 void onClick(DialogInterface dialog,1)">int which) {
17 Toast.makeText(MainActivity.this,"你选择了OK",Toast.LENGTH_SHORT).show();
18 }
19 });
20 消极按钮
21 builder.setNegativeButton("Cancel",1)">22 23 24 Toast.makeText(MainActivity.25 26 27 创建一个对话框
28 AlertDialog dialog = builder.create();
29 显示一个对话框
30 dialog.show();
31 }
1 * 列表对话框响应事件
showListDialog(View v) {
构件一个对话框Builder
设置标题
10 builder.setTitle(R.string.pick_color);
将资源文件转化为数组对象
12 arr_colors = getResources().getStringArray(R.array.arr_colors);
设置列表内容和点击事件
14 builder.setItems(R.array.arr_colors,"你选择了[" + arr_colors[which] + "]"21 AlertDialog dialog =22 23 24 }
1 * 单选框对话框按钮事件
showSingleDialog(View v) {
13 checkedItems=new [arr_colors.length];
14 设置列表内容和点击事件,第二个参数checkedItem表示默认选中项
15 builder.setSingleChoiceItems(R.array.arr_colors,1)">16 17 18 for(int i=0;i<checkedItems.length;i++){
19 if(i==which){
20 checkedItems[i]=1;
21 }else{
22 checkedItems[i]=0 }
24 }
25 Toast.makeText(MainActivity.27 28 29 builder.setPositiveButton("OK",1)">31 32 Toast.makeText(MainActivity.getCheckedItems(),1)">33 34 35 36 builder.setNegativeButton("Cancel",1)">37 38 39 Toast.makeText(MainActivity.40 41 42 43 AlertDialog dialog =44 45 46 }
1 * 复选框对话框事件
showMultipleDialog(View v) {
15 builder.setMultiChoiceItems(R.array.arr_colors,1)">null,1)"> DialogInterface.OnMultiChoiceClickListener() {
int which,1)">boolean isChecked) {
18 String msg=""19 if(isChecked){
20 checkedItems[which]=121 msg="选择"22 }23 checkedItems[which]=024 msg="取消"26 Toast.makeText(MainActivity.28 30 builder.setPositiveButton("OK",1)">31 32 33 Toast.makeText(MainActivity.35 36 37 builder.setNegativeButton("Cancel",1)">38 39 40 Toast.makeText(MainActivity.42 43 44 AlertDialog dialog =45 46 47 }
* 自定义对话框事件按钮
4 5 showCustomerDialog(View v) {
6
7 AlertDialog.Builder builder = 8 Get the layout inflater
9 LayoutInflater inflater = getLayoutInflater();
10 View customerView=inflater.inflate(R.layout.dialog_layout,1)">null Inflate and set the layout for the dialog
12 Pass null as the parent view because its going in the dialog layout
13 mUserName= (EditText) customerView.findViewById(R.id.username);
14 mPassword= (EditText) customerView.findViewById(R.id.password);
builder.setView(customerView)
16 Add action buttons
17 .setPositiveButton(R.string.sign_in,1)"> @Override
id) {
20 sign in the user ...
21 Toast.makeText(MainActivity.mPassword.getText(),1)"> })
24 .setNegativeButton(R.string.cancel2,1)">25 26 Toast.makeText(MainActivity.27
29 });
30 31 AlertDialog dialog =32 34 }