How can I check a radio object in Android?

I have a radio object in an Android game application. Before the user can proceed to the real game, he must choose a level that consists of three radio objects. Now, if the user clicks “play,” the application crashes. How can I use a check to see if a button has been selected?

I also have an edittext, which I just use: if ((editText.getText (). ToString (). Equals (""))) to find out if the user wrote a name, but this does not work on radio exchanges, or at least at least my game crashes even when I try to use this type of check.

Any help appreciated!

+3
source share
1 answer

You can take the RadioGroup identifier, which contains 3 radio buttons.

  RadioGroup radiogrp = (RadioGroup) findViewById(R.id.RadioGroup);
  int id = radiogrp.getCheckedRadioButtonId();

This will return the identifier of the selected switch in this group. If empty, the return value is -1.

So, you can set the if condition and check if the identifier == -1 is selected than no radio buttons.

+3
source

Source: https://habr.com/ru/post/1771180/


All Articles