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

Luc*_*man 40 checkbox android android-alertdialog

目前,当用户打开我的应用程序时,会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();
Run Code Online (Sandbox Code Playgroud)

如果有人能告诉我如何将添加CheckBoxAlertDialog,这将使应用不再显示AlertDialog当用户打开应用程序,这将是巨大的.提前致谢!

Jas*_*son 88

您必须setView(View)AlertDialog.Builder对象上使用该方法.这将传入View消息区域和按钮之间.只需View用a 充气CheckBox并传入即可.这是一个例子:

checkbox.xml

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

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

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

您的活动中的代码

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();
Run Code Online (Sandbox Code Playgroud)

  • 我认为你需要将findViewById返回的视图转换为CheckBox.否则很好的答案. (2认同)
  • 尽量避免在示例 xml 布局中使用长文本,如果文本太长,复选框将被隐藏 (2认同)

Sur*_*gch 13

在此处输入图片说明

The way to make a checkbox list is to use setMultiChoiceItems in the AlertDialog.

// Set up the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose some animals");

// Add a checkbox list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
boolean[] checkedItems = {true, false, false, true, false};
builder.setMultiChoiceItems(animals, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        // The user checked or unchecked a box
    }
});

// Add OK and Cancel buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // The user clicked OK
    }
});
builder.setNegativeButton("Cancel", null);

// Create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
Run Code Online (Sandbox Code Playgroud)

在这里,我硬编码了列表中的哪些项目已经被检查过。您更有可能希望在ArrayList<Integer>. 有关更多详细信息,请参阅文档示例。您还可以将选中的项目设置为null如果您始终希望所有内容都未选中。

对于contextthis如果您在活动中,则可以使用。

我更完整的答案在这里

科特林版

// Set up the alert builder
val builder = AlertDialog.Builder(context)
builder.setTitle("Choose some animals")

// Add a checkbox list
val animals = arrayOf("horse", "cow", "camel", "sheep", "goat")
val checkedItems = booleanArrayOf(true, false, false, true, false)
builder.setMultiChoiceItems(animals, checkedItems) { dialog, which, isChecked ->
    // The user checked or unchecked a box
}

// Add OK and Cancel buttons
builder.setPositiveButton("OK") { dialog, which ->
    // The user clicked OK
}
builder.setNegativeButton("Cancel", null)

// Create and show the alert dialog
val dialog = builder.create()
dialog.show()
Run Code Online (Sandbox Code Playgroud)


小智 6

您可以使用只有一项的多选列表:

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;
               }
           });
Run Code Online (Sandbox Code Playgroud)

然后在OnClick()警报对话框按钮中,您可以检查 的值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();

                   }
               });
Run Code Online (Sandbox Code Playgroud)

通过此首选项,您可以决定将来是否应再次显示该对话框。

  • 工作正常,直到你添加 `builder.setMessage(...)`,不幸的是 (2认同)