Android 弹窗

消息提示框

1
2
3
4
5
6
7
8
new AlertDialog.Builder(this)
.setTitle("标题")
.setMessage("简单的消息提示框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
}).show();

是否提示框

1
2
3
4
5
6
7
8
9
10
11
12
13
new AlertDialog.Builder(this)
.setTitle("带确定键的提示框")
.setMessage("确定吗")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
}).show();

文本提示框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
final EditText edt = new EditText(this);
edt.setMinLines(3);
new AlertDialog.Builder(this)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(edt)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

Log.d(TAG, edt.getText().toString());
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
}).show();

单选提示框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
new AlertDialog.Builder(this)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[]{"选项1","选项2","选项3","选项4","选项5","选项6"}, -1, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1) {
case 0:Log.d(TAG, "选择了一");break;
case 1:Log.d(TAG, "选择了二");break;
case 2:Log.d(TAG, "选择了三");break;
case 3:Log.d(TAG, "选择了四");break;
case 4:Log.d(TAG, "选择了五");break;
case 5:Log.d(TAG, "选择了六");break;
default: break;
}
arg0.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
}).show();

多选提示框

1
2
3
4
5
6
7
8
9
10
11
12
13
new AlertDialog.Builder(this)
.setTitle("多选框")
.setMultiChoiceItems(new String[]{"选项1","选项2","选项3","选项4","选项5","选项6"}, new boolean []{true,false,false,false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {

}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
}).show();

列表提示框

1
2
3
4
5
6
7
8
9
10
11
12
new AlertDialog.Builder(this)
.setTitle("列表框")
.setItems(new String[]{"列表1","列表2","列表3","列表4","列表5"}, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
})
.setNegativeButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {

}
}).show();

图片提示框

1
2
3
4
5
6
7
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.demo);
new AlertDialog.Builder(this)
.setTitle("图片框")
.setView(img)
.setPositiveButton("确定", null)
.show();