Android中几种常见的dialog显示格式,话不多说,直接上代码
package diaog.com.diaogdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn_Normal; // 普通
private Button btn_List; // 列表
private Button btn_Choice; // 单选
private Button btn_MoreChoice; // 多选
private Button btn_EditText;//可输入
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
btn_Normal = (Button) findViewById(R.id.normal_btn);
btn_List = (Button) findViewById(R.id.list_btn);
btn_Choice = (Button) findViewById(R.id.choice_btn);
btn_MoreChoice = (Button) findViewById(R.id.more_choice_btn);
btn_EditText = (Button) findViewById(R.id.editext_btn);
btn_Normal.setOnClickListener(this);
btn_List.setOnClickListener(this);
btn_Choice.setOnClickListener(this);
btn_MoreChoice.setOnClickListener(this);
btn_EditText.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.normal_btn:
dialogNormal();// 普通
break;
case R.id.list_btn:
dialogList(); // 列表
break;
case R.id.choice_btn:
dialogChoice(); // 单选
break;
case R.id.more_choice_btn:
dialogMoreChoice();// 多选
break;
case R.id.editext_btn:
dialogEditText();//可编辑
break;
default:
break;
}
}
/**
* 普通
*/
private void dialogNormal() {
DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case Dialog.BUTTON_POSITIVE:
Toast.makeText(MainActivity.this, "确认",
Toast.LENGTH_SHORT).show();
break;
case Dialog.BUTTON_NEGATIVE:
Toast.makeText(MainActivity.this, "取消",
Toast.LENGTH_SHORT).show();
break;
case Dialog.BUTTON_NEUTRAL:
Toast.makeText(MainActivity.this, "忽略",
Toast.LENGTH_SHORT).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("普通对话框");
builder.setMessage("是否确认退出?");
builder.setPositiveButton("确认", dialogOnclicListener);
builder.setNegativeButton("取消", dialogOnclicListener);
builder.setNeutralButton("忽略", dialogOnclicListener);
builder.create().show();
}
/**
* 列表
*/
private void dialogList() {
final String items[] = {"刘德华", "张柏芝", "蔡依林", "张学友"};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("列表");
// builder.setMessage("是否确认退出?"); //设置内容
builder.setIcon(R.mipmap.ic_launcher);
// 设置列表显示,注意设置了列表显示就不要设置builder.setMessage()了,否则列表不起作用。
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, items[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
}
/**
* 单选
*/
private void dialogChoice() {
final String items[] = {"男", "女", "其他"};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("单选");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
}
/**
* 多选
*/
private void dialogMoreChoice() {
final String items[] = {"JAVA", "C++", "JavaScript", "MySQL"};
final boolean selected[] = {true, false, true, false};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("多选");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(items, selected,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(MainActivity.this,
items[which] + isChecked, Toast.LENGTH_SHORT)
.show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
// android会自动根据你选择的改变selected数组的值。
// for (int i = 0; i < selected.length; i++) {
// Log.e("hongliang", "" + selected[i]);
// }
}
});
builder.create().show();
}
/**
* 可输入的对框框
*/
private void dialogEditText() {
final EditText editText = new EditText(this);
final AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("可编辑");
builder.setIcon(R.mipmap.ic_launcher);
builder.setView(editText);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, editText.getText().toString() + "", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
下来是布局文件,很简单就5个button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="diaog.com.diaogdemo.MainActivity">
<Button
android:id="@+id/normal_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通对话框" />
<Button
android:id="@+id/list_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/normal_btn"
android:text="列表对话框" />
<Button
android:id="@+id/choice_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/list_btn"
android:text="单选列表对话框" />
<Button
android:id="@+id/more_choice_btn"
android:layout_width="match_parent"
android:layout_below="@+id/choice_btn"
android:layout_height="wrap_content"
android:text="多选列表对话框" />
<Button
android:id="@+id/editext_btn"
android:layout_width="match_parent"
android:layout_below="@+id/more_choice_btn"
android:layout_height="wrap_content"
android:text="可编辑输入对话框" />
</RelativeLayout>
————————————————
版权声明:本文为CSDN博主「帅气大果果」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34471736/article/details/54248208