我想在Android应用中进行测验。
我做了一个RadioGroup
来显示每个问题的答案。我希望在代表答案的RadioButton
未被点击时显示,但它不能正常工作。
关注我的代码:
public class MainActivity extends ActionBarActivity {
private RadioGroup radioGroup;
int ans;
private Button btnSubmitQuiz;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
RadioGroup champ=(RadioGroup)findViewById(R.id.answer1);
champ.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.answer1A:
ans1 =0;
break;
case R.id.answer1B:
ans1 =1;
break;
default:
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
文件.xml是这样的:
<RadioGroup
android:id="@+id/answer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4">
<RadioButton
android:id="@+id/answer1A"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/answer1B"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
有人可以帮助我吗?
答案 0 :(得分:0)
将您的Toast消息移至包含错误答案的“case block”
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.answer1A:
ans1 =0;
break;
case R.id.answer1B:
ans1 =1;
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
break;
}
}
});}
假设R.id.answr1B持错了答案