【问题标题】:Android - sequential/nested dialog boxes, second dialog box closes the first oneAndroid - 顺序/嵌套对话框,第二个对话框关闭第一个
【发布时间】:2013-07-27 10:59:38
【问题描述】:

我有一个应用程序,当按下按钮时,会出现一个包含列表的对话框,在选择列表中的某个项目后,会出现一个数字选择器对话框。我想要发生的是,在用户从数字选择器对话框中选择一个数字后,只有数字选择器对话框会关闭,而对话框仍然处于打开状态

这是我目前的代码。

这是第一个对话框(带有列表的那个)的代码:

private void generateUnitDialog(String[] productUnit) {

    final CharSequence[] items = productUnit;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Make your selection");

    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Handle Confirm action here
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Handle cancel action here
        }
    });
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            // Do something with the selection
            Toast.makeText(getApplicationContext(),
                    "Item Click detected", Toast.LENGTH_SHORT)
                    .show();

            generateQuantityDialog();

        }
    });

    builder.setCancelable(false);

    AlertDialog alert = builder.create();
    alert.show();

}

When a certain list item is selected, the numberpicker dialog then pops up using this code:

public int generateQuantityDialog(){

         final Dialog d = new Dialog(com.angelo.orderform.OrderForm.this);

         d.setTitle("Choose Quantity");
         d.setContentView(R.layout.dialog);

         Button b1 = (Button) d.findViewById(R.id.button1);
         Button b2 = (Button) d.findViewById(R.id.button2);

         final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);

         np.setMaxValue(100000);
         np.setMinValue(1);
         np.setWrapSelectorWheel(true);
         //np.setOnValueChangedListener(this);

         b1.setOnClickListener(new OnClickListener(){
             @Override
             public void onClick(View v) {
                 //tv.setText(String.valueOf(np.getValue()));
                 d.dismiss();
             }    
        });
        b2.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              d.dismiss();
           }    
          });
       d.show();

       return(np.getValue());


}

用户选择某个数字后,数字选择器对话框以及列表中的那个对话框都会关闭。我希望带有列表的对话框继续存在。

显然,setCancellable(false) 没有帮助。

有什么想法吗?即使是不同的方法,我也愿意接受。

【问题讨论】:

  • 你能得到我的答案吗?

标签: android dialog numberpicker


【解决方案1】:
public class MainActivity extends Activity {
 Dialog d;
  AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


      final CharSequence[] items = {"a","b","c"};

        builder = new AlertDialog.Builder(this);
        builder.setTitle("Make your selection");

        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Handle Confirm action here
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Handle cancel action here
            }
        });
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                // Do something with the selection
                Toast.makeText(getApplicationContext(),
                        "Item Click detected", Toast.LENGTH_SHORT)
                        .show();

                generateQuantityDialog();

            }
        });

        builder.setCancelable(false);

        AlertDialog alert = builder.create();
        alert.show();

    }

public int generateQuantityDialog(){

     d = new Dialog(this);

    d.setTitle("Choose Quantity");
    d.setContentView(R.layout.al);

    Button b1 = (Button) d.findViewById(R.id.button1);
    Button b2 = (Button) d.findViewById(R.id.button2);

   //        final NumberPicker np = (NumberPicker) d.findViewById(R.id.timePicker1);

   //        np.setMaxValue(100000); 
   //        np.setMinValue(1);
        //        np.setWrapSelectorWheel(true);
    //np.setOnValueChangedListener(this);

    b1.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            //tv.setText(String.valueOf(np.getValue()));
            d.dismiss();
            AlertDialog alert = builder.create();
                alert.show();

        }


   });
   b2.setOnClickListener(new OnClickListener(){
     @Override
     public void onClick(View v) {
         d.dismiss();
            AlertDialog alert = builder.create();
                alert.show();

     }


     });
  d.show();

  return(54);


  }
  }

【讨论】:

  • 抱歉,我看不出这与我所拥有的有何不同。
【解决方案2】:

好的,我想通了,但我认为这是不好的做法。

我所做的是将生成数字选择器对话框的代码放置到 generateQuantityDialog() 原来所在的位置,然后在 d.dismiss() 之后调用 builder.show()

我会寻找解决方法,放弃这种方法。

【讨论】:

    【解决方案3】:

    你必须设置AlertDialog alert变量global

    根据您的要求,您可以取消或关闭alertdialog

    b1.setOnClickListener(new OnClickListener(){
                 @Override
                 public void onClick(View v) {
                     //tv.setText(String.valueOf(np.getValue()));
                     d.dismiss();
                     alert.dismiss() ; or 
                     alert.cancel();
                 }    
            });
    

    您可以根据需要关闭 alertDialog。

    如果选择第二个对话框。

    【讨论】:

    • 这个很混乱。我希望第一个对话框(警报对话框)在 numberpicker 执行后保持不变。你在那里的 onClickListener 使数字选择器和警报对话框消失,这不是我想要做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多