带警报框的单选按钮选中的侦听器

时间:2018-07-18 09:57:12

标签: android radio-button alertdialog radio-group

我希望单击单选按钮时出现一个警告框。 选择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();
}

如果我单选按钮选中,它会出现警报框,当我按下“否”时,它将取消选中。并再次转到同一台收音机,然后在警报框中按“是”,它将不进行检查,但会显示Toast。

问题出在哪里,并给出解决方案。

谢谢

2 个答案:

答案 0 :(得分:0)

首先要犯的错误是因为您需要选择,而不是选择,因此请用复选框替换单选按钮。

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

在我的代码下方替换下面的代码

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

答案 1 :(得分:0)

希望它将对您有帮助!您需要的只是radiogroup clearcheck()方法。

让我先告诉您代码中的问题。当您从警报对话框中选择“否”时,这两行效果

  

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

检查更改无法通过广播组收听。因此,radiogroup保留默认的checkchanges意味着它根据检查状态保留true或false。在这种情况下,当侦听器为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 条件,默认情况下其自动选择单选按钮。在条件下添加无线电组clearcheck。还要再次清除检查呼叫变更侦听器,以避免我检查按钮的isChecked()条件以显示对话框。