2012-03-19 106 views
27

當前用戶打開我的應用程序時,會打開一個AlertDialog,詢問他們是否要升級到專業版。我需要將CheckBox添加到AlertDialog,當用戶打開該應用時,該應用將不再顯示AlertDialog如何將複選框添加到警報對話框

以下是我對AlertDialog現在:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(" MY_TEXT"); 
    builder.setMessage(" MY_TEXT ") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Uri uri = Uri.parse("market://details?id=MY_APP_PACKAGE"); 
        Intent intent = new Intent (Intent.ACTION_VIEW, uri); 
        startActivity(intent);       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }).show(); 

如果有人能告訴我如何將CheckBox添加到AlertDialog,這將使應用不再顯示AlertDialog當用戶打開應用程序, 那太好了。 在此先感謝!

回答

71

您必須在AlertDialog.Builder對象上使用方法setView(View)。這將在消息區域和按鈕之間傳遞View。簡單地誇大了CheckBox一個View,並通過在這裏有一個例子:

checkbox.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <CheckBox 
     android:id="@+id/checkbox" 
     style="?android:attr/textAppearanceMedium" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" /> 

</FrameLayout> 
在活動

代碼

View checkBoxView = View.inflate(this, R.layout.checkbox, null); 
CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox); 
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

     // Save to shared preferences 
    } 
}); 
checkBox.setText("Text to the right of the check box."); 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(" MY_TEXT"); 
    builder.setMessage(" MY_TEXT ") 
      .setView(checkBoxView) 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Uri uri = Uri.parse("market://details?id=MY_APP_PACKAGE"); 
        Intent intent = new Intent (Intent.ACTION_VIEW, uri); 
        startActivity(intent);       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }).show(); 
+2

我認爲你需要將findViewById返回的視圖轉換爲CheckBox。好的答案,否則。 – Gallal 2014-04-15 08:52:55

+0

@Gallal你是對的。我做了更正。 – 2014-04-17 15:56:41

+1

儘量避免使用帶有示例xml文件的長文本,如果文本太長,複選框將被隱藏 – xDragonZ 2015-11-12 12:43:23

1

首先,您需要定義一個包含消息和複選框的佈局,以禁用後續視圖上的警報。然後,而不是調用builder.setMessage,你會叫:

builder.setView(myAlertViewWithDisablingCheckbox); 

然後,當用戶點擊警告對話框按鈕,你就必須檢查,看看是否複選框被選中,並保存偏好您應用程序的SharedPreferences。然後,您可以使用該首選項來確定是否應該再次向用戶顯示此警報對話框。

0

你可以使用一個multichoicelist只有一個項目:

final boolean checked = new boolean {false}; 
builder.setMultiChoiceItems(new String[]{"Remember decision}, checked, new DialogInterface.OnMultiChoiceClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i, boolean b) { 
        checked[i] = b; 
       } 
      }); 

然後在一個警告對話框按鈕,您可以檢查的checked[0]值,並保存在價值的應用程式的Sharedpreferences

builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         if(checked[0]){ 
          SharedPreferences.Editor editor = settings.edit(); 
          editor.putBoolean("preferences_never_buy_pro", true); 
          editor.apply(); 
         } 
         dialog.cancel(); 

        } 
       }); 

該個人喜好,你可以決定是否要在未來再次被所示的對話框。