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

时间:2022-03-05 09:25:17

Currently when the user opens my app an AlertDialog opens asking them if they would like to upgrade to the pro version. I need to add a CheckBox to the AlertDialog that will make the app no longer show the AlertDialog when the user opens the app.

目前,当用户打开我的应用程序时,会打开一个AlertDialog,询问他们是否要升级到专业版。我需要在AlertDialog中添加一个CheckBox,当用户打开应用程序时,它会使应用程序不再显示AlertDialog。

Here is what I have for the AlertDialog now:

以下是我现在对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();

If someone could show me how to add a CheckBox to the AlertDialog that will make the app no longer show the AlertDialog when the user opens the app, that would be great. Thanks in advance!

如果有人可以告诉我如何将AlertBox添加到AlertDialog,这将使用户在打开应用程序时不再显示AlertDialog,那就太棒了。提前致谢!

3 个解决方案

#1


71  

You have to use the method setView(View) on the AlertDialog.Builder object. This will put the passed in View between the message area and buttons. Simply inflate a View with a CheckBox and pass that in. Here's an example:

您必须在AlertDialog.Builder对象上使用方法setView(View)。这将把传递的视图放在消息区域和按钮之间。只需使用CheckBox对视图进行充气并将其传入。以下是一个示例:

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>

Code in your Activity

您的活动中的代码

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


1  

First you need to define a layout that contains the message and checkbox for disabling the alert on subsequent views. Then instead of calling builder.setMessage, you'll call:

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

builder.setView(myAlertViewWithDisablingCheckbox);

Then when the user clicks on the alert dialog button, you'll have to check to see if that checkbox has been checked, and save that preference in your app's SharedPreferences. Then you can use that preference to determine if this alert dialog should be shown to the user again.

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

#3


1  

You could use a multichoicelist with only one item:

你可以使用只有一个项目的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;
               }
           });

Then in the OnClick() of an alert dialog button you can check the value of checked[0]and save that value in your app's Sharedpreferences:

然后在警告对话框按钮的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();

                   }
               });

With this preference you can decide whether the dialog should be shown again in the future.

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

#1


71  

You have to use the method setView(View) on the AlertDialog.Builder object. This will put the passed in View between the message area and buttons. Simply inflate a View with a CheckBox and pass that in. Here's an example:

您必须在AlertDialog.Builder对象上使用方法setView(View)。这将把传递的视图放在消息区域和按钮之间。只需使用CheckBox对视图进行充气并将其传入。以下是一个示例:

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>

Code in your Activity

您的活动中的代码

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


1  

First you need to define a layout that contains the message and checkbox for disabling the alert on subsequent views. Then instead of calling builder.setMessage, you'll call:

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

builder.setView(myAlertViewWithDisablingCheckbox);

Then when the user clicks on the alert dialog button, you'll have to check to see if that checkbox has been checked, and save that preference in your app's SharedPreferences. Then you can use that preference to determine if this alert dialog should be shown to the user again.

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

#3


1  

You could use a multichoicelist with only one item:

你可以使用只有一个项目的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;
               }
           });

Then in the OnClick() of an alert dialog button you can check the value of checked[0]and save that value in your app's Sharedpreferences:

然后在警告对话框按钮的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();

                   }
               });

With this preference you can decide whether the dialog should be shown again in the future.

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