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

后端 未结 10 2152

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: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

提交回复
热议问题