Android培训实战课程一:Dialog篇
1对话框的.确定和取消
确定和取消这两个按钮 通过调用 setPositiveButton 方法 和 setNegativeButton 方法 可以设置按钮的显示内容以及按钮的监听事件。
使用AlerDialog 创建对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
使用builder设置对话框的title button icon 等等
1 builder.setIcon(R.drawable.icon);
2 builder.setTitle("你确定要离开吗?");
3 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
4 public void onClick(DialogInterface dialog, int whichButton) {
5 //这里添加点击确定后的逻辑
6 showDialog("你选择了确定");
7 }
8 });
9 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
10 public void onClick(DialogInterface dialog, int whichButton) {
11 //这里添加点击确定后的逻辑
12 showDialog("你选择了取消");
13 }
14 });
15 builder.create().show();
这个dialog用于现实onClick后监听的内容信息
1 private void showDialog(String str) {
2 new AlertDialog.Builder(MainDialog.this)
3 .setMessage(str)
4 .show();
5 }
2.多个按钮信息框
3.
1 AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
2 builder.setIcon(R.drawable.icon);
3 builder.setTitle("投票");
4 builder.setMessage("您认为什么样的内容能吸引您?");
5 builder.setPositiveButton("搞笑的", new DialogInterface.OnClickListener() {
6 public void onClick(DialogInterface dialog, int whichButton) {
7 showDialog("你选择了搞笑的");
8 }
9 });
10 builder.setNeutralButton("有意义的", new DialogInterface.OnClickListener() {
11 public void onClick(DialogInterface dialog, int whichButton) {
12 showDialog("你选择了有意义的");
13 }
14 });
15 builder.setNegativeButton("抒情的", new DialogInterface.OnClickListener() {
16 public void onClick(DialogInterface dialog, int whichButton) {
17 showDialog("你选择了抒情的");
18 }
19 });
20 builder.create().show();
4.列表框
这个数组用于列表选择
1 AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
2 builder.setTitle("列表选择框");
3 builder.setItems(mItems, new DialogInterface.OnClickListener() {
4 public void onClick(DialogInterface dialog, int which) {
5 //点击后弹出窗口选择了第几项
6 showDialog("你选择的id为" + which + " , " + mItems[which]);
7 }
8 });
9 builder.create().show();
4.单项选择列表框
mSingleChoice 用于记录单选中的ID
int mSingleChoiceID = -1;
1 AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
2
3 mSingleChoiceID = -1;
4 builder.setIcon(R.drawable.icon);
5 builder.setTitle("单项选择");
6 builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
7 public void onClick(DialogInterface dialog, int whichButton) {
8 mSingleChoiceID = whichButton;
9 showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
10 }
11 });
12 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
13 public void onClick(DialogInterface dialog, int whichButton) {
14 if(mSingleChoiceID > 0) {
15 showDialog("你选择的是" + mSingleChoiceID);
16 }
17 }
18 });
19 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
20 public void onClick(DialogInterface dialog, int whichButton) {
21
22 }
23 });
24 builder.create().show();
注明:未完,另还有两个自定义,读取进度框的源码例子,查看请进——》http://blog.sina.com.cn/s/blog_6aeab2610100x4er.html