当用户分别从广播组中选择男性,女性或未分类时,我想将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";
}
}
});
答案 0 :(得分: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";
}
}
});
答案 1 :(得分:1)
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";
}
}
});
答案 2 :(得分:1)
您应该按照以下方式处理:
// 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 :(得分:1)
试试这个
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;
}
}
});
答案 4 :(得分:0)
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;
}
}
});
}
}