<small id='csQbV'></small><noframes id='csQbV'>

      1. <tfoot id='csQbV'></tfoot>
      2. <legend id='csQbV'><style id='csQbV'><dir id='csQbV'><q id='csQbV'></q></dir></style></legend>
        • <bdo id='csQbV'></bdo><ul id='csQbV'></ul>
      3. <i id='csQbV'><tr id='csQbV'><dt id='csQbV'><q id='csQbV'><span id='csQbV'><b id='csQbV'><form id='csQbV'><ins id='csQbV'></ins><ul id='csQbV'></ul><sub id='csQbV'></sub></form><legend id='csQbV'></legend><bdo id='csQbV'><pre id='csQbV'><center id='csQbV'></center></pre></bdo></b><th id='csQbV'></th></span></q></dt></tr></i><div id='csQbV'><tfoot id='csQbV'></tfoot><dl id='csQbV'><fieldset id='csQbV'></fieldset></dl></div>
      4. Android 确认对话框返回 true 或 false

        时间:2023-08-31
        <legend id='jeWql'><style id='jeWql'><dir id='jeWql'><q id='jeWql'></q></dir></style></legend>
          <i id='jeWql'><tr id='jeWql'><dt id='jeWql'><q id='jeWql'><span id='jeWql'><b id='jeWql'><form id='jeWql'><ins id='jeWql'></ins><ul id='jeWql'></ul><sub id='jeWql'></sub></form><legend id='jeWql'></legend><bdo id='jeWql'><pre id='jeWql'><center id='jeWql'></center></pre></bdo></b><th id='jeWql'></th></span></q></dt></tr></i><div id='jeWql'><tfoot id='jeWql'></tfoot><dl id='jeWql'><fieldset id='jeWql'></fieldset></dl></div>

          <small id='jeWql'></small><noframes id='jeWql'>

              <bdo id='jeWql'></bdo><ul id='jeWql'></ul>
            • <tfoot id='jeWql'></tfoot>
                  <tbody id='jeWql'></tbody>

                  本文介绍了Android 确认对话框返回 true 或 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  似乎没有简单的方法让警报对话框返回一个简单的值.
                  此代码不起作用(answer 变量无法从侦听器中设置,实际上它甚至无法编译)

                  It seems to be there is no easy way to get an Alert dialog to return a simple value.
                  This code does not work (the answer variable cannot be set from within the listener, in fact it does not even compile)

                  public static boolean Confirm(Context context) {
                      boolean answer;
                      AlertDialog dialog = new AlertDialog.Builder(context).create();
                      dialog.setTitle("Confirmation");
                      dialog.setMessage("Choose Yes or No");
                      dialog.setCancelable(false);
                      dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int buttonId) {
                              answer = true;
                          }
                      });
                      dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int buttonId) {
                              answer = false;
                          }
                      });
                      dialog.setIcon(android.R.drawable.ic_dialog_alert);
                      dialog.show();
                      return answer;
                  }
                  

                  注意:方法是自包含的,即它不依赖于变量或外部构造,这一点很重要.只需调用它并得到你的答案,无论是真是假.

                  NOTE: It is important that the method is self contained, i.e., it does not depend on variables or constructs external to it. Just call it and get your answer, true or false.

                  那么,该怎么办?这个返回 truefalse 的简单愿望似乎比它应得的要复杂得多.

                  So, what to do? This simple wish of returning true or false seems to be much more complicated than it deserves.

                  另外,setButton 方法具有以下形式:

                  Also, the setButton method has the form:

                  dialog.setButton(int buttonId, String buttonText, Message msg)
                  

                  但不清楚如何使用,消息发送到哪里,发给谁,使用哪个处理程序?

                  But it is not clear how to use it, where is the meesage sent to, to whom, which handler is used?

                  推荐答案

                  好吧,我要说我对自己很满意,因为我自己找到了一个简单的答案!
                  但事实是,虽然我找到了一种返回值的方法(如下所示).没有用.

                  Well, I was going to say that I am very pleased with myself because I found a simple answer, all by myself!
                  But the truth is that although I find a way to return a value (which I show below). It is of no use.

                  真正的问题是我想要一个同步对话框,该对话框在 dialog.show() 之后恢复您的代码之前等待用户回答.
                  Android 中没有这样的野兽.所有对话框都是异步的,所以 dialog.show() 只在某个队列中发布对话框(我认为)并继续.因此,您无法及时得到答案.

                  The real problem is I wanted a synchronous Dialog, a dialog that waits for the user to answer before resuming your code after dialog.show().
                  There is no such beast in Android. All dialogs are asynchronous, so dialog.show() only posts the dialog in some queue (I think) and continues. Thus you don't get your answer in time.

                  尽管它的所有价值(没有),您将在下面找到如何在构建对话框的方法中设置一个值.也许这种技术还有其他用途,与对话生命周期无关.

                  For all its worth (nothing) below you'll find how to set a value inside the method that builds the dialog. Maybe there are other uses for this technique, not related to the dialog lifecycle.







                  为了提供一些相关信息,我会说如果你替换

                  To give some related info, I'll say that if you replace

                  boolean answer;
                  

                  final boolean answer;
                  

                  可以从侦听器中访问该变量,但不能为其分配新值,因为它被声明为 final.

                  it is possible to access the variable from within the listener, but it is not possible to assign it a new value, since it was declared as final.

                  诀窍来了.
                  将变量定义为:

                  Here comes the trick.
                  Define the variable as:

                  final boolean[] answer = new boolean[1];
                  

                  你们中的一些人已经明白为什么这会奏效.这里的最终变量不是布尔数组的单个元素,而是数组本身.
                  所以现在您可以根据需要分配数组元素 [0].

                  Some of you already see why this will work. The final variable here is not the single element of the boolean array, is the array itself.
                  So now you can assign the array element [0] as you wish.

                  dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int buttonId) {
                          answer[0] = true;
                      }
                  });
                  . . .
                  return answer[0];
                  

                  最后你可以从你的方法中返回它.

                  And finally you can return it from your method.

                  这篇关于Android 确认对话框返回 true 或 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Android:输入时如何从 TimePicker 获取时间 下一篇:Android 进度对话框

                  相关文章

                  最新文章

                  <i id='uCT2N'><tr id='uCT2N'><dt id='uCT2N'><q id='uCT2N'><span id='uCT2N'><b id='uCT2N'><form id='uCT2N'><ins id='uCT2N'></ins><ul id='uCT2N'></ul><sub id='uCT2N'></sub></form><legend id='uCT2N'></legend><bdo id='uCT2N'><pre id='uCT2N'><center id='uCT2N'></center></pre></bdo></b><th id='uCT2N'></th></span></q></dt></tr></i><div id='uCT2N'><tfoot id='uCT2N'></tfoot><dl id='uCT2N'><fieldset id='uCT2N'></fieldset></dl></div>

                    <bdo id='uCT2N'></bdo><ul id='uCT2N'></ul>

                    <legend id='uCT2N'><style id='uCT2N'><dir id='uCT2N'><q id='uCT2N'></q></dir></style></legend>
                      <tfoot id='uCT2N'></tfoot>

                      <small id='uCT2N'></small><noframes id='uCT2N'>