创建Alert Dialog android时动态更改按钮文本

时间:2015-06-05 10:28:30

标签: android android-alertdialog layout-inflater

您好我想在动态创建警报对话框时更改警报对话框按钮文本。这意味着我有基于布尔值的布尔值需要更改警报对话框按钮文本和警报dalog.Ex的布局: - 如果值为真则应该是警告对话框中的一个按钮,按钮文字"是"如果为false,则需要添加两个按钮,其中一个按钮文本应更改为" OK"另一个按钮是否定按钮。这应该在创建警报Dialog之前检查。所以我在创建警告对话框时更改了文本和布局权重。但它没有改变任何内容。总是显示默认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();

3 个答案:

答案 0 :(得分:3)

首先使用两个按钮创建您的布局R.layout.dialog_layout,如果您的值为 true ,则设置两个按钮权重为1 android:layout_weight="1",然后隐藏否< / strong>按钮即只显示单个按钮,如果值为 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();

答案 1 :(得分:0)

这里我为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();
}

答案 2 :(得分:0)

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

这是一个有效的例子:

 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();
        }
    });
}