0

Как я могу изменить макет пользовательского AlertDialog во время выполнения (onclicking_positive_button).Диалоговое окно изменения предупреждений при нажатии кнопки «положительная» во время выполнения

Пример: Создать AlertDialog с layout_1onClick положительных изменений Кнопка для layout_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

добавить dialog.dismiss() перед finalAlertDialog.show(); –

ответ

0

использовать это непосредственно,

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

спасибо за повтор ... я получаю эту ошибку: пытаюсь вызвать виртуальный метод 'void android.support.v7.app.AlertDialog.setContentView (int)' на ссылке нулевого объекта – Manu

+0

Я обновил свой ответ, теперь вы можете проверить , –

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

спасибо за ответ, он дает мне новый диалог оповещений, мне нужно заменить существующий макет на новый. – Manu

+0

Я обновил свой ответ выше, вы можете попробовать сейчас –

Смежные вопросы