我需要设置验证,用户必须填写/选择页面中的所有详细信息。如果有任何字段是空的,现在想要显示Toast message to fill.
,我需要在RadioGroup.
中为RadioButton
设置验证,我尝试了这段代码,但没有正常工作。请告诉我正确的方法。谢谢你。
// 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");
}
发布于 2014-07-28 18:15:36
如果只想检查一个RadioButton
,可以使用isChecked
函数
if(radioButton.isChecked())
{
// is checked
}
else
{
// not checked
}
如果你有一个RadioGroup
,你可以使用
if (radioGroup.getCheckedRadioButtonId() == -1)
{
// no radio buttons are checked
}
else
{
// one of the radio buttons is checked
}
发布于 2014-07-28 18:15:47
您所需要做的就是使用getCheckedRadioButtonId()
和isChecked()
方法,
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
发布于 2019-08-02 18:09:06
rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i) {
case R.id.type_car:
num=1;
Toast.makeText(getApplicationContext()," Car",Toast.LENGTH_LONG).show();
break;
case R.id.type_bike:
num=2;
Toast.makeText(getApplicationContext()," Bike",Toast.LENGTH_LONG).show();
break;
}
}
});
https://stackoverflow.com/questions/24992936
复制相似问题