如何添加第四个按钮?

时间:2015-03-10 08:49:48

标签: android android-layout android-alertdialog

我制作了一个警告框,其中包含标题,每个选项的说明和三个按钮。现在我看到我需要另一个按钮!但我看到只存在消极,中立和积极的按钮。这是我的代码:

alertDialog = new AlertDialog.Builder(Home.this);
        LayoutInflater inflater = this.getLayoutInflater();
        alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));


        alertDialog.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //do a thing
            }
        });

        alertDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //do a thing
            }
        });

        alertDialog.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do a thing
            }
        });

        alertDialog.show();

alert_dialog_layout.xml是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:background="@drawable/button_blue" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_and_margin"
        android:text="@string/text4"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_and_margin"
        android:gravity="center"
        android:text="@string/text5" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_and_margin"
        android:gravity="center"
        android:text="@string/text6"
        android:layout_marginBottom="@dimen/padding_and_margin" />
</LinearLayout>

我想要另一个按钮,因为我有4个选项。是否可以使用警报按钮或我更改了视图? 可以更改我自动制作的3(或4)按钮的布局吗? 谢谢你的回答

2 个答案:

答案 0 :(得分:1)

使用您自己的自定义视图(alert_dialog_layout.xml)为AlertDialog充气,并将按钮放在alert_dialog_layout.xml中

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.alert_dialog_layout, (ViewGroup) findViewById(R.id.root)); 

Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();

答案 1 :(得分:0)

您必须创建自定义布局。

http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

创建您自己的布局并对其进行充气以获取视图,然后按照其余链接进行操作。