**Dialog对话框和自定义对话框**
常用对话框
1.普通对话框
public void putong(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setIcon(R.drawable.ic_launcher_background);
builder.setMessage("文本");
//确认按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
2.单选对话框
public void danxuan(View view) {
//1.构造者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//2.设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图片
//设置标题
builder.setTitle("单选");
//确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
}
});
//设置单选列表
final String[] index = new String[] {"女子","男子","孩子"};
builder.setSingleChoiceItems(index, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你抓住了"+index[i], Toast.LENGTH_SHORT).show();
}
});
//3.使用构造者创建对话框
AlertDialog dialog = builder.create();
//4.显示 不show()不显示结果
dialog.show();
}
3.多选对话框
public void duoxuan(View view) {
//1.创建构造者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("多选");
final String[] strings = {"瑶瑶","刺激","得劲"};
final boolean[] b = {true,true,false};
//点击确定
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
for (int j = 0; j <b.length ; j++) {
if (b[j]){
Toast.makeText(MainActivity.this, strings[j], Toast.LENGTH_SHORT).show();
}
}
}
});
//点击取消
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(strings, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
}
});
//显示
AlertDialog dialog = builder.create();
dialog.show();
}
4.自定义对话框
1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图uuuuuuU"
></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/tytyty"
android:id="@+id/img"
></ImageView>
</LinearLayout>
2.java代码
@SuppressLint("InflateParams")
public void tupian(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
//确认按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
}
});
View inflate = LayoutInflater.from(this).inflate(R.layout.dol_layout2, null);
ImageView img = inflate.findViewById(R.id.img);
builder.setView(inflate);
final AlertDialog dialog = builder.create();
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
5.水平进度条对话框
public void shuiping(View view) {
//实例化对象
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//水平央视
progressDialog.setIcon(R.drawable.ic_launcher_background);
progressDialog.setMax(100);//设置最大值
progressDialog.setMessage("正在下载");//提示
//3.显示
progressDialog.show();
//计时器
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int anInt = 0;
@Override
public void run() {
progressDialog.setProgress(anInt+=10);
if (anInt==100){
progressDialog.dismiss();//消失
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, anInt+"%下载完成", Toast.LENGTH_SHORT).show();
}
});
//清除
progressDialog.cancel();
}
}
},0,1000);
}
6.圆形进度条对话框
public void yuanxing(View view) {
//1.构造者
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);//水平央视
progressDialog.setMax(100);//最大值
progressDialog.setMessage("正在下载");
//显示
progressDialog.show();
//计时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int index = 0;
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress(index+=10);
progressDialog.setMessage("下载中"+index);
if (index==100){
progressDialog.dismiss();//消失
timer.cancel();//清除
}
//吐司进度
Toast.makeText(MainActivity.this, index+"%下载完成", Toast.LENGTH_SHORT).show();
}
});
}
},0,1000);
}
7.日期选择选择对话
@RequiresApi(api = Build.VERSION_CODES.N)
public void date(View view) {
/**
* @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 datePicker, int i, int i1, int i2) {
Toast.makeText(MainActivity.this, i+"年"+i1+"月"+i2+"日", Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
8.时间选择对话框
@RequiresApi(api = Build.VERSION_CODES.N)
public void time(View view) {
/**
* @param context 上下文
* @param listener 监听器
* @param hourOfDay 默认的时
* @param minute 默认的分钟
* @param is24HourView 是否采用24时禁止
*/
Calendar instance = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
Toast.makeText(MainActivity.this, i+"小时"+i1+"分", Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MARCH),true).show();
}
自定义对话框
思路:
1.自定义类继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义类的对象
代码:
1.自定义对话框布局
<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>
代码
package com.example.a180811901;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
public class Dol extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
public Dol(@NonNull Context context) {
super(context);
}
private String yesstr;
public void setYesstr(String yesstr) {
this.yesstr = yesstr;
}
private String nostr;
public void setNostr(String nostr) {
this.nostr = nostr;
}
private String titlestr;
public void setTitlestr(String titlestr) {
this.titlestr = titlestr;
}
//2.定义变量
public YesOnclickListener yesOnclickListener;
//1.定义接口
public interface YesOnclickListener{
void click();
}
//3
public void setYesOnclickListener(YesOnclickListener yesOnclickListener) {
this.yesOnclickListener = yesOnclickListener;
}
//2.定义变量
private NoOnclickListener noOnclickListener;
//1.定义接口
public interface NoOnclickListener{
void click();
}
//3.
public void setNoOnclickListener(NoOnclickListener noOnclickListener) {
this.noOnclickListener = noOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
inview();
}
public void inview(){
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
if (titlestr!=null){
title.setText(titlestr);
}
if (nostr!=null){
no.setText(nostr);
}
if (yesstr!=null){
yes.setText(yesstr);
}
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yesOnclickListener.click();
dismiss();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yesOnclickListener.click();
dismiss();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
noOnclickListener.click();
dismiss();
}
});
}
}
主页面代码
public void zidingyi(View view) {
Dol dol = new Dol(this);
dol.setTitle("标题");
dol.setYesOnclickListener(new Dol.YesOnclickListener() {
@Override
public void click() {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
dol.setNoOnclickListener(new Dol.NoOnclickListener() {
@Override
public void click() {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
dol.show();
}