我想在选中单选按钮时出现一个警告对话框

时间:2016-06-21 05:57:54

标签: android android-alertdialog android-radiogroup android-radiobutton

我有一个带4个单选按钮的广播组。我想要在选中两个特定单选按钮中的任何一个时弹出警报对话框。我怎样才能实现这个目标?

1 个答案:

答案 0 :(得分:1)

     both    = (RadioButton) findViewById(R.id.both);

     RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rgroup);
    assert radioGroup != null;
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (veg.isChecked()) {
                proof="veg";
                //Toast.makeText(getApplicationContext(),proof,Toast.LENGTH_SHORT).show();
            } else if(nonv.isChecked()) {
                proof="non veg";
                    //Toast.makeText(getApplicationContext(),proof,Toast.LENGTH_SHORT).show();
            }
            else if (both.isChecked()){
                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("GPS is off");  // GPS not found
        builder.setMessage("Turn on location services"); // Want to enable?
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {

                ConnectToWifi.this.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

            }
        });
        builder.setNegativeButton("No", null);
        builder.create().show();

            }

        }
    });