Android:自定义对话框,包含数据

时间:2011-10-24 19:37:55

标签: android dialog bundle

我想将一些包的字符串放入自定义对话框中。到目前为止,我发现Dialog不会处理捆绑包。我尝试使用getIntent()。getExtras()创建一个onCreate方法,但它不起作用。

有人可以给我一个建议吗?

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

}
}

1 个答案:

答案 0 :(得分: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());

创建对话框时,不要忘记检查捆绑包是否为空