Android - AlertDialog 对话框

1. 基本使用流程

Step 1. 创建 AlertDialog.Builder 对象
Step 2. 调用 setIcon() 设置图标,setTitle() 或 setCustomTitle() 设置标题
Step 3. 设置对话框的内容:setMessage() 还有其他方法来指定显示的内容
Step 4. 调用 setPositive/Negative/NeutralButton() 设置:确定,取消,中立按钮
Step 5. 调用 create() 方法创建这个对象,再调用 show() 方法将对话框显示出来

2. 示例代码

activity_main.xml

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog" />

<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog with list" />

<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog with Radio Button" />

<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog with Checkbox" />
</LinearLayout>

MainActivity.java

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private boolean[] checkItems;

private AlertDialog alert = null;
private AlertDialog.Builder builder = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
findViewById(R.id.btn4).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
alert = null;
builder = new AlertDialog.Builder(MainActivity.this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("Tip")
.setMessage("你收到了一首特别诗,要看吗?")
.setNegativeButton("不要", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Canceled.", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("当然", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "\"Get out of my head.Get out of my head.Get out of my head.Get out of my head.\nGet\nout\nof\nmy\nhead\"", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Nothing happened.", Toast.LENGTH_SHORT).show();
}
}).create();
alert.show();
break;
case R.id.btn2:
final String[] members = new String[]{"Sayori", "Natsuki", "Yuri", "Monika"};
alert = null;
builder = new AlertDialog.Builder(MainActivity.this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("要把诗给谁看呢")
.setItems(members, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "你把诗给了" + members[which], Toast.LENGTH_SHORT).show();
}
}).create();
alert.show();
break;
case R.id.btn3:
final String[] fruits = new String[]{"Monika", "M nika", "M?nIkA", "mONiKA", " o ika"};
alert = null;
builder = new AlertDialog.Builder(MainActivity.this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("周日你将会和谁进行社团活动呢")
.setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "你选择了" + fruits[which], Toast.LENGTH_SHORT).show();
}
}).create();
alert.show();
break;
case R.id.btn4:
final String[] menu = new String[]{"Monika", "Sayori", "Natsuki", "Yuri"};
checkItems = new boolean[]{false, false, false, false};
alert = null;
builder = new AlertDialog.Builder(MainActivity.this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("来吧,像个大人一样做出选择")
.setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkItems[which] = isChecked;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result = "";
for (int i = 0; i < checkItems.length; i++) {
if (checkItems[i])
result += menu[i] + " ";
}
Toast.makeText(getApplicationContext(), "这些我全都要 " + result, Toast.LENGTH_SHORT).show();
}
})
.create();
alert.show();
break;
}
}
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×