Android复习笔记(10) -对话框的使用
来源:互联网 发布:caffe识别验证码 编辑:程序博客网 时间:2024/06/15 23:51
使用Alertdialog,话不多说,直接上代码
String[] items = new String[] {"xx", "yyy", "zzz"};
int selected = 0;
boolean[] selections = new boolean[] { false, false, false };
//普通对话框
public void dialog1(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hello World").
setMessage("Hello World Text")
.setCancelable(false) //设置不可被取消
.setPositiveButton("确定", listener1)
.setNegativeButton("取消", listener1)
.setNeutralButton("稍后", listener1);
builder.show();
}
//单选对话框
public void dialog2(View v) {
new AlertDialog.Builder(this)
.setTitle("单选Demo")
.setSingleChoiceItems(items, -1, listener2)
.setPositiveButton("确定", listener2)
.show();
}
//多选对话框
public void dialog3(View v) {
new AlertDialog.Builder(this)
.setTitle("单选Demo")
.setMultiChoiceItems(items, selections, listener3)
.setPositiveButton("确定", listener4)
.show();
}
OnClickListener listener4 = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < items.length; i++) {
sb.append(selections[i] ? items[i] + " " : "");
}
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show();
}
};
OnMultiChoiceClickListener listener3 = new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
selections[which] = isChecked;
System.out.println(items[which] + ": " + isChecked);
}
};
OnClickListener listener2 = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(getApplicationContext(), "确定", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), items[selected], 0).show();
break;
default:
Toast.makeText(getApplicationContext(), items[which], 0).show();
selected = which;
break;
}
}
};
OnClickListener listener1 = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(getApplicationContext(), "确定", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(getApplicationContext(), "取消", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_NEUTRAL:
Toast.makeText(getApplicationContext(), "稍后", Toast.LENGTH_SHORT).show();
break;
}
}
};
0 0
- Android复习笔记(10) -对话框的使用
- Android复习笔记(12) -handler的使用
- Android复习笔记(12)- listView的使用
- Android复习笔记(13)-Fragment的使用(1)
- android对话框的使用
- Android对话框的使用
- 【Android】对话框的使用
- 【Android】Android对话框的使用
- android search对话框的使用
- Android之 对话框 的使用
- android 之对话框的使用
- Android中对话框的使用
- android对话框的进一步使用
- Android中对话框的使用
- android之对话框的使用
- Android 中对话框的使用
- Android之对话框的使用
- Android自定义对话框的使用
- c++实现链表的抽象数据结构
- POJ 3620 Avoid The Lakes(dfs)
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
- HDU 5344
- 剑指Offer面试题24(Java版):二叉搜索树的后序遍历序列
- Android复习笔记(10) -对话框的使用
- HDU 5351 MZL's Border
- HDU 2546 饭卡
- 多线程1
- UVa 567 Risk (Floyd+任意两点最短路)
- C语言编程入门——综合练习(三)学生管理系统
- 正则表达式30分钟入门教程
- JAVA学习笔记(一)
- Oracle 11g R2+RAC+ASM+redhat安装详解1