在运行时单击正向按钮更改警报对话框布局

时间:2017-01-18 10:22:50

标签: android android-alertdialog android-dialogfragment customdialog

如何在运行时更改自定义AlertDialog的布局( onclicking_positive_button )。

示例:使用 layout_1 AlertDialog正面按钮更改为 layout_2

创建onClick
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        final LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.layout_1, null);
        dialogBuilder.setView(dialogView);

        final AlertDialog finalAlertDialog = alertDialog;
        dialogBuilder.setPositiveButton("time", 
                                  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

              View view=   inflater.inflate(R.layout.layout_2,null);
                finalAlertDialog.setContentView(view);
                finalAlertDialog.show();

            }
        });

        dialogBuilder.setNegativeButton("cancel", 
                                  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });

         alertDialog = dialogBuilder.create();

        alertDialog.show();

2 个答案:

答案 0 :(得分:0)

直接使用,

dialogBuilder.setContentView(R.layout.layout_2);

答案 1 :(得分:0)

根据您的要求,我建议您只创建一个用于对话框的xml文件,并在需要时使组件可见且不可见,这样首先创建如下所示的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/Textbox1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="Enter Your Text Here"
        android:text="Hello ,there !"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/TextBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/Textbox1"
        android:layout_marginTop="30dp"
        android:hint="Your Text Appears Here"
        android:textSize="25dp" />

    <Button

        android:id="@+id/CopyText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/TextBox2"
        android:layout_marginLeft="107dp"
        android:layout_marginStart="107dp"
        android:layout_marginTop="85dp"
        android:text="Copy Text" />

</RelativeLayout>

在您的MainActivity.java中使用以下代码

         LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
                final View promptView = layoutInflater.inflate(R.layout.activity_main, null);

                final android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(MainActivity.this)
                        .setView(promptView)
                        .setCancelable(false)
                        .setPositiveButton("time", null)
                        .setNegativeButton("cancel", null)
                        .create();

                alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {

                        Button btn_positive = alertDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE);
                        Button btn_negative = alertDialog.getButton(android.app.AlertDialog.BUTTON_NEGATIVE);

                        btn_positive.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                Button btn = (Button) promptView.findViewById(R.id.CopyText);

    /* Here i set button visibility gone so the same xml file but updated
 here you can set other component visibility Visible and Visibility.Gone 
as per your requirement */
                                if (btn.getVisibility() == View.GONE) {
                                    alertDialog.dismiss();
                                } else {
                                    btn.setVisibility(View.GONE);
                                }
                            }
                        });

                        btn_negative.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                alertDialog.dismiss();
                            }
                        });
                    }
                });
                alertDialog.show();