0

我试图将一些参数传递给 AlertDialog,这个对话框最显示这两个参数(假设“foo”和“bar”参数),我正在使用showDialog(int id). Activity 类中还有另一种方法,它需要一个Bundle对象来传递参数:showDialog(int id, Bundle args),但是这个方法只适用于 API 8 或更高版本,我需要使用 API 7。

在这里,我放了一些代码块,以使我正在做的事情更容易。

在我的活动中,我像这样创建 AlertDialog:

    protected Dialog onCreateDialog(int id) {
        final LayoutInflater factory = LayoutInflater.from(this);

        switch(id) {
        case DIALOG_ID:
            final View view = factory.inflate(R.layout.dialog_layout, null);
            final TextView fooValue = (TextView)view.findViewById(R.id.foo_label);
            final TextView barValue = (TextView)view.findViewById(R.id.foo_label);
            //fooLabel.setText("HERE MUST BE FOO PARAMETER VALUE");
            //barLabel.setText("HERE MUST BE BAR PARAMETER VALUE");

            return new AlertDialog.Builder(this).
                setIcon(R.drawable.icon).
                setTitle(R.string.app_name).
                setView(view).
                setPositiveButton(R.string.close, null).
                create();
...

在其他部分,我称这个对话框为:

    // THESE PARAMETERS MUST BE PASSED TO DIALOG
    int foo = result.getInt("foo");
    String bar = result.getString("bar");

    showDialog(DIALOG_ID);
...

非常感谢!

4

2 回答 2

2

我建议setFooBar(int foo, String bar)在实现上述功能的类中添加一个方法onCreateDialog,以便在调用之前接收 foo 和 bar 的值showDialog

如果您没有活动的实例,请考虑将方法和变量设为静态。

于 2011-01-22T01:19:47.513 回答
-1

试试这个例子

LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);// this layout can created by yourself whatever you want.
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();

布局名称:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent" android:textColor="#663355"
        android:layout_height="wrap_content" android:hint="@string/hello" />
</LinearLayout>
于 2011-01-22T11:49:13.867 回答