카테고리 없음

finish quiz app /정답여부 확인가능케 설정/앞/뒤로 재생 / 코딩

◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e 2020. 8. 23. 01:11
반응형

//questionTextView method로 따로 지정  = 화면에 answer지 띠우기.

//checkAnswer method


//
case R.id.previous_button:  
if(currentQuestionIndex>0){
currentQuestionIndex=(currentQuestionIndex + -1) % questionBank.length; // 무한반복
updateQuestion();
}
break;

 

mainActivity

package com.example.truecitizenquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener { //implement설정됨.

    private Button falseButton;
    private Button trueButton;
    private TextView questionTextView;           //variable 설정    V
    private ImageButton nextButton;             // V
    private ImageButton previousButton;

    private int currentQuestionIndex=0;       // V

    private boolean userChooseCorrect;

    private Question[] questionBank = new Question[]{       // Array define in android studio    V

            new Question(R.string.question_amendments,false),   //call constructor=setter
            new Question(R.string.question_constitution,true),
            new Question(R.string.question_declaration,true),
            new Question(R.string.question_independence_rights,true),
            new Question(R.string.question_religion,true),
            new Question(R.string.question_government,false),
            new Question(R.string.question_government_feds,false),
            new Question(R.string.question_government_senators,true),

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        falseButton = findViewById(R.id.false_button);      // xml 연결
        trueButton = findViewById(R.id.true_button);
        questionTextView =findViewById(R.id.answer_text_view);
        nextButton = findViewById(R.id.next_button);                //      V
        previousButton = findViewById(R.id.previous_button);                //      V


        falseButton.setOnClickListener(this);           //click 한다면 this로 -> make implement 클릭
        trueButton.setOnClickListener(this);
        nextButton.setOnClickListener(this);            //  V
        previousButton.setOnClickListener(this);
    }

    @Override                               //click시 method(->this) implement 설정.
    public void onClick(View view) {
        switch(view.getId()){                  //ID가 있다면
            case R.id.false_button:
                checkAnswer(userChooseCorrect=false);
                break;

            case R.id.true_button:
                checkAnswer(userChooseCorrect=true);
                break;

            case R.id.next_button:                          //     V 다음 재생
                //go to next question

                currentQuestionIndex=(currentQuestionIndex + 1) % questionBank.length; // 무한반복
                updateQuestion();
                break;

            case R.id.previous_button:                          //     V 이전재생
                if(currentQuestionIndex>0){
                    currentQuestionIndex=(currentQuestionIndex + -1) % questionBank.length; // 무한반복
                    updateQuestion();
                }

                break;
    }

    //questionTextView를 method로 따로 지정
    private void updateQuestion(){
        Log.d("Currnet","onClick"+currentQuestionIndex);
        questionTextView.setText(questionBank[currentQuestionIndex].getAnswerResId());
    }

    //checkAnswer method
    private void checkAnswer(boolean userChooseCorrect){
        boolean answerIsTrue = questionBank[currentQuestionIndex].isAnswerTrue();   //true이다
        int toastMessageId = 0;

        if(userChooseCorrect == answerIsTrue){
            toastMessageId = R.string.correct_answer;
        }else{
            toastMessageId = R.string.wrong_answer;
        }

        Toast.makeText(MainActivity.this, toastMessageId, Toast.LENGTH_SHORT).show();   //토스트로 띄우기

    }

}

question class

package com.example.truecitizenquiz;

public class Question {
    private int answerResId;            //variables
    private boolean answerTrue;

    public Question(int answerResId, boolean answerTrue) {      //constructor
        this.answerResId = answerResId;
        this.answerTrue = answerTrue;
    }

                                                    //getter and seeter
    public int getAnswerResId() {
        return answerResId;
    }

    public void setAnswerResId(int answerResId) {
        this.answerResId = answerResId;
    }

    public boolean isAnswerTrue() {
        return answerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        this.answerTrue = answerTrue;
    }
}

string.xml

<resources>
    <string name="app_name">TrueCitizenQuiz</string>
    <string name="question_declaration">The Declaration of Independance was Adpoted in 1776</string>
    <string name="true_text">true</string>
    <string name="false_text">false</string>
    <string name="correct_answer">That\'s correct</string>
    <string name="wrong_answer">That\'s incorrect</string>
    <string name="question_constitution">The Supreme law of the land is the Constitution.</string>
    <string name="question_amendments">The (U.S.) Constitution has 26 Amendments.</string>
    <string name="question_independence_rights">The two rights in the Declaration of Independence are:
        \n \t <b>life</b> \n  \t <b>pursuit of happiness</b>.</string>
    <string name="question_religion">Freedom of religion means:
        \n \t <b>You can practice any religion, or not practice a religion</b>.</string>
    <string name="question_government">Journalists is one branch or part of the government.</string>
    <string name="question_government_feds">Congress does not make federal laws.</string>
    <string name="question_government_senators">There are one hundred (100) U.S. Senators.</string>
    <string name="next_text">next</string>
    <string name="todo">TODO</string>

</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity"
    tools:ignore="ExtraText"
    android:background="@color/colorPrimaryDark">

    <ImageView
        android:src="@drawable/streifenhoernchen"
        android:layout_width="147dp"
        android:layout_height="141dp" />


    <TextView
        android:id="@+id/answer_text_view"
        android:text="@string/question_declaration"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/true_button"
            android:text="@string/true_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/false_button"
            android:text="@string/false_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="34dp"
       android:orientation="horizontal">        <!-- 이미지 버튼 추가-->

       <ImageButton
           android:id="@+id/previous_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           app:srcCompat="@android:drawable/ic_media_previous" />

       <ImageButton
        android:src="@android:drawable/ic_media_next"
        android:contentDescription="@string/todo"
        android:id="@+id/next_button"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="ContentDescription" />


</LinearLayout>

    </LinearLayout>