这可能不是实现以下目标的最佳方式,我知道这一点并对任何建议持开放态度。
目标:在同一活动中以后续顺序创建多项选择/真假型问题。
理想状态:以上在可重用的类中。 (由于下面代码中(findViewById)的必要性,我未能实现这一目标)
问题:测验逻辑不起作用。问题1将接受正确的答案,但不会在正确的答案中正确更改结果文本,但会将所有答案更改为重试。问题2不接受
我尝试过的事情:因为,虽然,切换 - 所有都以类似的方式设置。
{{1}}
老实说整个事情都很邋..我喜欢不同活动之间过渡的外观,并希望看到类似的东西发生在这里,而不是只是看不见。
任何建议都表示赞赏。谢谢。
答案 0 :(得分:1)
以下内容应为您提供一些指导方针,以构建一个可以帮助您更多地使用调查问卷的UI。我们在这里标记RadioGroup小部件的使用。使用RadioGroup,您可以遍历子对象。在这种情况下,我在RadioGroup中有几个RadioButton。以下是简单的RadioGroup,例如:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton3"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton4"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RadioGroup>
RadioGroup有一些很好的帮助方法来遍历子视图。你可以通过给RadioGroup提供最多数量的按钮来扩展它,甚至可以在你的代码中动态添加它们。
我在代码中的注释应该给你足够的方向来知道在哪里注入问题主持人逻辑。
/**
* Our RadioGroup
*/
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//gather our reference to the RadioGroup
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(radioListener);
}
@Override
protected void onResume() {
super.onResume();
//build our first question
buildQuestion(0);
}
/**
* This is how we will capture our User's response. Once they click on a radio button,
* the response can immediately be checked if it is correct.
* <p/>
* we can modify the accessor method submitAnswer(int) to be something like collectAnswer(int) instead.
* Then with the use of a button on the screen which the user can use to submit their answer.
*/
RadioGroup.OnCheckedChangeListener radioListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
default:
Log.w(TAG, "No Support for questions with more than 4 possible answers. checkId: " + checkedId);
break;
case R.id.radioButton4:
Log.i(TAG, "Button 4");
submitAnswer(3);
break;
case R.id.radioButton3:
Log.i(TAG, "Button 3");
submitAnswer(2);
break;
case R.id.radioButton2:
Log.i(TAG, "Button 2");
submitAnswer(1);
break;
case R.id.radioButton:
Log.i(TAG, "Button 1");
submitAnswer(0);
break;
}
}
};
/**
* Build and Display question for user.
*
* @param question the position which question whould be shown to the user.
*/
private void buildQuestion(int question) {
//this method would set and display your question
displayQuestionText(question);
//this would gather your answers to display to your user.
String[] orderedAnswers = displayPossibleAnswers(question);
for (int i = 0; i < radioGroup.getChildCount(); i++) {
View o = radioGroup.getChildAt(i);
if (o instanceof RadioButton) {
if(i < orderedAnswers.length) {
((RadioButton) o).setText(orderedAnswers[i]);
((RadioButton) o).setVisibility(View.Visible);
} else {
((RadioButton) o).setText("");
((RadioButton) o).setVisibility(View.Gone);
}
}
}
}
/**
* Submit user's answer. This also handles the return of checking answer to display to the user
* whether they got the question correct or incorrect.
*
* @param i position of user's answer
*/
private void submitAnswer(int i) {
//some method to check if this is the right answer
if (checkAnswer(i)) {
//user selected correct answer
textGetStartedReviewResult.setText("Correct!");
Toast.makeText(GetStarted.this, "Great job. Here's the next question.", Toast.LENGTH_SHORT).show();
buildQuestion(getNextQuestion());
} else {
//user was incorrect. Say something encouraging to them.
textGetStartedReviewResult.setText("Try Again.");
}
}