从对话框创建对话框

时间:2014-03-31 12:53:10

标签: android android-layout dialog

按下对话框的按钮时,我试图打开一个对话框。 - > 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中遇到了一些错误。我假设这是因为关闭的对话框无法创建新的对话框。现在我的问题是,如何通过点击它的按钮从另一个对话框中打开一个对话框?

3 个答案:

答案 0 :(得分:4)

在按钮上单击对话框

时尝试此操作
Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);
dialog.dismiss();
dialog2.show();

答案 1 :(得分:0)

而不是使用上下文

Dialog dialog2 = new Dialog(YourActivityName.this);

答案 2 :(得分:-1)

试试

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();