话不多说,直接上代码哈~
1、Dialog工具类
/**
* @Author Catherine
* @Description Custom common dialog.
* @Date 2021/5/27
**/
public class CustomCommonDialog extends Dialog {
/**
* 显示的标题
*/
private TextView dialog_tv_title;
/**
* 显示的图片
*/
private ImageView dialog_iv_image;
/**
* 显示的内容
*/
private TextView dialog_tv_content;
/**
* 确认和取消按钮
*/
private Button dialog_btn_negative, dialog_btn_positive;
/**
* 按钮之间的分割线
*/
private View dialog_view_column_line;
public CustomCommonDialog(Context context) {
super(context, R.style.CustomDialog);
}
/**
* 都是内容数据
*/
private String title;
private String content;
private String btnPositiveStr, btnNegativeStr;
private int imageResId = -1;
/**
* 底部是否只有一个按钮
*/
private boolean isSingle = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_custom_common_dialog);
//按空白处不能取消动画
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
refreshView();
//初始化界面控件的事件
initEvent();
}
/**
* 初始化界面的确定和取消监听器
*/
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
dialog_btn_positive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickBottomListener != null) {
onClickBottomListener.onPositiveClick();
}
}
});
//设置取消按钮被点击后,向外界提供监听
dialog_btn_negative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
if (onClickBottomListener != null) {
onClickBottomListener.onNegativeClick();
}
}
});
}
/**
* 初始化界面控件的显示数据
*/
private void refreshView() {
//如果用户自定了title和message
if (!TextUtils.isEmpty(title)) {
dialog_tv_title.setText(title);
dialog_tv_title.setVisibility(View.VISIBLE);
} else {
dialog_tv_title.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(content)) {
dialog_tv_content.setText(content);
}
//如果设置按钮的文字
if (!TextUtils.isEmpty(btnPositiveStr)) {
dialog_btn_positive.setText(btnPositiveStr);
} else {
dialog_btn_positive.setText("确定");
}
if (!TextUtils.isEmpty(btnNegativeStr)) {
dialog_btn_negative.setText(btnNegativeStr);
} else {
dialog_btn_negative.setText("取消");
}
if (imageResId != -1) {
dialog_iv_image.setImageResource(imageResId);
dialog_iv_image.setVisibility(View.VISIBLE);
} else {
dialog_iv_image.setVisibility(View.GONE);
}
/**
* 只显示一个按钮的时候隐藏取消按钮,回掉只执行确定的事件
*/
if (isSingle) {
dialog_view_column_line.setVisibility(View.GONE);
dialog_btn_negative.setVisibility(View.GONE);
} else {
dialog_btn_negative.setVisibility(View.VISIBLE);
dialog_view_column_line.setVisibility(View.VISIBLE);
}
}
@Override
public void show() {
super.show();
refreshView();
}
/**
* 初始化界面控件
*/
private void initView() {
dialog_btn_negative = (Button) findViewById(R.id.dialog_btn_negative);
dialog_btn_positive = (Button) findViewById(R.id.dialog_btn_positive);
dialog_tv_title = (TextView) findViewById(R.id.dialog_tv_title);
dialog_tv_content = (TextView) findViewById(R.id.dialog_tv_content);
dialog_iv_image = (ImageView) findViewById(R.id.dialog_iv_image);
dialog_view_column_line = findViewById(R.id.dialog_view_column_line);
}
/**
* 设置确定取消按钮的回调
*/
public OnClickBottomListener onClickBottomListener;
public CustomCommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
this.onClickBottomListener = onClickBottomListener;
return this;
}
public interface OnClickBottomListener {
/**
* 点击确定按钮事件
*/
void onPositiveClick();
/**
* 点击取消按钮事件
*/
void onNegativeClick();
}
public String getContent() {
return content;
}
public CustomCommonDialog setContent(String content) {
this.content = content;
return this;
}
public String getTitle() {
return title;
}
public CustomCommonDialog setTitle(String title) {
this.title = title;
return this;
}
public String getBtnPositiveStr() {
return btnPositiveStr;
}
public CustomCommonDialog setBtnPositiveStr(String btnPositiveStr) {
this.btnPositiveStr = btnPositiveStr;
return this;
}
public String getBtnNegativeStr() {
return btnNegativeStr;
}
public CustomCommonDialog setBtnNegativeStr(String btnNegativeStr) {
this.btnNegativeStr = btnNegativeStr;
return this;
}
public int getImageResId() {
return imageResId;
}
public boolean isSingle() {
return isSingle;
}
public CustomCommonDialog setSingle(boolean single) {
isSingle = single;
return this;
}
public CustomCommonDialog setImageResId(int imageResId) {
this.imageResId = imageResId;
return this;
}
}
2、layout_custom_common_dialog.xml布局文件
3、CustomDialog —— style.xml
4、@drawable/shape_rectangle_white
-
5、在Activity里使用
//初始化并弹出对话框方法
private void showDialog() {
CustomCommonDialog dialog = new CustomCommonDialog(this).setTitle("提示")
.setContent("悬浮窗权限将用于保持教师和学生间的通话。若不同意,将会影响部分功能的正常使用。\n请问是否同意我们向您获取该权限?")
.setBtnPositiveStr("同意并前往开启").setBtnNegativeStr("下次再说");
dialog.create();
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.setOnClickBottomListener(new CustomCommonDialog.OnClickBottomListener() {
@Override
public void onPositiveClick() {
if (checkIsHuaweiRom()) {
HuaweiFloatingPermissionUtils.applyPermission(MainActivity.this);
} else if (checkIsMiuiRom()) {
MiuiFloatingPermissionUtils.applyMiuiPermission(MainActivity.this);
} else if (checkIs360Rom()) {
QikuFloatingPermissionUtils.applyPermission(MainActivity.this);
} else if (checkIsMeizuRom()) {
MeizuFloatingPermissionUtils.applyPermission(MainActivity.this);
} else if (checkIsOppoRom()) {
OppoFloatingPermissionUtils.applyPermission(MainActivity.this);
} else {
requestAlertWindowPermission();
}
dialog.dismiss();
}
@Override
public void onNegativeClick() {
}
});
dialog.show();
//此处设置位置窗体大小,设置为手机屏幕宽度的5/6,注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果
dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this) / 6 * 5), LinearLayout.LayoutParams.WRAP_CONTENT);
}
感谢:https://www.jianshu.com/p/02cbde66a93e