如何检查是否一个单选按钮是在Android的一个RadioGroup中检查?是在、单选、按钮、RadioGroup

2023-09-05 04:44:16 作者:非賣品こ

我需要设置验证的用户必须填写/选择页面中的所有细节。如果有任何字段为空想告诉吐司消息来填补。在 RadioGroup中。这个我试过code,但没有正常工作。我建议正确的方法。三江源。

  //获取选定的单选按钮,从RadioGroup中
INT selectedId = gender.getCheckedRadioButtonId();
//找到单选按钮由归国ID
selectedRadioButton =(单选)findViewById(selectedId);
//你想要做什么用radioButtonText(将它保存到数据库的情况下)
。radioButtonText = selectedRadioButton.getText()的toString();

如果(radioButtonText.matches())
{
    Toast.makeText(getApplicationContext(),请选择性别,Toast.LENGTH_SHORT).show();
    Log.d(QAOD,性别为空);
}
其他
{
    Log.d(QAOD,性别选择);
}
 

解决方案

如果您想检查只是一个单选您可以使用器isChecked 功能

 如果(radioButton.isChecked())
{
  \\检查
}
其他
{
  \\未选中
}
 
Android studio教程 创建app项目

如果你有一个 RadioGroup中您可以使用

 如果(radioGroup.getCheckedRadioButtonId()== -1)
{
  \\没有单选按钮被选中
}
其他
{
  \\的一个单选按钮被选中
}
 

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 for RadioButton in the RadioGroup. I tried this code but didn't work properly. Suggest me correct way. Thankyou.

// get selected radio button from radioGroup
int selectedId = gender.getCheckedRadioButtonId();
// find the radiobutton by returned id
selectedRadioButton = (RadioButton)findViewById(selectedId);
// do what you want with radioButtonText (save it to database in your case)
radioButtonText = selectedRadioButton.getText().toString();

if(radioButtonText.matches(""))
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    Log.d("QAOD", "Gender is Null");
}
else
{
    Log.d("QAOD", "Gender is Selected");
}

解决方案

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
}

 
精彩推荐
图片推荐