2014-03-31 232 views
0

我試圖在按下對話框的按鈕時打開一個對話框。 - > 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。我認爲這是因爲一個關閉的對話框不能創建一個新的對話框。現在我的問題是,如何通過點擊它的按鈕從另一個對話框中打開一個對話框?

+0

'對話框dialog2 =新的對話框(背景);'相反請嘗試'YourActivityName.this'「 –

+0

在新對話框中嘗試dialog.dismiss()和dialog2.show() –

+0

http://stackoverflow.com/questions/22196886/show-one-progress-dialog-only-at-a-time-in-android/22197056#22197056 –

回答

4

試試這個按鈕點擊對話框

Dialog dialog2 = new Dialog(context); 
dialog2.setContentView(R.layout.dialog2); 
dialog.dismiss(); 
dialog2.show(); 
-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(); 
0

而不是使用上下文中使用

Dialog dialog2 = new Dialog(YourActivityName.this); 
+0

使用上下文工作正常;)我的問題是如何打開一個對話框與一個已經存在的對話框 – user2410644

+1

值得一提的是,你可以用'.this'而不是'.class'來引用外部類, – laalto