【问题标题】:I have an alertDialog in which i have check boxes . I want to retain the values of the check boxes checked in by the user我有一个 alertDialog ,其中有复选框。我想保留用户选中的复选框的值
【发布时间】:2020-05-12 12:04:55
【问题描述】:

我想保留用户输入的复选框值。 我希望在再次打开警报对话框时选择的值在那里。我在警报对话框中有复选框。一旦用户选择了他/她的选择然后回来,我希望使用先前的用户输入检查复选框,直到应用程序关闭。有办法吗?

【问题讨论】:

  • 使用全局变量供你使用。
  • 扩展对话框并拥有变量并使用警报对话框,而无需在进一步启动时创建另一个实例

标签: android android-studio checkbox android-alertdialog android-checkbox


【解决方案1】:

请试试这个

使用两个全局变量,一个用于标签,另一个用于选择。

        String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "thursday"};
        boolean[] checkedItems = {false, false, false, false, false};

然后点击显示对话框

   private void pickWeek() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose some days");

   // Add a checkbox list

    builder.setMultiChoiceItems(days, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i, boolean isChecked) {

                checkedItems[i]=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();
}

【讨论】:

    【解决方案2】:

    如果您已经有一个 Alertdialog,您可以简单地使用伴随对象来保存该值。将检查的值保存到伴随对象内的变量中,并在再次打开警报对话框时检索该值

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 2021-09-02
      • 2015-08-25
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多