2015-06-05 53 views
0

您好我想在動態創建警報對話框時更改警報對話框按鈕文本。這意味着我具有基於該布爾值的布爾值,需要更改警報對話框按鈕文本和佈局警報dalog .Ex: - 如果值爲true,則在警報對話框中應該有一個按鈕,並且按鈕文本爲「YES」,如果爲false,則需要添加兩個按鈕和一個按鈕文本應該變爲「OK」,另一個按鈕爲負按鈕。這應該在創建警報對話框之前進行檢查。到目前爲止,我在創建警報對話框時已更改了文本和版式權重。但它沒有改變任何內容。總是顯示默認xml文件中的警告對話框。在創建警報對話框時動態更改按鈕文本android

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

回答

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

我真的很感謝你answer.I知道這是什麼,我問我的,但我有使用LayoutInflater()膨脹來獲得父母layout.But您的解決方案沒有按」。 t使用這一個。你可以給我的方式使用LayoutInfalter到你的解決方案。 – Sajithv

+0

而我需要實現AlertDialog而不是對話框。 – Sajithv

3

首先有兩個按鈕,創建你的佈局R.layout.dialog_layout,並設置的兩個按鈕重1 ie android:layout_weight="1"如果您的值是true然後隱藏 bu tton即只顯示一個按鈕,如果值是那麼,只要你想

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

感謝您的回答。我需要知道如何使用LayoutInflater來解答。 – Sajithv

+0

以及如何在這些情況下使用AlertDialog.Builder。 – Sajithv

相關問題