【问题标题】:how can i save the multichoice state in an alertdialog?如何在警报对话框中保存多选状态?
【发布时间】:2020-06-05 11:20:26
【问题描述】:

我有一个代码,我在其中创建了一个方法来显示一个带有多选选项的对话框,但是当检查并单击“确定”按钮时,状态没有保存我该怎么做?我看到了一些带有 sharedpreferences 的东西,但我无法在我的代码中实现它,有人可以帮我吗?

 public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    final View view =   inflater.inflate(R.layout.fragment_inseticida, container, false);


    gps = view.findViewById(R.id.imageButton);
        gps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAlertDialog();
            }
        });



private void showAlertDialog (){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Habilitar GPS");

        String[] Gps = new String[]{
            "Habilitar GPS",

        };

        // Boolean array for initial selected items
        final boolean[] checkedgps = new boolean[]{
            true, // GPS

        };

        // Convert the color array to list
        final List<String> gpsList = Arrays.asList(Gps);

        builder.setMultiChoiceItems(Gps, checkedgps, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {


                checkedgps[i] = b;

                String CurrentItem  = gpsList.get(i);

                Toast.makeText(getActivity(),CurrentItem +  " " + b,Toast.LENGTH_SHORT).show();



            }
        });
        builder.setCancelable(false);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        AlertDialog dialog = builder.create();
        // Display the alert dialog on interface
        dialog.show();

}

【问题讨论】:

    标签: java android android-studio android-alertdialog multichoiceitems


    【解决方案1】:

    检查this所以答案。

    使用loadArray 检索最初在final boolean[] checkedgps 中的数据 和storeArraypublic void onClick(...) 中按下时保存数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2013-04-10
      • 2021-04-20
      相关资源
      最近更新 更多