Android 是/否对话框和生命周期
我确信这是一个基本的问题,但我的研究没有产生任何有用的结果。我的新应用程序在某些情况下需要使用“是/否”对话框,我不知道对话框如何适应应用程序的生命周期。例如,我想创建一个方法来支持这种类型的构造:Android 是/否对话框和生命周期,android,dialog,onclick,lifecycle,Android,Dialog,Onclick,Lifecycle,我确信这是一个基本的问题,但我的研究没有产生任何有用的结果。我的新应用程序在某些情况下需要使用“是/否”对话框,我不知道对话框如何适应应用程序的生命周期。例如,我想创建一个方法来支持这种类型的构造: if (yesNoAlert("Title", "Do you want to try again?") == true) { action1(); } else { action2(); } 该方法将如下所示: private boolean yesNoAlert(String ti
if (yesNoAlert("Title", "Do you want to try again?") == true) {
action1();
} else {
action2();
}
该方法将如下所示:
private boolean yesNoAlert(String title, String message) {
final boolean returnValue;
DialogInterface.OnClickListener dialogClickListener = new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
returnValue = true;
break;
case DialogInterface.BUTTON_NEGATIVE:
returnValue=false;
break;
}
}
};
alertbox = new AlertDialog.Builder(this);
alertbox.setMessage(message)
.setTitle(title)
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener)
.show();
}
。。。但正如你所看到的,它还没有结束——有许多事情是不正确的。我缺少的是如何知道对话已经结束。。。有什么方法可以使用,以便应用程序能够在按钮已按下的情况下获取信息?当然,BUTTON_肯定和BUTTON_否定操作会对此作出响应,但我的问题是如何返回一个指示符,以便等待响应的代码会在action1()或action2()处再次拾取,具体取决于响应
目前,我看不到我的应用程序能够确定这一点的任何方法,甚至也看不到从该代码生成方法/函数的有效方法。所以我错过了生命周期中的一些重要部分
我可以在哪里读到这篇文章?当然,互联网上有大量关于这方面的信息,但对于我这个相对新手来说,这就像试着用消防水龙带喝水一样。你可以这样做
private boolean yesNoAlert(String title, String message) {
new AlertDialog.Builder(this).setMessage(message)
.setTitle(title)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { action1(); }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { action2(); }
})
.show();
}
这将使需要采取的行动变得动态:
private Thread actionToDo;
private void yesNoAlert(String title, String message)
{
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
case DialogInterface.BUTTON_POSITIVE:
actionToDo.start();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).setTitle(title).show();
}
您可以使用侦听器来实现这一点。正如android文档中所说:
onDialogNegativeClick
和onDialogNegativeClick
)
公共类NoticeDialogFragment扩展了DialogFragment{
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
...
}setPositiveButton
或setNegativeButton
单击时)
公共类NoticeDialogFragment扩展了DialogFragment{
}是的,我知道。但是代码中有几个地方可能会出现“是/否”问题。。。结果是所有的程序逻辑可能都隐藏在Yes/No对话框中。考虑可能发生的情况,例如:当用户名为能量{结果(结果));如果(结果=ANX错误){if(YESNORADURE(标题,“发生错误。继续尝试?”)){} { if(yESNOReAltt(title,)尝试其他东西?)){YuGeTeHeDRILE());}@Dennis问题是您无法从
yesNoAlert
返回真/假值。调用show()对话框上的
在用户单击“是”或“否”按钮之前不会被阻止。相反,稍后将在主ui线程上调用相应的DialogInterface.OnClickListener
。如果要将逻辑与AlertDialog分开,则可以将例如“是/否DialogInterface.OnClickListener”作为实例变量。@Dennis这样的过程性方法是行不通的。你必须将与用户交互的代码转换为基于事件的模型。谢谢你的提示。我的主要问题是,我在多年前在一个完全不同的范式下学习了编程,并且非常成功。我可以做到这一点,但一些再培训是必不可少的。我欣赏指定您的方向,并将继续搜索有关范例之间差异的信息。将returnValue变量设置为“类变量”而不是“局部方法变量”。这样您就可以从onClick方法中设置“returnValue”。
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
...
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog's negative button
...
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
return builder.create();
}