在 Java 的 Switch 语句中更改 String 的值
Changing the value of String in Switch statement in Java
我正在尝试更改 Android 中 switch 语句 中字符串数据类型的值。该字符串在声明 switch 语句的同一方法中声明,当用户 单击单选按钮时,我需要 更改字符串 的值.
public void CreateQuestion(View view) {
String questionType = new String();
RadioGroup questionTypeRadioGroup = (RadioGroup) findViewById(R.id.questionTypeRadioGroup);
questionTypeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.true_false:
questionType = "True/False";
break;
case R.id.mcqs:
questionType = "MCQs";
break;
}
}
});
}
你应该全局声明你的字符串
public class ExampleActivity extends AppCompatActivity {
public String questionType;
....
public void CreateQuestion(View view) {
questionType = "";
questionTypeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.true_false:
questionType = "True/False";
break;
case R.id.mcqs:
questionType = "MCQs";
break;
}
}
});
}
....
}
我正在尝试更改 Android 中 switch 语句 中字符串数据类型的值。该字符串在声明 switch 语句的同一方法中声明,当用户 单击单选按钮时,我需要 更改字符串 的值.
public void CreateQuestion(View view) {
String questionType = new String();
RadioGroup questionTypeRadioGroup = (RadioGroup) findViewById(R.id.questionTypeRadioGroup);
questionTypeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.true_false:
questionType = "True/False";
break;
case R.id.mcqs:
questionType = "MCQs";
break;
}
}
});
}
你应该全局声明你的字符串
public class ExampleActivity extends AppCompatActivity {
public String questionType;
....
public void CreateQuestion(View view) {
questionType = "";
questionTypeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.true_false:
questionType = "True/False";
break;
case R.id.mcqs:
questionType = "MCQs";
break;
}
}
});
}
....
}