简体   繁体   中英

Display array on Android button and textview

i am currently doing Android multiple quiz game but i am a bit confused on how to display all the six Questions on a TextView and four Answers on Buttons? also, im not sure how to match the correct/incorrect answer in Android (because it involves Android Syntax).

PS: when player press the button whether its correct of incorrect player will move to the next new questions +answers and would it be possible if i want to store the questions and answers on txt file?

here is what i have done so far...

public class play extends Activity implements 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) {


}

use Radio buttons for selecting the right answer and use the button to proceed to the next question.!

Advantages

  1. the movement from one activity to other will be more real (as if turning a page)
  2. gives a feeling of moving one level up !! ;)
  3. most important of all easier to implement (well i find it easier that way)

xml Code for the activity layout:

<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 code for the game logic is as follows:

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;
    }

}
}

Edit

code for automatically going to next activity

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));

        }

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM