我有很多“扩展片段”文件。
我想在AppUtility中设置警告Dialog,以共享使用的不同片段文件。
当按钮点击不同的片段时,他们必须调用saveAlertDialog。但我想覆盖setPositiveButton。
因为他们已经替换了差异片段,或者他们称之为完成活动。
如何在不同的片段文件中使用共享功能(saveAlertDialog)和覆盖?
我的应用实用程序文件包含以下代码:
public static AlertDialog saveAlertDialog( Context mContext ) {
// Dialog style
TextView tv = new TextView(mContext);
tv.setText(mContext.getString(R.string.diary_save_alert));
tv.setTextSize(30);
tv.setPadding(30, 10, 10, 10);
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(mContext, R.style.customAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(contextThemeWrapper);
builder.setCustomTitle(tv);
builder.setPositiveButton(mContext.getString(R.string.save), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// VVVVVVV it have problem.
//mContext.finish();
}
});
builder.setNegativeButton(mContext.getString(R.string.not_save), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
return dialog;
}
我的不同片段文件。如下所示:
AlertDialog dialog = AppUtils.saveAlertDialog(mContext);
但我需要覆盖setPositiveButton以在不同的片段文件中设置不同的内容。
替换为BFragment或Cfragment或完成活动。
fragment = new BFragment();
if (fragment != null) {
getFragmentManager().beginTransaction().replace(R.id.diary_frame_container, fragment, "BFragment").addToBackStack("BFragment").commit();
} else {
Log.e(TAG, "Error in creating fragment");
}
有谁知道如何设计模式?
非常感谢你。答案 0 :(得分:0)
在DialogUtils
课程中编写以下函数
public static void showMessageDialog(Context context, String title, String message, final DialogClickListener dialogBtnClickListener, String positiveButtonText, String negativeButtonText) {
AlertDialog.Builder alert = new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog));
alert.setIcon(R.drawable.app_icon_small);
if (!TextUtils.isEmpty(title)) {
alert.setTitle(title);
}
if (!TextUtils.isEmpty(message)) {
alert.setMessage(message);
}
if (!TextUtils.isEmpty(positiveButtonText)) {
alert.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogBtnClickListener != null) {
dialogBtnClickListener.onDone(null, true, null);
}
}
});
}
if (!TextUtils.isEmpty(negativeButtonText)) {
alert.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogBtnClickListener != null) {
dialogBtnClickListener.onDone(null, false, null);
}
}
});
}
alert.show();
}
public static interface DialogClickListener {
public void onDone(String stringMetadata, boolean isPositive, EditText editText);
}
<强>用法强>
现在在你的片段类中将它们用作
DialogUtils.showMessageDialog(getContext(), "title", "content", new DialogUtils.DialogClickListener() {
@Override
public void onDone(String stringMetadata, boolean isPositive, EditText editText) {
if (!isPositive) {
// if negative button is clicked
}else {
// positive button clicked
}
}, "Ok", "cancel");
修改强>
获取警报对话框实例您可以选择以下任一选项:
您可以将showMessageDialog()
的返回类型设置为Dialog
或使接口onDone()
具有对话框实例的额外参数(顺便说一下只要其中一个按钮被单击,对话框就会自动消失,因此额外的参数不是一个好主意。
对于第一个工作建议,您必须进行如下更改
由于cancel()
类中没有任何dismiss()
或AlertDialog.Builder
方法
因此,不是AlertDialog.Builder
使用AlertDialog
实例。
final AlertDialog alertDialog = alert.create();
alertDialog.show();
享受!