1.写一个Dialog样式继承android:style/Theme.Dialog
<!--自定义Dialog的主题-->
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@color/dialogBg</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>
注:没有引入自定义主题可能会出现dialog大小异常,外部有系统dialog框...一些列问题。
2.写一个类继承Dialog
public class WithdrawDialog extends Dialog {
private ImageView iv_cancel;
private TextView tv_awardCount;
private TextView tv_cancel;
private TextView tv_confirm;
private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
public WithdrawDialog(@NonNull Context context) {
super(context);
}
public WithdrawDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected WithdrawDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
/**
* 设置取消按钮的显示内容和监听
* @param onNoOnclickListener
*/
public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) {
this.noOnclickListener = onNoOnclickListener;
}
/**
* 设置确定按钮的显示内容和监听
* @param onYesOnclickListener
*/
public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) {
this.yesOnclickListener = onYesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_withdraw);
//按空白处能否取消动画
setCanceledOnTouchOutside(true);
//初始化界面控件
initView();
//初始化界面控件的事件
setListener();
}
private void setListener() {
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
iv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
tv_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesClick();
}
}
});
}
public void setAwardCount(String awardCount) {
tv_awardCount.setText(awardCount);
}
private void initView() {
iv_cancel = (ImageView) findViewById(R.id.iv_cancel);
tv_awardCount = (TextView) findViewById(R.id.tv_awardCount);
tv_cancel = (TextView) findViewById(R.id.tv_cancel);
tv_confirm = (TextView) findViewById(R.id.tv_confirm);
}
/**
* 设置确定按钮和取消被点击的接口
*/
public interface onYesOnclickListener {
public void onYesClick();
}
public interface onNoOnclickListener {
public void onNoClick();
}
}
注:通过接口回掉可以在使用该dialog的类中设置监听事件。
3.在需要显示自定义dialog的地方写自己的显示方法和设置监听
private void showWithdrawDialog(String awardCount){
final WithdrawDialog dialog = new WithdrawDialog(this, R.style.MyDialog);
dialog.setNoOnclickListener(new WithdrawDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
dialog.dismiss();
}
});
dialog.setYesOnclickListener(new WithdrawDialog.onYesOnclickListener() {
@Override
public void onYesClick() {
Toast.makeText(WithdrawActivity.this, "调用提现接口", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
//放在show()方法之后否则空指针
dialog.setAwardCount(awardCount);
}
注:改变dialog的UI组件的内容需要在show()方法之后,否则空指针(UI组件尚未加载完成)。
4.效果如下图: