【问题标题】:Create Dialog from Dialog从对话框创建对话框
【发布时间】:2014-03-31 12:53:09
【问题描述】:

我试图在按下对话框的按钮时打开一个对话框。 --> buttonPressed --> Dialog1 --> Dialog2

第一个对话框的创建如下:

(When button is pressed):

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog1);         //<-- dialog1 contains a button
final Button button1 = (Button)dialog.findViewById(R.id.button1);  //<--in dialog1

button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //A NEW DIALOG BOX SHOULD APPEAR HERE WHEN BUTTON1 GETS CLICKED
    }
});

dialog.show();

到目前为止,这是我的代码。我尝试的是关闭对话框,然后打开一个新对话框:

dialog.dismiss();
Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);

dialog.show();

但是当试图打开一个这样的新对话框时,我在 Coreographer.class 中遇到了一些错误。我认为这是因为关闭的对话框无法创建新的对话框。现在我的问题是,如何通过单击另一个对话框的按钮从另一个对话框中打开一个对话框?

【问题讨论】:

  • Dialog dialog2 = new Dialog(context); 而不是上下文,试试YourActivityName.this"
  • 在新对话框中尝试 dialog.dismiss() 和 dialog2.show()
  • 你的问题是 dialog.show() 应该是 dialog2.show()。关闭后,您将无法显示对话框。

标签: android android-layout dialog


【解决方案1】:

在按钮点击对话框上试试这个

Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);
dialog.dismiss();
dialog2.show();

【讨论】:

    【解决方案2】:

    而不是使用上下文使用

    Dialog dialog2 = new Dialog(YourActivityName.this);
    

    【讨论】:

    • 使用上下文就可以正常工作;)我的问题是如何使用已经存在的对话框打开一个对话框
    • 对于它的价值,你会用.this而不是.class来引用外部类
    【解决方案3】:

    试试看

    final Dialog dialog1 = new Dialog(context);
    dialog.setContentView(R.layout.dialog1);         //<-- dialog1 contains a button
    final Button button1 = (Button)dialog1.findViewById(R.id.button1);  //<--in dialog1
    
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //A NEW DIALOG BOX SHOULD APPEAR HERE WHEN BUTTON1 GETS CLICKED
         Dialog dialog2 = new Dialog(context);
         dialog2.setContentView(R.layout.dialog2);
         dialog1.dismiss();
         dialog2.show();
        }
    });
    
    dialog.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2017-04-30
      相关资源
      最近更新 更多