目录
CustomDialog.java文件
package widget;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.test.R;
public class CustomDialog extends Dialog implements View.OnClickListener {
private TextView mTitle,mMessage,mCancer,mConfirm;
private String title,message,cancel,confirm;
private IOnCancelListener cancelListener;
private IOnConfirmListener confirmListener;
//Dialog的构造方法,继承Dialog需要写出其相应构造方法
public CustomDialog(@NonNull Context context) {
super(context);
}
public CustomDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
//public CustomDialog返回值是CustomDialog,不是void,所以使用CustomDialog自定义类型时只需要 .方法
//在private String title,message,cancel,confirm;中右键,Generate,setter中快捷构建相应方法
public CustomDialog setTitle(String title) {
this.title = title;
//可以使用return this,来返回某个类的引用。此时这个this关键字就代表类的名称,即CustomDialog
return this;
}
public CustomDialog setMessage(String message) {
this.message = message;
return this;
}
public CustomDialog setCancel(String cancel,IOnCancelListener listener) {
this.cancel = cancel;
this.cancelListener = listener;
return this;
}
public CustomDialog setConfirm(String confirm,IOnConfirmListener listener) {
this.confirm = confirm;
this.confirmListener = listener;
return this;
}
//重写onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dialog);
//设置宽度
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
Point size = new Point();
d.getSize(size);
p.width = (int)(size.x*0.9);//设置diglog的宽度是手机屏幕宽度的80%
getWindow().setAttributes(p);
mTitle = (TextView) findViewById(R.id.tv_title);
mMessage = (TextView) findViewById(R.id.tv_message);
mCancer = (TextView) findViewById(R.id.tv_cancel);
mConfirm = (TextView) findViewById(R.id.tv_confirm);
//使用TextUtils.isEmpty(),用来判断字符串是否为空
if(!TextUtils.isEmpty(title)){
mTitle.setText(title);
}
if(!TextUtils.isEmpty(message)){
mMessage.setText(message);
}
if(!TextUtils.isEmpty(cancel)){
mCancer.setText(cancel);
}
if(!TextUtils.isEmpty(confirm)){
mConfirm.setText(confirm);
}
mCancer.setOnClickListener(this);
mConfirm.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.tv_cancel:
if(cancelListener != null){
cancelListener.onCancel(this);
}
dismiss();
break;
case R.id.tv_confirm:
if(confirmListener != null){
confirmListener.onConfirm(this);
}
dismiss();
break;
}
}
public interface IOnCancelListener{
void onCancel(CustomDialog dialog);
}
public interface IOnConfirmListener{
void onConfirm(CustomDialog dialog);
}
}
MainActivity.java文件
package com.example.test;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import widget.CustomDialog;
public class MainActivity extends AppCompatActivity {
private Button mBtnDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnDialog = (Button) findViewById(R.id.btn_custom_dialog);
mBtnDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//CustomDialog自定义对话框定义
//实例化CustomDialog类,其方法在CustomDialog.java文件中实现
CustomDialog customDialog = new CustomDialog(MainActivity.this);
customDialog.setTitle("提示")
.setMessage("确认删除此项?")
.setCancel("取消", new CustomDialog.IOnCancelListener() {
@Override
public void onCancel(CustomDialog dialog) {
Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show();
}
}).setConfirm("确认",new CustomDialog.IOnConfirmListener() {
@Override
public void onConfirm(CustomDialog dialog) {
Toast.makeText(MainActivity.this, "已删除", Toast.LENGTH_SHORT).show();
}
}).show();
}
});
}
}
layout_dialog.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@drawable/bg_dialog">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:text="提示"
android:textStyle="normal"
android:layout_marginTop="20sp"/>
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:text="删除?"
android:layout_marginTop="20sp"
android:layout_marginBottom="20dp"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#989589"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消"
android:textSize="20sp"
android:textColor="#00EEEE"
android:gravity="center"/>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#989589"/>
<TextView
android:id="@+id/tv_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="确定"
android:textSize="20sp"
android:textColor="#00EEEE"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
activity_main.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">
<Button
android:id="@+id/btn_custom_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义Dialog"
android:textAllCaps="false"/>
bg_dialog.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="20dp"/>
</shape>