New file |
| | |
| | | package com.hdl.photovoltaic.widget; |
| | | |
| | | import android.app.Dialog; |
| | | import android.content.Context; |
| | | import android.os.Bundle; |
| | | import android.text.TextUtils; |
| | | import android.view.View; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.databinding.LoadingConfirmBinding; |
| | | |
| | | /** |
| | | * 确认框 |
| | | */ |
| | | public class ConfirmationDialog extends Dialog { |
| | | public ConfirmationDialog(@NonNull Context context) { |
| | | super(context, R.style.Custom_AlertDialog); |
| | | this.mContext = context; |
| | | } |
| | | |
| | | private final Context mContext; |
| | | private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器 |
| | | private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器 |
| | | private LoadingConfirmBinding viewBinding; |
| | | private String titleStr, contentStr, yesStr, noStr; |
| | | |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | viewBinding = LoadingConfirmBinding.inflate(getLayoutInflater()); |
| | | setContentView(viewBinding.getRoot()); |
| | | // setCancelable(true);//系统后退可以取消 |
| | | //空白处不能取消动画 |
| | | setCanceledOnTouchOutside(false); |
| | | //初始化界面控件 |
| | | initView(); |
| | | //初始化界面数据 |
| | | initData(); |
| | | //初始化界面控件的事件 |
| | | initEvent(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 自定义"标题"文本 |
| | | * |
| | | * @param title 内容 |
| | | */ |
| | | public void setTitle(String title) { |
| | | if (TextUtils.isEmpty(title)) { |
| | | return; |
| | | } |
| | | titleStr = title; |
| | | if (viewBinding != null) { |
| | | viewBinding.loadingConfirmationTitleTv.setText(titleStr); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 自定义"确认"文本 |
| | | * |
| | | * @param confirm 内容 |
| | | */ |
| | | public void setConfirmation(String confirm) { |
| | | if (TextUtils.isEmpty(confirm)) { |
| | | return; |
| | | } |
| | | yesStr = confirm; |
| | | |
| | | |
| | | if (viewBinding != null) { |
| | | viewBinding.dialogConfirmTv.setText(yesStr); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 自定义"取消"文本 |
| | | * |
| | | * @param cancel 内容 |
| | | */ |
| | | public void setCancel(String cancel) { |
| | | if (TextUtils.isEmpty(cancel)) { |
| | | return; |
| | | } |
| | | noStr = cancel; |
| | | if (viewBinding != null) { |
| | | viewBinding.dialogCancelTv.setText(noStr); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 自定义"内容"文本 |
| | | * |
| | | * @param content 内容 |
| | | */ |
| | | public void setContentText(String content) { |
| | | if (TextUtils.isEmpty(content)) { |
| | | return; |
| | | } |
| | | contentStr = content; |
| | | if (viewBinding != null) { |
| | | viewBinding.loadingConfirmationContentTv.setText(contentStr); |
| | | } |
| | | } |
| | | |
| | | private void initEvent() { |
| | | viewBinding.dialogCancelLy.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | if (noOnclickListener != null) { |
| | | noOnclickListener.Cancel(); |
| | | } |
| | | } |
| | | }); |
| | | viewBinding.dialogConfirmLy.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | if (yesOnclickListener != null) { |
| | | yesOnclickListener.Confirm(); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化界面控件的显示数据 |
| | | */ |
| | | private void initData() { |
| | | //如果用户自定了title和message |
| | | if (!TextUtils.isEmpty(titleStr)) { |
| | | viewBinding.loadingConfirmationTitleTv.setText(titleStr); |
| | | } |
| | | if (!TextUtils.isEmpty(contentStr)) { |
| | | viewBinding.loadingConfirmationContentTv.setText(contentStr); |
| | | } |
| | | //如果设置按钮文字 |
| | | if (!TextUtils.isEmpty(yesStr)) { |
| | | viewBinding.dialogConfirmTv.setText(yesStr); |
| | | } |
| | | if (!TextUtils.isEmpty(noStr)) { |
| | | viewBinding.dialogCancelTv.setText(noStr); |
| | | } |
| | | } |
| | | |
| | | private void initView() { |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 设置取消按钮监听 |
| | | * |
| | | * @param onNoOnclickListener - |
| | | */ |
| | | public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) { |
| | | if (onNoOnclickListener != null) { |
| | | this.noOnclickListener = onNoOnclickListener; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置确定按钮监听 |
| | | * |
| | | * @param yesOnclickListener - |
| | | */ |
| | | public void setYesOnclickListener(onYesOnclickListener yesOnclickListener) { |
| | | if (yesOnclickListener != null) { |
| | | this.yesOnclickListener = yesOnclickListener; |
| | | } |
| | | |
| | | } |
| | | |
| | | public interface onNoOnclickListener { |
| | | void Cancel(); |
| | | |
| | | } |
| | | |
| | | public interface onYesOnclickListener { |
| | | void Confirm(); |
| | | } |
| | | } |