我在想是否可以创建自定义基类/方法来创建对话框,但是在实时加载自定义操作。我不知道我是否很好地解释了我。
如果我有下一个来源:
public void showDialogCustom(String title, String content, String positiveText, String negativeText) {
getBaseActivity().hideMaterialDialog();
getBaseActivity().setDialog(new MaterialDialog.Builder(getActivity())
.title(title)
.content(content)
.positiveText(positiveText)
.negativeText(negativeText)
.canceledOnTouchOutside(false)
.cancelable(false)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//SOME ACTION!!!
}
})
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//SOME ACTION!!!
}
})
.show());
}
我想为正面或负面点击自定义操作。使用文本参数和动作调用类CustomDialog,如CustomDialog.showDialogCustom(params ...)。这可能吗?
谢谢。
答案 0 :(得分:2)
这可能会对你有所帮助。您可以根据需要创建自定义对话框布局。无论何时你想显示任何对话框,只需根据你的需要传递参数。在DialogButtonListener
中,您将获得正面和负面按钮的按钮点击事件。
public void displayDialog(final Context mContext, final String title, String msg, final String strPositiveBtn,
final String strNegativeBtn, final boolean isTitle, final boolean isNegativeBtn, final DialogButtonListener mDialogButtonListener) {
if (mContext != null && !((Activity) mContext).isFinishing()) {
if (TextUtils.isEmpty(msg)) {
msg = mContext.getString(R.string.alert_some_error);
}
final Dialog mDialog = new Dialog(mContext, R.style.StyleCommonDialog);
final WindowManager.LayoutParams wlmp = mDialog.getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER;
wlmp.width = WindowManager.LayoutParams.MATCH_PARENT;
wlmp.height = WindowManager.LayoutParams.WRAP_CONTENT;
mDialog.getWindow().setAttributes(wlmp);
mDialog.setCancelable(true);
mDialog.setCanceledOnTouchOutside(false);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.custom_common_dialog);
final TextView tvTitle = mDialog.findViewById(R.id.common_dialog_tvTitle);
final TextView tvMsg = mDialog.findViewById(R.id.common_dialog_tvMsg);
final TextView btnPositive = mDialog.findViewById(R.id.common_dialog_btnPositive);
final TextView btnNegative = mDialog.findViewById(R.id.common_dialog_btnNegative);
tvTitle.setText(title.trim());
tvMsg.setText(setFormattedHtmlString(msg.trim()));
if (isTitle) {
tvTitle.setVisibility(View.VISIBLE);
} else {
tvTitle.setVisibility(View.GONE);
}
btnPositive.setOnClickListener(new OnClickListenerWrapper(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
mDialogButtonListener.onPositiveButtonClicked();
}
}));
if (!TextUtils.isEmpty(strPositiveBtn.trim())) {
btnPositive.setText(strPositiveBtn.trim());
}
if (isNegativeBtn) {
btnNegative.setVisibility(View.VISIBLE);
btnNegative.setOnClickListener(new OnClickListenerWrapper(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
mDialogButtonListener.onNegativeButtonClicked();
}
}));
if (!TextUtils.isEmpty(strNegativeBtn.trim())) {
btnNegative.setText(strNegativeBtn.trim());
}
} else {
btnNegative.setVisibility(View.GONE);
}
mDialog.show();
}
}
DialogButtonListener接口是这样的:
public interface DialogButtonListener {
void onPositiveButtonClicked();
void onNegativeButtonClicked();
}