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

时间:2012-03-19 00:34:51

标签: android checkbox 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();

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

4 个答案:

答案 0 :(得分:76)

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

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>

您的活动中的代码

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

答案 1 :(得分:3)

您可以使用只有一个项目的多重列表:

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]的值并将该值保存在您应用的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();

                   }
               });

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

答案 2 :(得分:1)

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

builder.setView(myAlertViewWithDisablingCheckbox);

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

答案 3 :(得分:1)

Enter image description here

创建复选框列表的方法是在+.0001*x中使用c=np.random.normal(1-tf.sin(np.pi*x),1),0)

setMultiChoiceItems

在这里,我用硬编码对列表中的哪些项目进行了检查。您更可能希望在AlertDialog中跟踪它们。有关更多详细信息,请参见documentation example。如果您始终希望所有内容都未选中就开始,也可以将选中的项目设置为// 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<Integer>,如果您处于活动状态,则可以使用null

我的完整答案是here

科特林版本

context
相关问题