转自:https://blog.csdn.net/guizhongyun/article/details/60339208
Android常用如下七种对话框,基本涵盖了大部分应用场景,所以有些时候没必要自己去定义对话框,除非对话框的样式不满足UI的设计.常用七种对话框如下所示:
- 简单对话框
- 简单列表对话框
- 单选列表对话框
- 多选列表对话框
- 进度条对话框
- 编辑对话框
- 自定义对话框
1. 简单对话框
private void showNormalDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("简单对话框")
.setIcon(R.mipmap.ic_launcher)
.setMessage("这是一个简单对话框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//定义自己想要做的操作
showText("确定");
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("取消");
}
});
builder.create().show();
}
2. 简单列表对话框
private void showListDialog() {
final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("简单列表对话框");
//由于message 和 items 都属于弹出框的内容部分,两者只能显示一个, 所以千万不要加这句,不然列表显示不出来
// builder.setMessage("这是一个简单的列表对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText(items[which]);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("确定");
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("取消");
}
});
builder.create().show();
}
3. 单选列表对话框
private void showSingleChoiceDialog() {
final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("简单列表对话框");
//由于message 和 items 都属于弹出框的内容部分,两者只能显示一个, 所以千万不要加这句,不然列表显示不出来
// builder.setMessage("这是一个简单的列表对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText(items[which]);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("确定");
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("取消");
}
});
builder.create().show();
}
4. 多选对话框.
private void showMultiChoiceDialog() {
final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("简单列表对话框");
//由于message 和 items 都属于弹出框的内容部分,两者只能显示一个, 所以千万不要加这句,不然列表显示不出来
// builder.setMessage("这是一个简单的列表对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(items, new boolean[]{false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
showText(items[which]);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("确定");
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showText("取消");
}
});
builder.create().show();
}
5. 进度条对话框.
private void showProgressDialog() {
final ProgressDialog dialog = new ProgressDialog(this);
final int MAX_PROGRESS = 100;
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setTitle("提示");
dialog.setMessage("这是一个进度条对话框");
dialog.setProgress(0);
//监听取消时间
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
showText("进度条结束");
}
});
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
int progress = 0;
while (progress < MAX_PROGRESS) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
//非UI线程,可以处理DialogUI
dialog.setProgress(progress);
}
dialog.cancel();
}
}).start();
}
6. 编辑对话框,也属于自定义对话框.
private void showEditDialog() {
final EditText editText = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("编辑对话框");
builder.setMessage("这是一个编辑对话框");
builder.setView(editText);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String input = editText.getText().toString();
showText(input);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
7. 自定义对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="299dip"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="提示"
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:gravity="center_vertical"
android:paddingLeft="20.0dip"
android:textSize="16dip"
android:background="#288DF5"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="left|center"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:textColor="@color/black"
android:text="@string/custom_dialog_msg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip">
<Button
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_weight="1"
android:text="确定"
android:textColor="@color/white"
android:textSize="16sp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_marginLeft="5dip"
android:layout_weight="1"
android:text="取消"
android:textColor="@color/white"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
/**
* alertDialog提供了setView与setContentView两种方法自定义视图。
* setContentView是在AlertDialog的父类Dialog的方法,在父类Dialog方法中,是调用了Window.setContentView。这是对应整个对话框窗口的view。
* setView则是AlertDialog的方法,setView指的设置CustomView的部分而不是整个窗体,如果我们设置了buttonPanel又设置了contentPanel,则会在自定义的view下面显示自带的button。
*/
public class TestActivity extends BaseActivity {
AlertDialog alertDialogWithSetView;
AlertDialog alertDialogWithSetContentView;
void showDialogWithSetView() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_dialog, null);
Button btnConfirm = (Button) layout.findViewById(R.id.btn_confirm);
btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_confirm:
ToastUtil.showToast(context, "点击了确定按钮");
alertDialogWithSetView.dismiss();
break;
}
}
});
alertDialogWithSetView = builder.setView(layout).create();
alertDialogWithSetView.setCancelable(false);//false-dialog弹出后会点击返回键,dialog不消失; true-点击返回键可消失;要想消失可以调用dismiss方法
alertDialogWithSetView.setCanceledOnTouchOutside(false);//false-点击对话框以外的区域,dialog不消失; true-点击对话框以外区域,dialog消失.
//先调用show这个方法,然后才设置dialog的长宽
alertDialogWithSetView.show();
//获取屏幕的长宽
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenheight = display.getHeight();
int screenWidth = display.getWidth();
//设置弹出框的长宽
Window window = alertDialogWithSetView.getWindow();
//设置窗口的位置
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = screenWidth / 3;
layoutParams.height = screenheight / 4;
window.setAttributes(layoutParams);
}
void showDialogWithSetContentView() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
alertDialogWithSetContentView = builder.create();
alertDialogWithSetContentView.setCancelable(false);//false-dialog弹出后会点击返回键,dialog不消失; true-点击返回键可消失;要想消失可以调用dismiss方法
alertDialogWithSetContentView.setCanceledOnTouchOutside(false);//false-点击对话框以外的区域,dialog不消失; true-点击对话框以外区域,dialog消失.
//先调用show这个方法,然后才设置dialog的长宽
alertDialogWithSetContentView.show();
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_dialog, null);
Button btnConfirm = (Button) layout.findViewById(R.id.btn_confirm);
btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_confirm:
ToastUtil.showToast(context, "点击了确定按钮");
alertDialogWithSetContentView.dismiss();
break;
}
}
});
alertDialogWithSetContentView.setContentView(layout);
//获取屏幕的长宽
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenheight = display.getHeight();
int screenWidth = display.getWidth();
//设置弹出框的长宽
Window window = alertDialogWithSetContentView.getWindow();
//设置窗口的位置
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = screenWidth / 3;
layoutParams.height = screenheight / 4;
window.setAttributes(layoutParams);
}
@Override
public int setCustomContentViewResourceId() {
return 0;
}
@Override
public void initParam() {
}
@Override
public void initView() {
}
@Override
public void initOperator() {
}
}