Dialog 是Android 常用控件之一,主要以弹出框的形式与用户进行交互。对话框是提示用户作出决定或输入额外信息的小窗口。 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件。通过本章学习可以快速掌握Dialog的使用方法。主要涉及的知识点如下:
- 简单对话框
- 多选按钮对话框
- 单选按钮对话框
- 列表对话框
- 水平进度条对话框
- 圆形进度条对话框
- 自定义图文对话框
- 自定义输入对话框
- 自定义样式对话框
- 自定义Loading样式对话框
- 继承 DialogFragment 实现对话框
- Activity形式的 对话框
Dialog 继承关系如下:
java.lang.Object
↳ android.app.Dialog
Dialog 基本样式解析
1.标题
这是可选项,只应在内容区域被详细消息、列表或自定义布局占据时使用。 如需陈述的是一条简单消息或问题(如图 1 中的对话框),则不需要标题。
2.内容区域
它可以显示消息、列表或其他自定义布局。
3.操作按钮
对话框中的操作按钮不应超过三个。
1. 简单对话框
- 实现效果:
- 实现代码如下:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.gril).setTitle("简单对话框")
.setMessage("设置Dialog 显示的内容")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DiaLogMethods.this, "点击了确定按钮",
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancle", null).create().show();
2. 多选按钮对话框
- 实现效果:
- 实现代码:
final String font[] = { "小号字体", "中号字体", "大号字体", "超大号字体" };
final boolean[] MultiChoice = new boolean[] { false, true, false, false };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选对话框")
.setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(font, MultiChoice,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
MultiChoice[which] = isChecked;
String choiceString = "";
for (int i = 0; i < MultiChoice.length; i++) {
if (MultiChoice[i]) {
choiceString = choiceString + font[i]
+ " ";
}
}
if (choiceString.equals("")
|| choiceString.length() == 0) {
// 都不选的处理方法
Toast.makeText(DiaLogMethods.this,
"请选择一个内容", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(DiaLogMethods.this,
"选择的字体为" + choiceString,
Toast.LENGTH_SHORT).show();
}
}
}).setPositiveButton("OK", null)
.setNegativeButton("Cancle", null).create().show();
3.单选按钮对话框
- 实现效果:
- 实现代码如下:
final String font[] = { "小号字体", "中号字体", "大号字体", "超大号字体" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框")
.setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(font, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(DiaLogMethods.this,
"选择的字体为:" + font[which],
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).setPositiveButton("OK", null)
.setNegativeButton("Cancle", null).create().show();
4. 列表对话框
- 实现效果如下:
- 实现代码如下:
final String font[] = { "小号字体", "中号字体", "大号字体", "超大号字体" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle(" 列表对话框")
.setItems(font, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DiaLogMethods.this,
"选择内容是:" + font[which], Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("Cancle", null)
.setPositiveButton("OK", null).create().show();
5. 水平进度条对话框
- 实现效果如下:
- 实现代码如下:
final ProgressDialog progressDialog = new ProgressDialog(
DiaLogMethods.this);
progressDialog.setTitle("进度对话框");
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setMessage("加载中...");
// 水平进度条显示
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 圆形进度条显示
// progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.setButton("Cancle",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DiaLogMethods.this, "取消进度条对话框",
Toast.LENGTH_LONG).show();
progressDialog.cancel();
count = 0;
}
});
progressDialog.setMax(100);
progressDialog.show();
count = 0;
new Thread() {
@Override
public void run() {
while (count <= 100) {
progressDialog.setProgress(count++);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
progressDialog.dismiss();
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}.start();
6. 圆形进度条对话框
- 实现效果如下:
- 实现代码如下:
final ProgressDialog progressDialog = new ProgressDialog(
DiaLogMethods.this);
progressDialog.setTitle("进度对话框");
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setMessage("加载中...");
// 水平进度条显示
// progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 圆形进度条显示
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.setButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DiaLogMethods.this, "取消进度条对话框",
Toast.LENGTH_LONG).show();
progressDialog.cancel();
count = 0;
}
});
progressDialog.setMax(100);
progressDialog.show();
count = 0;
new Thread() {
@Override
public void run() {
while (count <= 100) {
progressDialog.setProgress(count++);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
progressDialog.dismiss();
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}.start();
注意 :
7. 自定义图文对话框
- 实现效果如下:
- 实现代码如下:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View contextview = getLayoutInflater().inflate(
R.layout.dialog_customt_img_tv, null);
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linlout1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.linlout2);
ImageView img1 = (ImageView) contextview.findViewById(R.id.img1);
TextView tv1 = (TextView) contextview.findViewById(R.id.tv1);
// 这里可以处理一些点击事件
builder.setIcon(R.drawable.gril).setTitle("自定义对话框")
.setView(contextview)
// 或者在这里处理一些事件
.setPositiveButton("OK", null)
.setNegativeButton("Cancle", null).create().show();
注意: 1. 自定义图文对话框的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="自定义Dialog"
android:textColor="@android:color/black"
android:textSize="25sp" />
</LinearLayout>
8. 自定义输入对话框
- 实现效果如下:
- 实现代码如下:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View Tittleview = getLayoutInflater().inflate(
R.layout.dialog_custom_layout, null);
ImageView img2 = (ImageView) Tittleview.findViewById(R.id.img2);
TextView textView = (TextView) Tittleview.findViewById(R.id.tv2);
textView.setText("自定义对话框");
img2.setImageResource(R.drawable.ic_launcher);
// 自定义tittle
builder.setCustomTitle(Tittleview);
View contentView = getLayoutInflater().inflate(
R.layout.dialog_custom_et, null);
EditText username = (EditText) contentView.findViewById(R.id.username);
EditText passworld = (EditText) contentView
.findViewById(R.id.passworld);
builder.setView(contentView);
builder.setPositiveButton("OK", null).setNegativeButton("Cancle", null)
.create().show();
注意 : 自定义对话框 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="自定义Dialog"
android:textColor="@android:color/black"
android:textSize="25sp" />
</LinearLayout>