如何将复选框添加到警报对话框

问题描述:

当前用户打开我的应用程序时,会打开一个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当用户打开应用程序, 那太好了。 在此先感谢!

您必须在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

首先,您需要定义一个包含消息和复选框的布局,以禁用后续视图上的警报。然后,而不是调用builder.setMessage,你会叫:

builder.setView(myAlertViewWithDisablingCheckbox); 

然后,当用户点击警告对话框按钮,你就必须检查,看看是否复选框被选中,并保存偏好您应用程序的SharedPreferences。然后,您可以使用该首选项来确定是否应该再次向用户显示此警报对话框。

你可以使用一个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(); 

        } 
       }); 

该个人喜好,你可以决定是否要在未来再次被所示的对话框。