public class YesOrNoDialog extends Dialog implements View.OnClickListener {
ImageView iv_close;//关闭图标
TextView tv_title;//标题
TextView tv_content;//内容
TextView tv_cancle;//取消按钮
TextView tv_confirm;//确定按钮
private Context context;
private OnAction onAction;//动作
private boolean touchOutsideCancle = true;//点击空白对话框是否消失
private String titleStr = "温馨提示";//对话框标题
private String contentStr = "是否确认?";//对话框内容
private String cancleStr = "取消";//取消按钮文字
private String confirmStr = "确认";//确定按钮文字
public YesOrNoDialog(
@NonNull Context context,
boolean touchOutsideCancle,
String titleStr,
String contentStr,
String cancleStr,
String confirmStr,
OnAction onAction) {
super(context, R.style.CumstomDialog);
this.context = context;
this.touchOutsideCancle = touchOutsideCancle;
this.titleStr = titleStr;
this.contentStr = contentStr;
this.cancleStr = cancleStr;
this.confirmStr = confirmStr;
this.onAction = onAction;
init();
}
private void init() {
initView();
initParam();
settingWindow();
}
private void initParam() {
tv_title.setText(titleStr);
tv_content.setText(contentStr);
tv_cancle.setText(cancleStr);
tv_confirm.setText(confirmStr);
setCanceledOnTouchOutside(touchOutsideCancle);
iv_close.setVisibility(touchOutsideCancle ? View.VISIBLE : View.INVISIBLE);//如果不同意
}
private void initView() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_yes_no, null);
setContentView(view);
iv_close = findViewById(R.id.iv_close);
tv_title = findViewById(R.id.tv_title);
tv_content = findViewById(R.id.tv_content);
tv_cancle = findViewById(R.id.tv_cancle);
tv_confirm = findViewById(R.id.tv_confirm);
iv_close.setOnClickListener(this);
tv_cancle.setOnClickListener(this);
tv_confirm.setOnClickListener(this);
}
private void settingWindow() {
Window dialogWindow = getWindow();
assert dialogWindow != null;
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高度
lp.width = (int) (d.widthPixels * 0.9); // 宽度设置为屏幕的0.9
dialogWindow.setAttributes(lp);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_close:
dismiss();
break;
case R.id.tv_cancle:
onAction.onCancleClicked(this);
break;
case R.id.tv_confirm:
onAction.onConfirmClicked(this);
break;
default:
break;
}
}
public interface OnAction {
void onCancleClicked(YesOrNoDialog yesOrNoDialog);
void onConfirmClicked(YesOrNoDialog yesOrNoDialog);
}
}
<?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="wrap_content"
android:background="@drawable/yesnodialog_bg"
android:orientation="vertical"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:id="@+id/iv_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="@drawable/icon_close" />
</LinearLayout>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="操作确认"
android:textColor="@color/appColor"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="确定参加?" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<TextView
android:id="@+id/tv_cancle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/gray_bt_bg"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="取消"
android:textColor="#fff"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_confirm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:background="@drawable/blue_bt_bg"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="确认"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<style name="CumstomDialog" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:backgroundDimAmount">0.7</item><!--黑色透明度-->
<item name="android:windowBackground">@color/transparent</item><!--背景透明-->
<item name="android:backgroundDimEnabled">true</item><!--模糊-->
<item name="android:windowContentOverlay">@null</item>
</style>