你可以做的是做一个通用的功能,显示一个对话框。在函数的参数中,您可以传递上下文,图像和背景颜色本身。把它作为一个单例,并从你想要显示对话框的地方调用它。调用一个函数将会很容易。我将一个对话框添加到监听器中。
public void showErrorDialogWithListener(Activity activity, String message, final View.OnClickListener onClickListener) {
try {
try {
if (errorDialog != null && errorDialog.isShowing()) {
errorDialog.dismiss();
}
} catch (Exception e) {
}
errorDialog = new Dialog(activity, R.style.DialogTheme);
errorDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
errorDialog.getWindow().getAttributes().windowAnimations = R.style.Animations_LoadingDialogFade;
errorDialog.setContentView(R.layout.dialog_error);
WindowManager.LayoutParams layoutParams = errorDialog.getWindow().getAttributes();
errorDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layoutParams.dimAmount = Constants.dialogDimAmount;
errorDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
errorDialog.getWindow().setAttributes(layoutParams);
errorDialog.setCancelable(true);
errorDialog.setCanceledOnTouchOutside(true);
LinearLayout mParentRl = (LinearLayout) errorDialog.findViewById(R.id.parentRl);
BaseActivity.overrideFonts(activity, mParentRl);
Button mBtnOk = (Button) errorDialog.findViewById(R.id.btnOk);
TextView messageTv = (TextView) errorDialog.findViewById(R.id.messageTv);
messageTv.setText(message);
mBtnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClickListener.onClick(view);
errorDialog.dismiss();
}
});
errorDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
这是一个只有一个按钮的对话框。希望这可以帮助。