需要帮助获取自定义布局以使用我的Android AlertDialog

Bri*_*ian 6 android android-layout android-alertdialog

我已成功为我的Android应用程序创建了一个有效的AlertDialog:

public class MyClass extends DialogFragment{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ArrayList selectedItems = new ArrayList();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.pick_toppings)
        builder.setMultiChoiceItems(R.array.my_array, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    selectedItems.add(which);
                } else if (selectedItems.contains(which)) {
                    selectedItems.remove(Integer.valueOf(which));
                }
            }
        });
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // do stuff here ...
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // do stuff here ...
            }
        });

        return builder.create();
    }
}
Run Code Online (Sandbox Code Playgroud)

此MultiChoiceItems列表由数组支持 /res/values/array.xml

<resources>
    <array name="my_array">
        <item>item 01</item>
        <item>item 02</item>
        <item>item 03</item>
        <item>item 04</item>
        <item>item 05</item>
    </array>
</resources>
Run Code Online (Sandbox Code Playgroud)

从我的Activity中,我用这种方式调用AlertDialog:

MyClass myClass = new MyClass();
myClass.show(getSupportFragmentManager(), "My Dialog");
Run Code Online (Sandbox Code Playgroud)

我现在要做的是使用AlertDialog的自定义布局,以便我可以执行交替行着色,自定义按钮和添加EditText之类的操作,这样我就可以使用"其他"选项来填写"其他".

做了一些谷歌搜索后,看起来我需要创建一个新的布局,并将AlertDialog的视图设置为此布局.所以,我创建了一个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="other"
        android:textSize="18sp"/>
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后我将它添加到我的DialogFragment类:

LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.my_new_layout, null);
Run Code Online (Sandbox Code Playgroud)

然后

builder.setView(view);
Run Code Online (Sandbox Code Playgroud)

你可以猜到,这没用.新的CheckBox和EditText是在从我的数组中填充的其他复选框之后插入的,但它看起来很糟糕,而且我似乎无法控制从数组创建的复选框的外观.

就像我说的,我希望能够添加这个新的CheckBox/EditText组合,并且能够自定义整个AlertDialog的外观.

我真的想要使用数组,/res/values/array.xml这样如果我想在列表中添加新项目,我就不必硬编码新选项.

我想做的是什么?如果是这样,一些建议会很棒.

谢谢

这就是我希望我的AlertDialog看起来像/行为:

Bri*_*ian 0

好吧,我终于自己解决了这个问题。这是我的决心:

新版面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

新班级:

public class MyClass extends DialogFragment{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String[] theOptions = getResources().getStringArray(R.array.options);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.pick_toppings)

        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        LinearLayout view = (LinearLayout) layoutInflater.inflate(R.layout.my_layout, null);

        for(String option : theOptions){
            CheckBox checkbox = new CheckBox(getContext());
            checkbox.setText(option);
            view.addView(checkbox);
        }

        LinearLayout otherLinearLayout = new LinearLayout(getContext());
        otherLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        otherLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        otherLinearLayout.setId(R.id.otherLinearLayout);

        CheckBox otherCheckBox = new CheckBox(getContext());
        otherCheckBox.setText("other");
        otherCheckBox.setId(R.id.otherCheckBox);

        EditText otherEditText = new EditText(getContext());
        otherEditText.setId(R.id.otherEditText);

        otherLinearLayout.addView(otherCheckBox);
        otherLinearLayout.addView(otherEditText);

        view.addView(otherLinearLayout);

        builder.setView(view);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // do stuff here ...
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // do stuff here ...
            }
        });

        return builder.create();
    }
}
Run Code Online (Sandbox Code Playgroud)