如何在AlertDialog中设置AlertDialog.finish()?
How to set AlertDialog.finish() in the AlertDialog?
我目前正在使用需要使用大量 AlertDialogs 的应用程序。我目前在这里编写了一个基本的代码:
protected void StopButton () {
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
finish();
}
});
StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
finish();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
}
停止按钮起作用,当我调用它时对话框出现。然而,finish();功能不起作用。
经过审查,我发现 finish();没有完成对话框,而是整个应用程序。我知道我需要在里面放一个 AlertDialog.cancel。
问题是这样的:如您所见,AlertDialog 仅在 StopDialog 完成后创建。
如何在 StopDialog 完成之前设置 AlertDialog.finish()?
AlertDialog
没有这样的方法 finish()
。 finish()
引用 Activity
class 并将完成当前 Activity
。您应该改为使用 dismiss()
.
您可以使用像 dialog.dismiss()
这样的 DialogInterface
对象来关闭 AlertDialog
。
您可以阅读文档以获得更广泛的想法:)
将 finish()
替换为 dialog.dismiss()
,如下所示:
AlertDialog.Builder StopDialog = new AlertDialog.Builder(TestActivity.this);
StopDialog.setTitle("Title");
StopDialog.setMessage("Stop");
StopDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
dialog.dismiss();
}
});
StopDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
dialog.dismiss();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
我目前正在使用需要使用大量 AlertDialogs 的应用程序。我目前在这里编写了一个基本的代码:
protected void StopButton () {
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
finish();
}
});
StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
finish();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
}
停止按钮起作用,当我调用它时对话框出现。然而,finish();功能不起作用。
经过审查,我发现 finish();没有完成对话框,而是整个应用程序。我知道我需要在里面放一个 AlertDialog.cancel。
问题是这样的:如您所见,AlertDialog 仅在 StopDialog 完成后创建。
如何在 StopDialog 完成之前设置 AlertDialog.finish()?
AlertDialog
没有这样的方法 finish()
。 finish()
引用 Activity
class 并将完成当前 Activity
。您应该改为使用 dismiss()
.
您可以使用像 dialog.dismiss()
这样的 DialogInterface
对象来关闭 AlertDialog
。
您可以阅读文档以获得更广泛的想法:)
将 finish()
替换为 dialog.dismiss()
,如下所示:
AlertDialog.Builder StopDialog = new AlertDialog.Builder(TestActivity.this);
StopDialog.setTitle("Title");
StopDialog.setMessage("Stop");
StopDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
dialog.dismiss();
}
});
StopDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
dialog.dismiss();
}
});
AlertDialog alert = StopDialog.create();
alert.show();