4

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

我想要的是这样的:

<Switch>被点击 --> 我显示一个 AlertDialog --> 如果按下是或否 --> 然后用setChecked(boolean)翻转` ,[boolean = true 如果按下yes, false 如果按下 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();
                        }
                    }
                });
4

5 回答 5

5

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

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();
        }
    });
于 2016-07-05T17:07:14.940 回答
2

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

    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();


        }
    });
于 2016-07-05T16:52:33.870 回答
2

拦截触摸事件将解决问题。如果不想将事件转发到系统,则返回 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;
        }
    });
于 2016-10-27T08:31:23.753 回答
1

使用以下更新的代码。

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();
                            }
                        }
                    }
                });

因此,每当用户单击 时NoAlertDialog您都应该通过使用再次切换开关

sw_enableDisable.toggle();

但这会反过来调用onCheckedChanged(),然后又会重新循环。因此,要处理这个问题,请维护一个布尔值toogledProgrammatically,并true在您在代码中切换它时将其设置为。然后当onCheckedChanged()被调用时,只需检查它是否以编程方式切换。如果是,则不要执行任何操作,否则显示警报。

if (toogledProgrammatically) {
                            toogledProgrammatically = false;
                        } else {
                            if (isChecked)
.
.
.
于 2015-07-26T19:26:40.953 回答
0

尝试使用 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);
    })
});
于 2015-07-26T19:24:26.170 回答