4.dialog对话框
4.1简介
4.2常用方法
4.3具体步骤
- 实例化builder
- 设置标题 图片
- 选择具体的dialog
- 创建dialog
- 显示
4.4实例
确认选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | private void showDialog1() { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("确认对话框"); // 设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图片 builder.setMessage("确认对话框的提示内容"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "succeed", Toast.LENGTH_SHORT) .show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show(); } |
单项选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("选择你的性别"); builder.setIcon(R.drawable.ic_launcher); builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); // 数组 选中次数 AlertDialog dialog = builder.create(); dialog.show(); } |
多项选择器(带取消按键)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | private void showDialog1() { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("选择你的年纪"); // 0 builder.setIcon(R.drawable.ic_launcher); builder.setMultiChoiceItems(multi_list, null, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { Toast.makeText( MainActivity.this, "你选择的是" + multi_list[which], Toast.LENGTH_SHORT).show(); } else { Toast.makeText( MainActivity.this, "你没选择的是" + multi_list[which], Toast.LENGTH_SHORT).show(); } } });// 数组 是否选中 点击事件 // 添加取消按钮 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss();// 隐藏对话框 } }); AlertDialog dialog = builder.create();// 创建 获取dialog builder.show();// 显示对话框 } |
显示列表对话款
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | private void showDialog5() { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("部门列表"); // 0 builder.setIcon(R.drawable.ic_launcher); builder.setItems(item_list, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "喜欢" + item_list[which], Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create();// 创建 获取dialog builder.show();// 显示对话框 } |
自定义列表显示框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private void showDialog5() { LayoutInflater inflater = LayoutInflater .from(MainActivity.this); View view = inflater.inflate(R.layout.dialog_layout, null); // 获取样式布局 AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this); builder.setTitle("自定义对话框"); // 0 builder.setIcon(R.drawable.ic_launcher); builder.setView(view); AlertDialog dialog = builder.create();// 创建 获取dialog builder.show();// 显示对话框 } |
百度云实例:http://pan.baidu.com/s/1o6rLFQE