【问题标题】:Change Button Text dynamically when creating Alert Dialog android创建警报对话框android时动态更改按钮文本
【发布时间】:2015-06-05 10:28:30
【问题描述】:

嗨,我想在动态创建警报对话框时更改警报对话框按钮文本。这意味着我有基于该布尔值的布尔值需要更改警报对话框按钮文本和警报 dalog 的布局。例如:- 如果那里的值为真应该是警报对话框中的一个按钮,按钮文本为“是”,如果为 false,则需要添加两个按钮,其中一个按钮文本应更改为“确定”,另一个按钮为否定按钮。这应该在创建警报对话框之前进行检查。到目前为止,我在创建警报对话框时更改了文本和布局权重。但它没有改变任何东西。始终显示默认 xml 文件中的警报对话框。

 View dialogView = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        LinearLayout view = (LinearLayout)dialogView.findViewById(R.id.dialog_button_layout);
        Button btnYes = (Button) dialogView.findViewById(R.id.btn_yes);
       if(value){
            view.setWeightSum(1);        
            btnYes.setText(R.string.yes_text);            
        }else {
            view.setWeightSum(2);
            btnYes.setText(R.string.ok_text);            
        }

           AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(MyActivity.this,
                R.style.OurTheme)).setView(dialogView);
        Dialog = builder.create();
        Dialog.show();

【问题讨论】:

    标签: android android-alertdialog layout-inflater


    【解决方案1】:

    首先使用两个按钮创建您的布局R.layout.dialog_layout,并将两个按钮的权重设置为 1 即android:layout_weight="1" 如果您的值为 true 然后隐藏 No 按钮即仅在值为 false 时显示单个按钮,然后根据需要动态更改文本

    View dialogView = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        LinearLayout view = (LinearLayout)dialogView.findViewById(R.id.dialog_button_layout);
        Button btnYes = (Button) dialogView.findViewById(R.id.btn_yes);
        Button btnNo = (Button) dialogView.findViewById(R.id.btn_no);
       if(value){
             btnNo.setVisibility(View.GONE);    
            btnYes.setText(R.string.yes_text);            
        }else {
    
            btnYes.setText(R.string.ok_text); 
           btnNo.setText(R.string.no_text); 
        }
    
           AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(MyActivity.this,
                R.style.OurTheme)).setView(dialogView);
        Dialog = builder.create();
        Dialog.show();
    

    【讨论】:

      【解决方案2】:

      在这里,我为 AlertDailog 创建了自定义布局,如果它适合您,请使用它。

      custom_alert_dialog.xml

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
      
          android:layout_height="fill_parent" >
      
          <TextView
              android:id="@+id/alert_title"
              android:layout_width="fill_parent"
              android:layout_height="40dp"
              android:gravity="center"
              android:text="Alert Title"
              android:textColor="@android:color/white"
              android:textSize="19sp" />
      
          <TextView
              android:id="@+id/text"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/alert_title"
              android:layout_margin="15dp"
              android:gravity="center"
              android:text="Your Message here"
              android:textColor="#FFF" />
      
          <LinearLayout
              android:id="@+id/button_holder"
              android:layout_width="fill_parent"
              android:layout_height="50dp"
              android:layout_below="@+id/text"
              android:gravity="center"
              android:orientation="horizontal"
              android:weightSum="2" >
      
              <LinearLayout
                  android:id="@+id/positive_btn_holder"
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:orientation="horizontal" >
      
                  <Button
                      android:id="@+id/positive_btn"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:text="OK" />
              </LinearLayout>
      
              <LinearLayout
                  android:id="@+id/negative_btn_holder"
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:orientation="horizontal" >
      
                  <Button
                      android:id="@+id/negative_btn"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:text="Cancel" />
              </LinearLayout>
          </LinearLayout>
      
      </RelativeLayout>
      

      在我的活动中,我使用这一行来显示 AlertDialog,

      showAlertDialog(MainActivity.this, false);
      
      
      
      /**
       * Inside this method the alert dialog buttons are displayed based on category - param. Which is a boolean variable.
       * @param mAppContext
       * @param category - boolean
       */
      private void showAlertDialog(Context mAppContext, boolean category) {
          final Dialog dialog = new Dialog(mAppContext);
          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
          dialog.setContentView(R.layout.activity_main);
          TextView alert_title = (TextView) dialog.findViewById(R.id.alert_title);
          TextView text = (TextView) dialog.findViewById(R.id.text);
          LinearLayout negative_btn_holder = (LinearLayout) dialog
                  .findViewById(R.id.negative_btn_holder);
          LinearLayout positive_btn_holder = (LinearLayout) dialog
                  .findViewById(R.id.positive_btn_holder);
          Button positive_btn = (Button) dialog.findViewById(R.id.positive_btn);
          Button negative_btn = (Button) dialog.findViewById(R.id.negative_btn);
          alert_title.setText("Title...");
          // Based on your's visible and invisible the button here
          if (category == false) {
              negative_btn_holder.setVisibility(View.GONE);
          }
      
          text.setText("Android custom dialog example!");
      
          positive_btn.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  dialog.dismiss();
              }
          });
          negative_btn.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  dialog.dismiss();
              }
          });
      
          dialog.show();
      }
      

      【讨论】:

      • 我真的很感谢你的回答。我知道​​这是我问的但我的一个我使用 LayoutInflater().inflate 来获取父布局。但是你的解决方案不使用这个。你能给我是使用 LayoutInfalter 解决方案的方式。
      • 我需要实现 AlertDialog 而不是对话框。
      【解决方案3】:

      这实际上看起来是合法的。我不确定你的代码有什么问题,也许它在代码的另一部分。

      这是一个工作示例:

       public interface CompletionBlock {
          void onCompletion();
      }
      
       public static void showYesNoDialog(Context context, String msg, String yes, String no, final View.OnClickListener yesListener, final View.OnClickListener noListener) {
          final Dialog dialog = new Dialog(context);
          dialog.setContentView(R.layout.yes_no_alert_layout);
      
          TextView text = (TextView) dialog.findViewById(R.id.textView);
          text.setText(Html.fromHtml(msg));
      
          Button yesButton = (Button) dialog.findViewById(R.id.yesButton);
          yesButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (yesListener != null)
                      yesListener.onClick(v);
                  dialog.dismiss();
              }
          });
          if (yes != null)
              yesButton.setText(yes);
      
          Button noButton = (Button) dialog.findViewById(R.id.noButton);
          noButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (noListener != null)
                      noListener.onClick(v);
                  dialog.dismiss();
              }
          });
          if (no != null)
              noButton.setText(no);
      
          ((Activity) context).runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  dialog.show();
              }
          });
      }
      

      【讨论】:

      • 感谢您的回答。我需要知道如何使用 LayoutInflater 来回答您的问题。
      • 以及如何在这些场景中使用 AlertDialog.Builder。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 2016-12-01
      相关资源
      最近更新 更多