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

后端 未结 10 2154

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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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");
    }
    

提交回复
热议问题