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

How to flip the checked status of a Switch only after confirm dialog?

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

我想要的是:

单击

<Switch> --> 我显示一个 AlertDialog --> 如果按下是或否 --> 然后用 setChecked(boolean)、[boolean = true if pressed yes, and false如果按否]。

问题:点击<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();
                        }
                    }
                });

尝试使用onClick:

switch.setOnClickListener(new OnCLickListener(){
    Here_is_createing_of_dialog...
    dialogBuilder.setPositiveButton(
    getString("YES"),
    new DialogInterface.OnClickListener() { 
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        switch.setChecked(true);
    })
});

使用以下更新代码。

sw_enableDisable
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        if (toogledProgrammatically) {
                            toogledProgrammatically = false;
                        } else {
                            if (isChecked) {

                                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                        MainActivity.this);

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

                                                        dialog.cancel();
                                                    }
                                                })

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

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

                                                        dialog.cancel();
                                                        toogledProgrammatically = true;
                                                        sw_enableDisable.toggle();
                                                    }
                                                });

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

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

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

                                                        dialog.cancel();
                                                        toogledProgrammatically = true;
                                                        sw_enableDisable.toggle();
                                                    }
                                                });

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

因此,每当用户点击 AlertDialogNo 时,您应该使用

再次切换开关
sw_enableDisable.toggle();

但这又会调用 onCheckedChanged() 然后它会再次循环。因此,要处理该问题,请维护一个布尔值 toogledProgrammatically 并将其设置为 true 每当您在代码中切换它时。然后当 onCheckedChanged() 被调用时,只需检查它是否以编程方式切换。如果是,则什么都不做,否则显示警报。

if (toogledProgrammatically) {
                            toogledProgrammatically = false;
                        } else {
                            if (isChecked)
.
.
.

通过设置后退状态来防止切换。

    Switch switchButton=(Switch)rootView.findViewById(R.id.switch_id);


    switchButton.setOnClickListener(new OnClickListener() {

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

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


            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        if(estado)btn.setChecked(true);
                        else btn.setChecked(false);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //No button clicked
                        break;
                    }
                }
            };

            String message="Disable function?";
            if(!btn.isChecked()) message="Enable function?";

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage(message).setPositiveButton("YES", dialogClickListener)
                .setNegativeButton("NO", dialogClickListener).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();
        }
    });

拦截触摸事件即可解决问题。 Return 如果不想将事件转发给系统,则为 TRUE。我想出了以下方法。 已测试,按预期工作。

switchButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
                case ACTION_DOWN:
                    if (!switchButton.isChecked()) {
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                ActivityLogin.this);

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

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

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

                                                dialog.cancel();
                                            }
                                        });

                        AlertDialog alertDialog = alertDialogBuilder.create();
                        alertDialog.show();
                        return true;
                    } else{
                        // show dialog Sure you want to disable? and handle the button events accordingly
                    }
            }
            return false;
        }
    });