我有一个AlertDialogUtils
类生成AlertDialog
,以便在发生错误时可以从任何活动中调用它。问题是,我无法在finish()
方法中将createDialog()
作为onClickListener
拨打“关闭”按钮。
有什么想法可能吗?
AlertDialogUtils类代码:
public class AlertDialogUtils extends Dialog {
private Context mContext;
public AlertDialogUtils(Context context) {
super(context);
mContext = context;
}
public void CreateAlertDialog(String errorMessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(errorMessage)
.setCancelable(true)
.setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mContext.finish();
//error here. Intend to close the activtiy that created this dialog and has the error
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
// The dialog utils is outside an activity. Need to set owner
alert.show();
}
}
答案 0 :(得分:2)
请你这样试试吗? :
interface ICloseActivity {
void close();
}
class MyActivityToClose extends Activity implements ICloseActivity {
public void close() {
finish();
}
}
// -------
public class AlertDialogUtils extends Dialog {
private Context mContext;
private ICloseActivity mICloseActivity;
public AlertDialogUtils(Context context, ICloseActivity aActivity) {
super(context);
mContext = context;
mICloseActivity = aActivity;
}
public void CreateAlertDialog(String errorMessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(errorMessage)
.setCancelable(true)
.setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// mContext.finish();
//error here. Intend to close the activtiy that created this dialog and has the error
//TRY THIS please:
mICloseActivity.close();
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
// The dialog utils is outside an activity. Need to set owner
alert.show();
}
答案 1 :(得分:2)
不是在构造函数中传递Context
,而是传递Activity
。它继承了Context
,因此您可以在任何需要Context
的地方使用它;同时,您也可以在需要时拨打finish()
。
答案 2 :(得分:2)
感谢您的建议。遗憾的是,ICloseActivity
对我不起作用。我通过以下方式解决了完成活动和解除对话框的问题:
我按如下方式更改了CreateAlertDialog()
方法并创建了辅助函数exitActivity()
:
public void CreateAlertDialog(String title, String errorMessage, int alertType){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(title)
.setIcon(R.drawable.alert_icon)
.setMessage(errorMessage)
.setCancelable(false)
.setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss = true;
exitActivity();
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
alert.show();
}
public void exitActivity() {
this.getOwnerActivity().finish();
}
在调用此实用程序时,我只需设置dismiss侦听器并完成活动,因为它被解除。
dialog = new AlertDialogUtils(MyActivity.this);
dialog.setOwnerActivity(MyActivity.this);
dialog.CreateAlertDialog(getString(R.string.no_data),
getString(R.string.empty_table_message), R.id.list_view_alertType);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
@Override
public void onDismiss(DialogInterface arg0) {
finish();
}
});
通过这种方式,我可以根据需要处理关闭当前的活动。
此外,对于我不想关闭而不是致电exitActivity()
的活动,我致电dialog.cancel()
:
.setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
cancel();
}
});