是/否对话框和生命周期
我确信这是一个基本问题,但我的研究没有产生任何有用的结果。我的新应用程序在某些情况下需要使用“是/否”对话框,但我不知道对话框如何适应应用程序生命周期。例如,我想创建一个方法来支持这种类型的构造:
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_POSITIVE 和 BUTTON_NEGATIVE 操作会对此做出响应,但我的问题是如何使用指示器返回,以便等待响应的代码将在 action1() 或 action2( ),取决于响应。
目前,我没有看到我的应用程序有任何方法来确定这一点,甚至也没有看到从该代码创建方法/函数的有效方法。所以我错过了生命周期中的一些重要部分。
我可以在哪里读到这方面的内容?当然,互联网上有大量关于这方面的信息,但对于我作为一个相对新手来说,这就像尝试从消防水龙带中喝水一样。
I'm sure this is a fundamental question, but my research yields nothing useful. My new application needs to use a Yes/No dialog under a few circumstances, and I'm not getting how dialogs fit into the application lifecycle. For example, I would like to create a method to support this type of construct:
if (yesNoAlert("Title", "Do you want to try again?") == true) {
action1();
} else {
action2();
}
The method would look something like this:
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();
}
... but as you can see, it is not finished - there are a number of things not quiet correct about it. The piece I'm missing is how to know that the dialog has finished... what method exists that can be utilized so that the application can pick up on the fact that the button has been pressed? Of course, the BUTTON_POSITIVE and BUTTON_NEGATIVE actions respond to that, but my question is how to return with an indicator, so that the code that's waiting for a response will pick up again at action1() or action2(), depending upon response.
At present, I do not see any way for my application to determine this, - nor even a valid way to make a method/function from that code. so I'm missing some vital piece from the lifecycle.
Where might I read up on this? Of course, there are volumes of information available on internet about this, but for me as a relative newbie it's like trying drink from a fire hose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将使需要采取的行动变得动态:
This will make the action that needs to be taken dynamic:
你可以这样做
You could do like this
您可以使用侦听器来实现此目的。正如 Android 文档中所述:
使用您需要支持的操作定义一个界面(
onDialogPositiveClick
和onDialogNegativeClick
)。公共类NoticeDialogFragment扩展DialogFragment {
}
使显示对话框的类实现您的界面。
公共类MainActivity扩展FragmentActivity
实现NoticeDialogFragment.NoticeDialogListener{
...
}
让您的对话框在正确的时刻调用这些方法(当检测到
setPositiveButton
或setNegativeButton
点击时)。公共类NoticeDialogFragment扩展DialogFragment {
...
<前><代码>@Override
公共对话框 onCreateDialog(Bundle savingInstanceState) {
// 构建对话框并设置按钮单击处理程序
AlertDialog.Builder 构建器 = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
公共无效onClick(DialogInterface对话框,int id){
// 将肯定按钮事件发送回宿主活动
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
公共无效onClick(DialogInterface对话框,int id){
// 将否定按钮事件发送回宿主活动
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
返回 builder.create();
}
}
参考 http://developer.android.com/guide/主题/ui/dialogs.html#PassingEvents
you can use a listener to achieve this. Like said in android documentation:
Define a interface with the actions you need to support (
onDialogPositiveClick
andonDialogNegativeClick
).public class NoticeDialogFragment extends DialogFragment {
}
Make the class that displays the dialog implements your interface.
public class MainActivity extends FragmentActivity
implements NoticeDialogFragment.NoticeDialogListener{
...
}
Make your dialog invoke these methods on the correct moment (when detect
setPositiveButton
orsetNegativeButton
click).public class NoticeDialogFragment extends DialogFragment {
...
}
Ref http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents