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

Android 如何将复选框添加到警报对话框,android,checkbox,android-alertdialog,Android,Checkbox,Android Alertdialog,当前,当用户打开我的应用程序时,会打开一个警报对话框,询问他们是否要升级到专业版 我需要在警报对话框中添加一个复选框,当用户打开应用程序时,该复选框将使应用程序不再显示警报对话框 下面是我现在为警报对话框准备的内容: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(" MY_TEXT"); builder.setMessage(" MY_TEXT ")

当前,当用户打开我的应用程序时,会打开一个
警报对话框
,询问他们是否要升级到专业版

我需要在
警报对话框
中添加一个
复选框
,当用户打开应用程序时,该复选框将使应用程序不再显示
警报对话框

下面是我现在为
警报对话框
准备的内容:

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

如何将
复选框
添加到
警报对话框
,以使应用程序在用户打开应用程序时不再显示
警报对话框

首先,您需要定义一个布局,其中包含消息和复选框,用于在后续视图中禁用警报。然后,您将调用以下命令,而不是调用
builder.setMessage

builder.setView(myAlertViewWithDisablingCheckbox);

然后,当用户单击“警报”对话框按钮时,您必须检查该复选框是否已选中,并将该首选项保存在应用程序的
SharedReferences
中。然后,您可以使用该首选项确定是否应再次向用户显示此警报对话框。

您必须使用
AlertDialog.Builder
对象上的方法
设置视图(视图)
。这将把传入的
视图
放在消息区域和按钮之间。只需使用
复选框
视图
充气,然后将其传递进去即可。下面是一个例子:

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>

您可以将多选项列表仅用于一个项目:

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;
               }
           });
然后,在警报对话框按钮的
OnClick()
中,您可以检查
checked[0]
的值,并将该值保存在应用程序的
SharedReferences

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

                   }
               });
使用此首选项,您可以决定以后是否再次显示该对话框。

制作复选框列表的方法是使用
警报对话框中的
设置多选项项

// 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();
在这里,我硬编码列表中的哪些项目已经检查过了。您更可能希望在
ArrayList
中跟踪它们。有关更多详细信息,请参阅。如果您总是希望所有内容都以非选中状态开始,则还可以将选中的项目设置为
null

对于
上下文
,如果您在活动中,可以使用

我更全面的回答是

科特林版本
我认为您需要将findViewById返回的视图强制转换为复选框。否则答案很好。尽量避免在示例xml Layout中使用长文本,如果文本太长,复选框将被隐藏。xml文件保存在哪里?在布局中还是其他地方?谢谢哦,上面写着R.louyouts.checkbox.xml。。。哎呀。teehee@JasonRobinson我知道这有点老了,但我想在我的对话片段中做类似的事情。但是,
View checkBoxView=View.inflate(这个,R.layout.checkbox,null)正在发出此错误“错误:(28,42)错误:不兼容类型:WifivsDataDialog无法转换为上下文”,如果复选框与文本对齐。我不想摆弄固定值…任何人?都可以正常工作,直到您添加
builder.setMessage(…)
,不幸的是,如果您也在对话框生成器上调用setMessage。这个答案行不通。唯一的解决方案是使用SetView不适用于builder.setMessage(“某些消息”);开始看起来不错,但是你不能再使用“setMessage”。可以,但它将删除复选框。因此,如果您需要一个带有文本和复选框的对话框,这将不起作用。用户在其问题中使用setMessage。。。
// 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();
// 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()