设置变量值,从无线电组中选择一个单选按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设置变量值,从无线电组中选择一个单选按钮相关的知识,希望对你有一定的参考价值。
当用户分别从广播组中选择男性,女性或未分类时,我想将String变量值设置为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";
}
}
当我在选择任何单选按钮后打印该值时,它显示为null。如果你知道怎么做,请帮助:)谢谢。
解决了感谢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";
}
}
});
答案
你需要使用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";
}
}
});
另一答案
你应该处理这个,如下所示:
// 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;
}
另一答案
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";
}
}
});
另一答案
试试这个
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;
}
}
});
另一答案
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;
}
}
});
}
}
以上是关于设置变量值,从无线电组中选择一个单选按钮的主要内容,如果未能解决你的问题,请参考以下文章