1.普通对话框
//TODO 普通对话框
public void normal_dialog(){
//TODO 1:构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("hello body");//设置标题
builder.setMessage("我是美女甜雪");//设置内容
//设置确定按钮
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
2.单选对话框
//单选对话框
private void single_dialog() {
//TODO 1:构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("杨成果的成分是");//设置标题
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
final String[] items={"男人","女人","不想"};
//TODO 设置单选列表
//TODO 参数一 列表 参数二 默认选中下表 参数三 事件监听
builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中烦恼的"+items[which], Toast.LENGTH_SHORT).show();
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
3.多选对话框
private void mulite_dialog() {
final String[] items={"凤姐","小赵","如花","陈琳"};
final boolean[] flags={true,true,true,false};
//TODO 1:构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:画画
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("杨成果的女朋友们");//设置标题
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0;i<flags.length;i++){
if(flags[i]){
Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();
}
}
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
//TODO 设置多选列表
//TODO 参数一 列表 参数二 默认选中下表 参数三 事件监听
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
4.自定义对话框
1.自定义布局xml
<?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"
android:gravity="center">
<TextView
android:textSize="30sp"
android:text="大美女"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="+@id/iv"
android:src="@drawable/tp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
java代码
//TODO 0:获得视图
View view=LayoutInflater.from(this).inflate(R.layout.customer_dialog,null);
ImageView imageView=view.findViewById(R.id.iv);
imageView.setOnclickListener(new OnclickListsner(){
//点击图片就可以吐司或者关闭对话框了
});
//TODO 1:构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置自定义布局
builder.setView(view);
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
5.水平进度条对话框
//水平进度条
private void hor_progress_dialog() {
//TODO 1:实例化对象
final ProgressDialog dialog=new ProgressDialog(this);
//TODO 2:设置属性
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置水平央视
dialog.setMax(100);
dialog.setMessage("正在下载");
//TODO 3:显示
dialog.show();
//TODO 4:模拟定时器
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);//0秒后执行,每隔1000毫秒执行一次
}
6.圆形进度条对话框
private void hor_progress_dialog() {
//TODO 1:实例化对象
final ProgressDialog dialog=new ProgressDialog(this);
//TODO 2:设置属性
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置圆圈
dialog.setMax(100);
dialog.setMessage("正在下载");
//TODO 3:显示
dialog.show();
//TODO 4:模拟定时器
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);//0秒后执行,每隔1000毫秒执行一次
}
7.日期选择对话框
//日期选择对话框DatePickerDialog
private void date_dialog() {
/**
*
*
* @param context 上下文
* @param listener 监听器
* @param year 默认的年
* @param month 默认的月
* @param dayOfMonth 默认的日
*/
Calendar calendar=Calendar.getInstance();//日历对象
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
8.时间选择对话框
//时间对话框:TimePickerDialog
private void time_dialog() {
/**
* @param context 上下文
* @param listener 监听器
* @param hourOfDay 默认的时
* @param minute 默认的分钟
* @param is24HourView 是否采用24时禁止
*/
Calendar calendar=Calendar.getInstance();//日历对象
//TODO 1:实例化对象 TODO 2:show
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();
}
自定义对话框
1.思路:
1.自定义类继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义类的对象
2.代码
(1)xml布局:自定义对话框的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#11ffffff">
<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>
</RelativeLayout>
(2)自定义类继承dialog
public class DiyDialog 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 DiyDialog(@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 DiyDialog(@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.zidingyi_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();
}
}