android - 如何仅在确认对话框后翻转开关的选中状态?

标签 android android-alertdialog oncheckedchanged android-switch

我的问题是 - 当我点击 <Switch> 时,它首先被切换,然后是 OnCheckedChangeListener叫做。

我想要的是:

<Switch>单击 --> 我显示一个 AlertDialog --> 如果按下是或否 --> 然后用 setChecked(boolean) 翻转 ` , [boolean = true if pressed yes, and false if pressed no].

问题:当<Switch>单击,它会自动翻转。我想首先从 AlertDialog 中用是或否来限定它。

sw_enableDisable
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        if (isChecked) {

                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                    getActivity());

                            alertDialogBuilder
                                    .setMessage(
                                            "Sure you want to enable?. ")
                                    .setCancelable(true)
                                    .setPositiveButton(
                                            getString("YES"),
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog,
                                                                    int which) {

                                                    dialog.cancel();
                                                }
                                            })

                                    .setNegativeButton(
                                            getString("NO"),
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(DialogInterface dialog,
                                                                    int which) {

                                                    dialog.cancel();
                                                }
                                            });

                            AlertDialog alertDialog = alertDialogBuilder.create();
                            alertDialog.show();
                            sw_enDis_alreadyClicked = true;

                        } else {
                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                    getActivity());
                            alertDialogBuilder
                                    .setMessage(
                                            "Sure you want to disable?")
                                    .setCancelable(true)
                                    .setPositiveButton(
                                            getString("YES"),
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog,
                                                                    int which) {
                                                    dialog.cancel();
                                                }
                                            })

                                    .setNegativeButton(
                                            getString("NO"),
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(DialogInterface dialog,
                                                                    int which) {

                                                    dialog.cancel();
                                                }
                                            });

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

最佳答案

试试这段代码。它对我有用:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    Switch switchButton = (Switch) findViewById(R.id.my_switch_id);

    switchButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final Switch btn = (Switch) v;
            final boolean switchChecked = btn.isChecked();

            if (btn.isChecked()) {
                btn.setChecked(false);
            } else {
                btn.setChecked(true);
            }

            String message = "Are you sure you want to disable this setting?";
            if (!btn.isChecked()) {
                message = "Are you sure you want to enable this setting?";
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(this); // Change "this" to `getActivity()` if you're using this on a fragment
            builder.setMessage(message)
                   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int i) {
                            // "Yes" button was clicked
                            if (switchChecked) {
                                btn.setChecked(true);
                            } else {
                                btn.setChecked(false);
                            }
                        }
                    })
                   .setNegativeButton("Cancel", null)
                   .show();
        }
    });

关于android - 如何仅在确认对话框后翻转开关的选中状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31640784/

相关文章:

java - 为什么 java Android 中的 super.onDestroy() 在析构函数中居于首位?

android - 在android中获取微录音频的指纹/语音打印

java - 捕获异常或对 Arrays.copyOf 进行空检查是好习惯吗

android - 在 android 中是否可以在更改 Activity 之前留下一个 AlertDialog?

android - 带有 ListView 和按钮的自定义对话框始终全屏显示

android - 动态插入 3 张图像到水平 ScrollView 或 Viewpager

android - Alertdialog 在 android 中创建异常

java - 如何在android中使用带有 'onCheckedChanged'方法的switch语句?

android - 可以在没有交互的情况下调用 onCheckedChanged 方法吗?

android - android 中的 OnCheckedChangeListener 和 OnClickListener——按钮在第一次单击时半工作,在第二次单击时完成?