我目前正在进行Android多项测验游戏,但我对如何在TextView上显示所有六个问题以及在按钮上显示四个问题感到困惑?另外,我不确定如何在Android中匹配正确/不正确的答案(因为它涉及Android语法)。
PS:当玩家按下按钮时,不正确的玩家是否会移动到下一个新问题+答案,如果我想将问题和答案存储在txt文件中,是否有可能?
这是我到目前为止所做的......
public class play extends Activity实现OnClickListener {
private int correctanswers;
private TextView questionstextview;
private TextView questionnumber;
private TextView playerfeedback;
private int totalanswer;
private int score;
private List<Question> QuestionList;
Button answer1,answer2,answer3,answer4;
Button AnswerButtons [] = {answer1,answer2,answer3,answer4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
QuestionList = new ArrayList<Question>();
ArrayList <String> answer = new ArrayList<String>();
answer.add("8");
answer.add("9");
answer.add("3");
answer.add("1");
QuestionList.add(new Question("what is 4+4", answer, 0));
answer.add("17");
answer.add("20");
answer.add("15");
answer.add("14");
QuestionList.add(new Question("what is 7+8?", answer, 3));
answer.add("20");
answer.add("30");
answer.add("19");
answer.add("34");
QuestionList.add(new Question("what is 10+10?", answer, 0));
answer.add("12");
answer.add("11");
answer.add("13");
answer.add("14");
QuestionList.add(new Question("what is 6+6?", answer, 0));
answer.add("6");
answer.add("5");
answer.add("4");
answer.add("7");
QuestionList.add(new Question("what is 4+3?", answer, 3));
answer.add("7");
answer.add("9");
answer.add("10");
answer.add("11");
QuestionList.add(new Question("what is 3+7?", answer, 2));
questionstextview = (TextView) findViewById (R.id.questionstextview);
questionnumber = (TextView) findViewById (R.id.questionnumber);
View AnswerButton1 = findViewById(R.id.answerbutton1);
AnswerButton1.setOnClickListener(this);
View AnswerButton2 = findViewById(R.id.answerbutton2);
AnswerButton2.setOnClickListener(this);
View AnswerButton3 = findViewById(R.id.answerbutton3);
AnswerButton3.setOnClickListener(this);
View AnswerButton4 = findViewById(R.id.answerbutton4);
AnswerButton4.setOnClickListener(this);
}
private void ButtonPress (Button answerButton){
}
public play() {
}
@Override
public void onClick(View v) {
}
答案 0 :(得分:0)
使用单选按钮选择正确的答案,然后使用按钮继续下一个问题。!
优势
xml活动布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.stackoverflow.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/rad_btn_group1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked" >
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/radio_button1" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="@string/radio_button2" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="@string/radio_button3" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button4"
android:layout_marginTop="21dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="@string/btn_submit" />
</RadioGroup>
</RelativeLayout>
游戏逻辑的java代码如下:
public class MainActivity extends Activity implements OnClickListener{
Button btn ;
RadioGroup selectionGroup;
public static int score = 0;//to access it in other class as well to ++ or --
boolean answerAttempt = false; //to penalize player changing the answer ;)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
selectionGroup = (RadioGroup) findViewById(R.id.rad_btn_group1);
selectionGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//because we know radiobutton3 is contains the right answer :)
if(answerAttempt)
{
score -= 2;
}
else if (R.id.radioButton3 == checkedId && !answerAttempt)
{
++score;
answerAttempt = true; //meaning first attempt is made by the player
}
else
{
--score;
answerAttempt = true; //meaning first attempt is made by the player
}
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())//this is the id of the button clicked (in our case submit button)
{
case R.id.button1://this is the id of the submit button in xml
//start another activity from here for next question on another activity :)
break;
}
}
}
修改强>
自动转到下一个活动的代码
e@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//because we know radiobutton3 is contains the right answer :)
if (R.id.radioButton3 == checkedId)
{
++score;
//goes to the next question :)
startActivity(new Intent(getBaseContext(), next_question_activity.class));
}
else
{
//goes to the next question without troubling the score :)
startActivity(new Intent(getBaseContext(), next_question_activity.class));
}
}
});