一.常用对话框
1.普通对话框
在布局里,仅仅写了一个按钮用来触发对话框
<Button
android:text="普通对话框"
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
以下是Activity里的对话框创建及操作代码
but = findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1.创建 构建者模式
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//普通对话框
//2.设置内容
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();
}
});
//3.创建
AlertDialog dialog = builder.create();
//4.展示
dialog.show();
}
});
效果展示
2.单选对话框
在普通对话框的基础上,实现对于数据的单选操作
同样,做一个按钮仅作触发
<Button
android:text="单选对话框"
android:id="@+id/but_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
以下是Activity的代码
but_2 = findViewById(R.id.but_2);
but_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder single = new AlertDialog.Builder(MainActivity.this);//单选对话框
//2.设置内容
single.setIcon(R.mipmap.ic_launcher);//设置图标
single.setTitle("提示");//设置标题
final String[] msg = new String[]{"java", "php", "c++"};
//数据源 默认选中的下标 监听事件
single.setSingleChoiceItems(msg, 0, new DialogInterface.OnClickListener() {//设置数据
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "" + msg[which], Toast.LENGTH_SHORT).show();
}
});
single.setPositiveButton("确定", new DialogInterface.OnClickListener() {//设置确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
single.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
//3.创建
AlertDialog dialog = single.create();
//4.展示
dialog.show();
}
});
在AlertDialog对象中调用setSingleChoiceItems方法设置数据
其中的三个参数分别为:数据源,默认选中的下标以及监听事件
效果展示
3.多选对话框
布局中
<Button
android:text="多选对话框"
android:id="@+id/but_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Activity中的java代码
but_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder check = new AlertDialog.Builder(MainActivity.this);//多选对话框
//2.设置内容
check.setIcon(R.mipmap.ic_launcher);//设置图标
check.setTitle("提示");//设置标题
final String[] msg = new String[]{"java", "php", "c++"};
final boolean[] bols = new boolean[]{false, true, false};
check.setMultiChoiceItems(msg, bols, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
for (int i = 0; i < bols.length; i++) {
if (isChecked) {
Toast.makeText(MainActivity.this, "" + msg[which], Toast.LENGTH_SHORT).show();
}
}
}
});
check.setPositiveButton("确定", new DialogInterface.OnClickListener() {//设置确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
check.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
//3.创建
AlertDialog dialog = check.create();
//4.展示
dialog.show();
}
});
其中的setMultiChoiceItems方法需要的三个参数分别为:数据源,一个boolean类型的数组作为默认是否选中,一个点击监听器
效果展示
4.自定义对话框
通过自己写出布局设置到对话框中
形成自定义对话框
以下自定义布局
<TextView
android:text="云想衣裳花想容"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
一个按钮用来触发
<Button
android:text="自定义对话框"
android:id="@+id/but_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
以下是Activity中的代码
but_4 = findViewById(R.id.but_4);
but_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);//自定义对话框
//2.设置内容
myDialog.setIcon(R.mipmap.ic_launcher);//设置图标
myDialog.setTitle("提示");//设置标题
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);//视图解析器
View view = inflater.inflate(R.layout.dia_content, null);
myDialog.setView(view);//将自己自定义的布局设置进去
myDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {//设置确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
myDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
//3.创建
AlertDialog dialog = myDialog.create();
//4.展示
dialog.show();
}
});
效果展示
5.进度对话框
对话框中以进度条或圆形模糊展示
用于触发的按钮
<Button
android:text="进度对话框"
android:id="@+id/but_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在Activity中的代码
其中自定义的计时任务用来模拟网络下载
but_5 = findViewById(R.id.but_5);
but_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);//进度对话框
//2.设置内容
progressDialog.setIcon(R.mipmap.ic_launcher);//设置图标
progressDialog.setTitle("提示");//设置标题
progressDialog.setMessage("正在下载");
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//水平进度
// progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//模糊进度
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i = 0;
@Override
public void run() {
progressDialog.setProgress(i);
if (progressDialog.getProgress()==progressDialog.getMax()){//进度完成
timer.cancel();//取消计时
progressDialog.dismiss();//隐藏对话框
}
i+=10;
}
}, 0, 1000);
//设置确定按钮
progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
//3.展示
progressDialog.show();
}
});
效果展示
6.日期选择对话框
按钮
<Button
android:text="日期对话框"
android:id="@+id/but_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后代码
calendar 是用来获取当前日期时间等的对象
but_6 = findViewById(R.id.but_6);
final Calendar calendar = Calendar.getInstance();//获取当前日期
but_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//日期选择对话框
DatePickerDialog date = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year + ":" + month + ":" + dayOfMonth, Toast.LENGTH_SHORT).show();
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//展示
date.show();
}
});
效果展示
7.时间选择对话框
按钮
<Button
android:text="时间对话框"
android:id="@+id/but_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
代码
OnTimeSetListener中的第四个参数用来设置是否以24小时制展示
true为是24小时制
but_7 = findViewById(R.id.but_7);
but_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//时间选择对话框
TimePickerDialog time = new TimePickerDialog(MainActivity.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);
//展示
time.show();
}
});
效果展示
二.完全自定义对话框
完全自定义对话框,通过自己自定义类继承Dialog,然后在自定义类里边完成各种操作
首先有一个自定义的布局展示Dialog
<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>
再自定义自己的Dialog类去继承
public class MyDialog extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
private String titleStr;
private String messageStr;
private String yesStr;
private String noStr;
public MyDialog(Context context) {
super(context);
}
private YesOnclickListener yesOnclickListener;
private NoOnclickListener noOnclickListener;
public interface YesOnclickListener {
void OnClick();
}
public interface NoOnclickListener {
void OnClick();
}
public void SetYesOnclickListener(String yesStr, YesOnclickListener yesOnclickListener) {
this.yesStr = yesStr;
this.yesOnclickListener = yesOnclickListener;
}
public void SetNoOnclickListener(String noStr, NoOnclickListener noOnclickListener) {
this.noStr = noStr;
this.noOnclickListener = noOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydialog);
initViews();
if (titleStr != null) {
title.setText(titleStr);
}
if (messageStr != null) {
message.setText(messageStr);
}
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesOnclickListener.OnClick();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noOnclickListener.OnClick();
}
});
}
public void setTitleStr(String titleStr) {
this.titleStr = titleStr;
}
public void setmessageStr(String messageStr) {
this.messageStr = messageStr;
}
private void initViews() {
title = findViewById(R.id.title);
message = findViewById(R.id.message);
no = findViewById(R.id.no);
yes = findViewById(R.id.yes);
}
}
其中使用了接口回调 可以在MainActivity中去重写两个按钮的方法然后自己写各种操作
以下是效果
要开心加油