开发者

How can I validate a radiobuttongroup in Android?

开发者 https://www.devze.com 2023-01-21 13:07 出处:网络
I have a radiobuttongroup in a game app in Android. Before the user can go on to the actual game he has to choose a level, which are three radiobuttons. Now, if the user clicks \"play\" the app crashe

I have a radiobuttongroup in a game app in Android. Before the user can go on to the actual game he has to choose a level, which are three radiobuttons. Now, if the user clicks "play" the app crashes. How can I use a validation to see if a button was chosen?

I have an edittext also, w开发者_JAVA技巧hich I simply use a: if ((editText.getText().toString().equals(""))) to see if the user has written a name, but this doesn't work on radiobuttons, or at least my game crashes even when I try to use this type of check.

Any help appreciated!


You can take the RadioGroup id which contains 3 radio buttons.

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

This will return the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

So you can put if condition and check whether the id == -1 than no radio button is selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消