How to check if a radiobutton is checked in a radiogroup in Android?

后端 未结 10 2134

I need to set validation that user must fill / select all details in a page. If any fields are empty wanna show Toast message to fill. Now I need set validation

相关标签:
10条回答
  • 2020-12-04 13:17
    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (mRadioButtonMale.isChecked()) {
                    text = "male";
                } else {
                    text = "female";
                }
            }
        });
    

    OR

     mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (mRadioButtonMale.isChecked()) { text = "male"; }
                if(mRadioButtonFemale.isChecked()) { text = "female"; }
            }
        });
    
    0 讨论(0)
  • 2020-12-04 13:19

    I used the id's of my radiobuttons to compare with the id of the checkedRadioButton that I got using mRadioGroup.getCheckedRadioButtonId()

    Here is my code:

    mRadioButton1=(RadioButton)findViewById(R.id.first);
    mRadioButton2=(RadioButton)findViewById(R.id.second);
    mRadioButton3=(RadioButton)findViewById(R.id.third);
    mRadioButton4=(RadioButton)findViewById(R.id.fourth);
    
    mNextButton=(Button)findViewById(R.id.next_button);
    
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int selectedId=mRadioGroup.getCheckedRadioButtonId();
    
            int n=0;
            if(selectedId==R.id.first){n=1;}
    
            else if(selectedId==R.id.second){n=2;}
    
            else if(selectedId==R.id.third){n=3;}
    
            else if(selectedId==R.id.fourth){n=4;}
            //. . . .
        }
    
    0 讨论(0)
  • 2020-12-04 13:24

    If you want to check on just one RadioButton you can use the isChecked function

    if(radioButton.isChecked())
    {
      // is checked    
    }
    else
    {
      // not checked
    }
    

    and if you have a RadioGroup you can use

    if (radioGroup.getCheckedRadioButtonId() == -1)
    {
      // no radio buttons are checked
    }
    else
    {
      // one of the radio buttons is checked
    }
    
    0 讨论(0)
  • 2020-12-04 13:32

    All you need to do is use getCheckedRadioButtonId() and isChecked() method,

    if(gender.getCheckedRadioButtonId()==-1)
    {
        Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    }
    else
    {
        // get selected radio button from radioGroup
        int selectedId = gender.getCheckedRadioButtonId();
        // find the radiobutton by returned id
        selectedRadioButton = (RadioButton)findViewById(selectedId);
        Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
    }
    

    https://developer.android.com/guide/topics/ui/controls/radiobutton.html

    0 讨论(0)
  • 2020-12-04 13:33

    Try to use the method isChecked();

    Like,

    selectedRadioButton.isChecked() -> returns boolean.

    Refere here for more details on Radio Button

    0 讨论(0)
  • 2020-12-04 13:35

    Use the isChecked() function for every radioButton you have to check.

    RadioButton maleRadioButton, femaleRadioButton;
    
    maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
    femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
    

    Then use the result for your if/else case consideration.

    if (maleRadioButton.isChecked() || femaleRadioButton.isChecked()) {
         Log.d("QAOD", "Gender is Selected");
    } else {
        Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
        Log.d("QAOD", "Gender is Null");
    }
    
    0 讨论(0)
提交回复
热议问题