android - 在 Android 中将参数传递给 AlertDialog

标签 android android-activity bundle builder android-alertdialog

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

我在这里放置了一些代码块以简化我正在做的事情。

在我的 Activity 中,我像这样创建 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);
...

非常感谢!

最佳答案

我建议在实现上面的onCreateDialog 函数的类中添加一个方法setFooBar(int foo, String bar),以在之前接收foo 和bar 的值showDialog 被调用。

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

关于android - 在 Android 中将参数传递给 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4764711/

相关文章:

java - 在自定义适配器类中调用 Activity 方法

android - 使用动态文本更新 UI

android - 在 android 中我如何传递 Arraylist<Float>

c# - 在 C# 中有类似 Bundle 的东西吗?

android - DialogFragment 中的 setArgument 有什么意义?

android - 如何在 Android 上使用 UsbDeviceConnection 从 HID 设备请求 USB HID 报告描述符

android - 如何使用 Phonegap 在 Android 上嵌入 YouTube 视频

android - Firestore 作为离线持久性机制有多可靠?

android - 在 javadoc 中自动生成@version 值

ruby-on-rails - bundle 安装错误 : "too many redirects"