【问题标题】:Android AlertDialog Custom ViewAndroid AlertDialog 自定义视图
【发布时间】:2016-02-14 22:00:16
【问题描述】:

我在 Stack Overflow 上搜索过这个主题。但是,我找不到任何类似的问题。

我有一个带有自定义布局的警报对话框。当我尝试使用警报对话框的findViewById 方法访问此自定义布局中的任何视图时,我最终会出现空指针异常。

自定义对话框的 XML 布局是这样的:

    <TextView
        style="@style/TextAppearance.AppCompat.Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_large"
        android:text="@string/settings" />

    <TextView
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_large"
        android:text="Search Distance" />

    <SeekBar
        android:id="@+id/sb_distance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="25"
        android:padding="@dimen/padding_large" />
</LinearLayout>

这是访问布局的代码:

AlertDialog settingsDialog = new AlertDialog.Builder(getActivity()).
                setTitle(R.string.settings)
                .setView(R.layout.layout_distance_settings)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Do sth here
                        dialogInterface.dismiss();
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create();

        SeekBar distanceSB = (SeekBar) settingsDialog.findViewById(R.id.sb_distance);
        distanceSB.setProgress(5);
        settingsDialog.show();

使用布局充气器创建布局可以解决问题。但是,我想知道我在这里做错了什么。

使用布局充气器,一切正常。代码如下:

AlertDialog settingsDialog = new AlertDialog.Builder(this).
                    setTitle(R.string.settings)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            // Do sth here
                            dialogInterface.dismiss();
                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .create();

            View parentView = settingsDialog.getLayoutInflater().inflate(R.layout.layout_distance_settings, null);

            SeekBar distanceSB = (SeekBar) parentView.findViewById(R.id.sb_distance);
            distanceSB.setProgress(5);
            settingsDialog.setView(parentView);
            settingsDialog.show();

所以我的问题又是这样的:使用setView 的正确方法是什么以及如何访问用作setView? 参数的布局子视图

【问题讨论】:

  • 你能展示你用来膨胀视图的代码吗?
  • 警报对话框生成器的setView方法用于设置自定义布局。
  • 我问的是您在说“使用布局充气器创建布局可以解决问题”时编写的代码。你写了什么?我想将它与您说的其他代码进行比较。

标签: android xml android-layout android-alertdialog


【解决方案1】:

您必须先扩充您的布局。为此,我们使用 LayoutInflater 类:

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View dialogView=inflater.inflate(R.layout.layout_distance_settings,null);

并将其传递给setView() 方法。

所以你的最终代码是:

LayoutInflater inflater = getActivity().getLayoutInflater();
                View dialogView=inflater.inflate(R.layout.layout_distance_settings,null);
AlertDialog settingsDialog = new AlertDialog.Builder(getActivity()).
                setTitle(R.string.settings)
                .setView(dialogView)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Do sth here
                        dialogInterface.dismiss();
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create();

        SeekBar distanceSB = (SeekBar) settingsDialog.findViewById(R.id.sb_distance);
        distanceSB.setProgress(5);
        settingsDialog.show();

【讨论】:

  • 我知道我可以通过充气来做到这一点。但是,我使用了警报对话框生成器的 setView 方法。 setView 的正确使用方法是什么?或者我可以访问 setView 设置的视图吗?
  • @ycagri 获取视图,可以使用 dialogView.findViewById(R.id.sb_distance)
  • 查看我更新的问题。我使用了 AlertDialog.Builder setView 方法。在文档中,此方法可以将布局资源 id 作为参数。我想学习 AlertDialog.Builder 使用的 setView 方法的正确方法。
猜你喜欢
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
相关资源
最近更新 更多