0

如何在運行時更改自定義AlertDialog的佈局(onclicking_positive_button)。在運行時單擊肯定按鈕時更改警報對話框佈局

舉例:創建AlertDialoglayout_1肯定按鈕變化onClicklayout_2

 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(); 
+0

finalAlertDialog.show前添加dialog.dismiss()()XML文件; –

回答

0

這直接使用,

dialogBuilder.setContentView(R.layout.layout_2); 
+0

謝謝重播...我得到這個錯誤:嘗試調用虛擬方法'void android.support.v7.app.AlertDialog.setContentView(int)'null對象引用 – Manu

+0

我已經更新了我的答案,現在您可以檢查。 –

0

根據您的要求,我建議你創建只有一個用於對話的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(); 
+0

感謝您的回覆,它給了我新的警報對話框,我需要用新的替換現有的佈局。 – Manu

+0

我更新了我的答案,上面你現在可以試試 –

相關問題