2011-10-24 30 views
0

我想將一些字符串放入自定義對話框中。到目前爲止,我想出了Dialog不處理捆綁。我試圖用getIntent()。getExtras()創建一個onCreate方法,但它不起作用。Android:使用包中的數據定製對話框

有人可以給我一個建議嗎?

package com.droidfish.apps.acli; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class ShowDetails extends Activity { 
TextView tvShowDetailsContent1, tvShowDetailsContent2, 
     tvShowDetailsContent3; 
public String sDetailText1, sDetailText2, sDetailText3; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    /** Display Custom Dialog */ 
    // CustomizeDialog customizeDialog = new CustomizeDialog(this); 
    CustomizeDialog customizeDialog = new CustomizeDialog(this); 

    tvShowDetailsContent1 = (TextView) findViewById(R.id.tvShowDetailText1); 
    tvShowDetailsContent2 = (TextView) findViewById(R.id.tvShowDetailText2); 
    tvShowDetailsContent3 = (TextView) findViewById(R.id.tvShowDetailText3); 
    savedInstanceState = this.getIntent().getExtras(); 
    sDetailText1 = savedInstanceState.getString("param1"); 
    sDetailText2 = savedInstanceState.getString("param2"); 
    sDetailText3 = savedInstanceState.getString("param3"); 

    tvShowDetailsContent1.setText(sDetailText1); 
    tvShowDetailsContent2.setText(sDetailText2); 
    tvShowDetailsContent3.setText(sDetailText3); 
    customizeDialog.show(); 
} 

class CustomizeDialog extends Dialog implements OnClickListener { 
    Button okButton; 
    ShowDetails sh = new ShowDetails(); 

    public CustomizeDialog(Context context) { 
     super(context); 
     /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */ 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     /** Design the dialog in main.xml file */ 
     setContentView(R.layout.showdetails); 

     okButton = (Button) findViewById(R.id.bOkButton); 
     okButton.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (v == okButton) 
      dismiss(); 
    } 

} 
} 
+0

爲什麼不能從捆綁中獲取活動中的字符串,然後將其傳遞給Di一個日誌? – coder

回答

2

您可以實現自己的構造函數,它接受一個包中的對話框

public CustomizeDialog(Context context, Bundle bundle) { 
     super(context); 


     /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */ 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     /** Design the dialog in main.xml file */ 
     setContentView(R.layout.showdetails); 

     //do whatever with your bundle here 

     okButton = (Button) findViewById(R.id.bOkButton); 
     okButton.setOnClickListener(this); 
} 

然後在你的onCreate你可以叫

CustomizeDialog customizeDialog = new CustomizeDialog(this, getIntent().getExtras()); 

不要忘記檢查,如果你的包在創建對話框時爲空