如何根据对话框中选择的按钮自定义切换按钮?

时间:2018-02-07 05:05:32

标签: android

我希望根据对话按下按钮打开或关闭开关按钮。但是当我按下它时它工作正常但是当我按下时它不起作用否则意味着根据对话框中的选定选项,交换按钮和对话框不同步这是我的代码

s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(DisplayBasicData.this);
            alertDialogBuilder.setMessage("make decision");
                    alertDialogBuilder.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if(isChecked){
                                        s.setChecked(true);
                                    }

                                    dialog.cancel();
                                }

                            });

            alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("ygyu",""+isChecked);
                    if(!isChecked ||isChecked){
                        s.setChecked(false);

                        dialog.dismiss();}

                }
            });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }

2 个答案:

答案 0 :(得分:0)

使用

 final Switch switch1 =(Switch) findViewById(R.id.switch1);

     switch1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
            alertDialogBuilder.setMessage("make decision");
            alertDialogBuilder.setPositiveButton("yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if((!switch1.isChecked()) ||(switch1.isChecked())) {
                                switch1.setChecked(true);
                            }
                            dialog.cancel();
                        }
                    });

            alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if ((!switch1.isChecked()) || (switch1.isChecked())) {
                        switch1.setChecked(false);
                    }
                    dialog.dismiss();
                }
            });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    });

答案 1 :(得分:0)

我对你的代码进行了一些编辑,看看:

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(DisplayBasicData.this);
            alertDialogBuilder.setMessage("make decision");
                    alertDialogBuilder.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                        s.setChecked(true);


                                    dialog.cancel();
                                }

                            });

            alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("ygyu",""+isChecked);

                        s.setChecked(false);

                        dialog.dismiss();


                }
            });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }