带有警告框的单选按钮已选中侦听器

Radio Button Checked Listener With Alert Box

我希望在单击单选按钮时出现警告框。 勾选yes时勾选,不勾选取消勾选

我使用单选组 CheckedChangeListener 和警报对话框尝试了此代码。

 public void onRadioButtonChecked() {
    Log.v(TAG,"Radio Button : ");
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int buttonId) {
            // Check which radio button was clicked
            Log.v(TAG,"on checked: value" + radioGroup+" radio "+buttonId);
            switch(buttonId) {
                case R.id.radioButton:
                    Log.v(TAG,"radio button1 : ");
                    openDefault();
                    break;
                case R.id.radioButton2:
                    Log.v(TAG,"radio button2 : ");
                    openCalendar();
                    break;
                case R.id.radioButton3:
                    Log.v(TAG,"radio button3 : ");
                    openCustom();
            }
        }
    });
}

public void openDefault(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you really want to enable Default settings? ");
            alertDialogBuilder.setPositiveButton("yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            radioGroup.setOnCheckedChangeListener(null);
                            radiobutton1.setChecked(true);
                            onRadioButtonChecked();
                            mOutputText.setText("Default Settings\n");
                            DefaultAlert();
                            Toast.makeText(MainActivity.this, "Default Settings Enabled",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            alertDialogBuilder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            radioGroup.setOnCheckedChangeListener(null);
                            radiobutton1.setChecked(false);
                            onRadioButtonChecked();
                        }
                    });

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

public void openCalendar(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you really want to enable Google calendar settings? ");
    alertDialogBuilder.setPositiveButton("yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    radioGroup.setOnCheckedChangeListener(null);
                    radiobutton2.setChecked(true);
                    onRadioButtonChecked();
                    mOutputText.setText("");
                    getResultsFromApi();
                    Toast.makeText(MainActivity.this, "Google Calendar Settings Enabled",
                            Toast.LENGTH_SHORT).show();
                }
            });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    radioGroup.setOnCheckedChangeListener(null);
                    radiobutton2.setChecked(false);
                    onRadioButtonChecked();
                }
            });

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

public void openCustom(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you really want to enable Custom settings? ");
    alertDialogBuilder.setPositiveButton("yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    radioGroup.setOnCheckedChangeListener(null);
                    radiobutton3.setChecked(true);
                    onRadioButtonChecked();
                    Toast.makeText(MainActivity.this, "Custom Settings Enabled",
                            Toast.LENGTH_SHORT).show();
                }
            });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    radioGroup.setOnCheckedChangeListener(null);
                    radiobutton3.setChecked(false);
                    onRadioButtonChecked();
                }
            });

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

如果我选中单选按钮,它会出现警告框,当我按下 "No" 时,它将取消选中。然后再次转到同一个收音机,然后从不会检查的警告框中按 "yes",但会出现 Toast。

问题是什么,给个解决办法

谢谢

首先你犯了错误,因为这里你需要选择,而不是选项,所以请用复选框替换你的单选按钮。

或者打开对话框的解决方案使用复选框注册单击侦听器或现在作为单选按钮,这对打开对话框很有用。

用下面我的代码替换你的波纹管代码

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int buttonId) {
                // Check which radio button was clicked
                Log.v(TAG,"on checked: value" + radioGroup+" radio "+buttonId);
                switch(buttonId) {
                    case R.id.radioButton:
                        Log.v(TAG,"radio button1 : ");
                        openDefault();
                        break;
                    case R.id.radioButton2:
                        Log.v(TAG,"radio button2 : ");
                        openCalendar();
                        break;
                    case R.id.radioButton3:
                        Log.v(TAG,"radio button3 : ");
                        openCustom();
                }
            }
        });

我的代码

radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDefault();
            }
        });
        radioButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCalendar();
            }
        });
        radioButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCustom();
            }
        });

希望对您有所帮助!您只需要 radiogroup clearcheck() 方法。

让我先说说你的代码有什么问题。当你select没有from alert dialog box效果的这两行

radioGroup.setOnCheckedChangeListener(null);
radiobutton1.setChecked(false);

检查更改无法通过广播组收听。所以 radiogroup 默认持有 checkchanges 意味着它持有 true 或 false 取决于检查状态。在这种情况下 listener 为 null 时,它无法保留代码中的引用。

为避免此问题,您需要删除此行

radioGroup.setOnCheckedChangeListener(null);

但这会导致您的对话框重复打开。因此,在这里您可以借助 clearcheck 方法获得解决方案。试试下面的代码

public void onRadioButtonChecked() {
    Log.v("","Radio Button : ");
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int buttonId) {
            // Check which radio button was clicked
            Log.v("","on checked: value" + radioGroup+" radio "+buttonId);
            switch(buttonId) {

                case R.id.radioButton1:
                    Log.v("","radio button1 : ");
                    if(radiobutton1.isChecked())
                    {
                        openDefault();
                    }
                    break;
                case R.id.radioButton2:
                    Log.v("","radio button2 : ");

                    if(radiobutton2.isChecked()) {
                        openCalendar();
                    }
                    break;
                case R.id.radioButton3:
                    Log.v("","radio button3 : ");

                    if(radiobutton3.isChecked()) {
                        openCustom();
                    }
                    break;
            }
        }
    });
}



public void openDefault(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you really want to enable Default settings? ");
    alertDialogBuilder.setPositiveButton("yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "Default Settings Enabled",
                            Toast.LENGTH_SHORT).show();
                }
            });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    radioGroup.clearCheck();
                }
            });

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

public void openCalendar(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you really want to enable Google calendar settings? ");
    alertDialogBuilder.setPositiveButton("yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "Google Calendar Settings Enabled",
                            Toast.LENGTH_SHORT).show();
                }
            });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    radioGroup.clearCheck();
                }
            });

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

public void openCustom(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you really want to enable Custom settings? ");
    alertDialogBuilder.setPositiveButton("yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                    Toast.makeText(MainActivity.this, "Custom Settings Enabled",
                            Toast.LENGTH_SHORT).show();
                }
            });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    radioGroup.clearCheck();
                }
            });

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

只需清空您的 YES 条件,默认情况下它会自动 select 单选按钮。在 NO 条件下添加 radiogroup clearcheck。并且还 clearcheck 再次调用 changelistener 以避免我检查按钮的条件 isChecked() 以显示对话框。