- 普通对话框
- 单选对话框
- 多选对话框
- 水平进度条
- 圆形进度条
- 日期选择对话框
- 时间选择对话框
- 自定义对话框(重点)
对话框 | 类 | 特殊 |
---|---|---|
普通 | AlertDialog.Builder() | |
单选 | AlertDialog.Builder() | setSingleChoiceItems() |
多选 | AlertDialog.Builder() | setMultiChoiceItems() |
日期 | DatePickerDialog | new DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day); |
时间 | TimePickerDialog | new TimePickerDialog(context, TimePickerDialog.OnTimeSetListener,时,分,是否24进制); |
水平 | ProgressDialog | setStyle(ProgressDialog.STYLE_HORIZONTAL) |
圆圈 | ProgressDialog | setStyle(ProgressDialog.STYLE_SPINNER) |
自定义 | AlertDialog.Builder() | setView() |
一、 普通对话框
private void narmal_Dialog() {
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);//设置图标
builder.setTitle("警告:");//设置标题
builder.setMessage("确定要删除吗?");//设置内容
//设置确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//吐司提示
Toast.makeText(MainActivity.this,"你点击了确定",Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//吐司提示
Toast.makeText(MainActivity.this,"你点击了取消",Toast.LENGTH_SHORT).show();
}
});
//使用建造者创建对话框
AlertDialog dialog = builder.create();
//设置对话框显示
dialog.show();
}
二、单选对话框
private void single_Dialog() {
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置属性
builder.setIcon(R.mipmap.ic_launcher);//设置图标
builder.setTitle("请选择省份");//设置标题
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了确定",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了取消",Toast.LENGTH_SHORT).show();
}
});
//设置单选列表
final String[] items={"北京","上海","深圳"};
//事件监听
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();
}
});
//使用建造者创建对话框
AlertDialog dialog = builder.create();
//显示
dialog.show();
}
三、多选对话框
private void mulite_Dialog() {
//设置多选列表
final String[] items={"红色","绿色","蓝色","黄色"};
final boolean[] flags={true,true,true,false};
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);//设置图标
builder.setTitle("请选择你喜欢的颜色");//设置标题
//确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
//事件监听
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this,"选中了"+items[which],Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"取消了"+items[which],Toast.LENGTH_SHORT).show();
}
}
});
//显示
builder.show();
}
四、水平进度条
private void hor_Progress_Dialog() {
//实例化对象
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
//设置进度条水平
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置最大值
dialog.setMax(100);
dialog.setMessage("正在下载……");
//显示
dialog.show();
//模拟定时
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if (progress==100) {
dialog.dismiss();//消失
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);
}
五、 圆形进度条
private void hor_Progress_Dialog() {
//实例化对象
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
//设置圆圈进度条
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//设置最大值
dialog.setMax(100);
dialog.setMessage("正在下载……");
//显示
dialog.show();
//模拟定时
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
if (progress==100) {
dialog.dismiss();//消失
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);
}
六、日期选择对话框
private void date_Dialog() {
//日历对象
Calendar calendar = Calendar.getInstance();
//实例化对象
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Toast.makeText(MainActivity.this, i+"-"+(i1+1)+"-"+i2, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
七、时间选择对话框
private void time_Dialog() {
Calendar calendar=Calendar.getInstance();//日历对象
//实例化对象
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).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="vertical">
<TextView
android:id="@+id/textView"
android:textSize="30sp"
android:text="大美女"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
代码
private void customer_Dialog() {
View view= LayoutInflater.from(this).inflate(R.layout.layout_customer_dialog,null);
ImageView imageView=view.findViewById(R.id.image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击了美女图片", Toast.LENGTH_SHORT).show();
}
});
//构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置自定义布局
builder.setView(view);
//确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
//显示
builder.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:background="#fff">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="警告"
android:textColor="#38ADFF"
android:textSize="16sp"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center"
android:text="保护视力,少玩手机"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="取消"
android:textColor="#7D7D7D"
android:textSize="16sp"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E4E4E4"/>
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="确定"
android:textColor="#38ADFF"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
创建MyDialog类并继承Dialog
package com.example.day01_homework;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
public class MyDialog extends Dialog {
private Button yes;//确定按钮
private Button no;//取消按钮
private TextView titleTV;//消息标题文本
private TextView message;//消息提示文本
private String titleStr;//从外界设置的title文本
private String messageStr;//从外界设置的消息文本
//确定文本和取消文本的显示的内容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = display.getWidth()*4/5;
getWindow().setAttributes(lp);
}
public MyDialog(@NonNull Context context) {
super(context);
//去掉默认的标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
/**
* 设置取消按钮的显示内容和监听
*
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 设置确定按钮的显示内容和监听
*
* @param str
* @param yesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dialog);
//空白处不能取消动画
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
initData();
//初始化界面控件的事件
initEvent();
}
/**
* 初始化界面控件
*/
private void initView() {
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
titleTV = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
}
/**
* 初始化界面控件的显示数据
*/
private void initData() {
//如果用户自定了title和message
if (titleStr != null) {
titleTV.setText(titleStr);
}
if (messageStr != null) {
message.setText(messageStr);
}
//如果设置按钮文字
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
/**
* 初始化界面的确定和取消监听
*/
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesOnclick();
}
}
});
//设置取消按钮被点击后,向外界提供监听
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
/**
* 从外界Activity为Dialog设置标题
*
* @param title
*/
public void setTitle(String title) {
titleStr = title;
}
public void setMessage(String message) {
messageStr = message;
}
public interface onNoOnclickListener {
public void onNoClick();
}
public interface onYesOnclickListener {
public void onYesOnclick();
}
}