在从单选组中选择单选按钮时设置变量值
Posted
技术标签:
【中文标题】在从单选组中选择单选按钮时设置变量值【英文标题】:Set a variable value on select a radio button from radio group 【发布时间】:2018-03-08 17:38:25 【问题描述】:当用户从无线电组中分别选择男性、女性或未分类时,我想将字符串变量值设置为 1,2,3。
现在我正在这样做,但即使我选择了一个单选按钮,它也会将变量值设置为null
代码:
RadioGroup genderGroup;
RadioButton male, female, unclassified;
String strGender;
genderGroup = findViewById(R.id.genderGroup);
male = findViewById(R.id.genderMale);
female = findViewById(R.id.genderFemale);
unclassified = findViewById(R.id.genderUnclassified);
if (genderGroup.getCheckedRadioButtonId() == -1)
strGender = null; //no buttons selected
else
if (male.isChecked()) // one of the radio buttons is checked
strGender = "1";
else if (female.isChecked())
strGender = "2";
else if (unclassified.isChecked())
strGender = "3";
当我在选择任何单选按钮后打印该值时,它显示为空。 如果你知道怎么做,请帮忙:)谢谢。
已解决感谢 Nilesh。 正确的代码是:
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
if (checkedId == R.id.genderMale)
strGender = "1";
else if (checkedId == R.id.genderFemale)
strGender = "2";
);
【问题讨论】:
【参考方案1】:你需要使用RadioGroup.OnCheckedChangeListener
此组中选中的单选按钮更改时要调用的回调的接口定义。
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
if (checkedId == R.id.genderMale)
strGender = "1";
else if (checkedId == R.id.genderFemale)
strGender = "2";
);
【讨论】:
【参考方案2】:您应该按如下方式处理:
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId())
case R.id.genderMale:
if (checked)
strGender = 1;
break;
case R.id.genderFemale:
if (checked)
strGender = 2;
break;
case R.id.genderUnclassified:
if (checked)
strGender = 3;
break;
【讨论】:
【参考方案3】:genderGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
// find which radio button is selected
if(checkedId == R.id.genderMale)
strGender = "1";
else if(checkedId == R.id.genderFemale)
strGender = "2";
else
strGender = "3";
);
【讨论】:
【参考方案4】:试试这个
String strGender =" ";
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
switch(checkedId)
case R.id.genderMale:
strGender = "1";
break;
case R.id.genderFemale:
strGender = "2";
break;
case R.id.genderUnclassified:
strGender = "3";
break;
);
【讨论】:
【参考方案5】:public class MainActivity extends Activity
RadioGroup genderGroup;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup genderGroup = (RadioGroup) findViewById(R.id.radioGroup1);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
public void onCheckedChanged(RadioGroup group, int checkedId)
switch(checkedId)
case R.id.radio0:
// do operations specific to this selection
strGender = "1";
break;
case R.id.radio1:
// do operations specific to this selection
strGender = "2";
break;
case R.id.radio2:
// do operations specific to this selection
strGender = "3";
break;
);
【讨论】:
以上是关于在从单选组中选择单选按钮时设置变量值的主要内容,如果未能解决你的问题,请参考以下文章
如何检查单选按钮是不是在 Android 的单选组中被选中?