Android

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค ํ€ด์ฆˆ ํ’€๋ฉด ์ •๋‹ต ์•Œ๋ ค์ฃผ๋Š” app ๊ฐœ๋ฐœ

567Rabbit 2024. 6. 5. 16:53

 

์ฐธ ๋˜๋Š” ๊ฑฐ์ง“์„ ๋ˆ„๋ฅด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์ œ๊ฐ€ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ์•Œ๋ ค์ฃผ๋Š” ์–ดํ”Œ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

 

๋ฌธ์ œ: ์ง€๊ตฌ๋Š” ํƒœ์–‘ ์ฃผ์œ„๋ฅผ ๋ˆ๋‹ค.
์ •๋‹ต: ์˜ˆ

๋ฌธ์ œ: ์„œ์šธ์€ ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๊ฐ€ ์•„๋‹ˆ๋‹ค.
์ •๋‹ต: ์•„๋‹ˆ์˜ค

๋ฌธ์ œ: ๋ฌผ์€ ํ™”ํ•™์‹์œผ๋กœ H2O๋ฅผ ๊ฐ€์ง„๋‹ค.
์ •๋‹ต: ์˜ˆ

๋ฌธ์ œ: ๋ถ๊ทน์€ ๋‚จ๊ทน๋ณด๋‹ค ๋”ฐ๋œปํ•˜๋‹ค.
์ •๋‹ต: ์•„๋‹ˆ์˜ค

๋ฌธ์ œ: ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ๊ณต์‹ ์–ธ์–ด๋Š” ์˜์–ด์ด๋‹ค.
์ •๋‹ต: ์•„๋‹ˆ์˜ค

๋ฌธ์ œ: ์‚ฌ๋žŒ์˜ ์‹ฌ์žฅ์€ ํ•˜๋ฃจ์— ์•ฝ 100,000๋ฒˆ ๋›ด๋‹ค.
์ •๋‹ต: ์˜ˆ

๋ฌธ์ œ: ์—๋ฒ ๋ ˆ์ŠคํŠธ ์‚ฐ์€ ์„ธ๊ณ„์—์„œ ๊ฐ€์žฅ ๋†’์€ ์‚ฐ์ด๋‹ค.
์ •๋‹ต: ์˜ˆ

๋ฌธ์ œ: ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋Š” ์‚ผ๊ฐํ˜•์˜ ์„ธ ๋ณ€์˜ ๊ธธ์ด์— ๋Œ€ํ•œ ์ด๋ก ์ด๋‹ค.
์ •๋‹ต: ์˜ˆ

๋ฌธ์ œ: ์•„ํด๋กœ 11ํ˜ธ๋Š” ํ™”์„ฑ์— ์ฐฉ๋ฅ™ํ•œ ์ตœ์ดˆ์˜ ์šฐ์ฃผ์„ ์ด๋‹ค.
์ •๋‹ต: ์•„๋‹ˆ์˜ค

๋ฌธ์ œ: ์ปดํ“จํ„ฐ์˜ CPU๋Š” ์ค‘์•™ ์ฒ˜๋ฆฌ ์žฅ์น˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
์ •๋‹ต: ์˜ˆ

 

 

 

 

 

๋ฌธ์ œ Resource ๋„ฃ๊ธฐ

 

๋จผ์ €, res -> values -> strings.xml ์— ๋ฌธ์ œ resource๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

    <string name="q1">์ง€๊ตฌ๋Š” ํƒœ์–‘ ์ฃผ์œ„๋ฅผ ๋ˆ๋‹ค.</string>
    <string name="q2">์„œ์šธ์€ ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ˆ˜๋„๊ฐ€ ์•„๋‹ˆ๋‹ค.</string>
    <string name="q3">๋ฌผ์€ ํ™”ํ•™์‹์œผ๋กœ H2O๋ฅผ ๊ฐ€์ง„๋‹ค.</string>
    <string name="q4">๋ถ๊ทน์€ ๋‚จ๊ทน๋ณด๋‹ค ๋”ฐ๋œปํ•˜๋‹ค.</string>
    <string name="q5">๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ๊ณต์‹ ์–ธ์–ด๋Š” ์˜์–ด๋‹ค.</string>
    <string name="q6">์‚ฌ๋žŒ์˜ ์‹ฌ์žฅ์€ ํ•˜๋ฃจ์— ์•ฝ 100,000๋ฒˆ ๋›ด๋‹ค.</string>
    <string name="q7">์—๋ฒ ๋ ˆ์ŠคํŠธ ์‚ฐ์€ ์„ธ๊ณ„์—์„œ ๊ฐ€์žฅ ๋†’์€ ์‚ฐ์ด๋‹ค.</string>
    <string name="q8">ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋Š” ์‚ผ๊ฐํ˜•์˜ ์„ธ ๋ณ€์˜ ๊ธธ์ด์— ๋Œ€ํ•œ ์ด๋ก ์ด๋‹ค.</string>
    <string name="q9">์•„ํด๋กœ 11ํ˜ธ๋Š” ํ™”์„ฑ์— ์ฐฉ๋ฅ™ํ•œ ์ตœ์ดˆ์˜ ์šฐ์ฃผ์„ ์ด๋‹ค.</string>
    <string name="q10">์ปดํ“จํ„ฐ์˜ CPU๋Š” ์ค‘์•™ ์ฒ˜๋ฆฌ ์žฅ์น˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.</string>

 

 

 

 

 

Quiz ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

 

 

Quiz ๋ผ๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , Quiz ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

package com.~.quiz.model;

public class Quiz {

    private int question;
    private boolean answer;

    public Quiz(){

    }

    // ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ
    public Quiz(int question, boolean answer) {
        this.question = question;
        this.answer = answer;
    }

    // getter and setter ๋งŒ๋“ค๊ธฐ
    public int getQuestion() {
        return question;
    }

    public void setQuestion(int question) {
        this.question = question;
    }

    public boolean isAnswer() {
        return answer;
    }

    public void setAnswer(boolean answer) {
        this.answer = answer;
    }
}

 

 

 

 

 

ํ™”๋ฉด ๊ฐœ๋ฐœํ•˜๊ธฐ

 

 

ํ™”๋ฉด์„ ๊ฐœ๋ฐœํ•œ๋‹ค. progressBar๋Š” ์ง„ํ–‰์ƒํ™ฉ์„ Bar ํ˜•ํƒœ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์•Œ๋ ค์ฃผ๋Š” Widgets ์ด๋‹ค. 

 

 

 

 

 

๋กœ์ง ๊ฐœ๋ฐœํ•˜๊ธฐ

 

package com.yujinoh.quiz;

import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.yujinoh.quiz.model.Quiz;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    TextView txtQuiz;
    ProgressBar progressBar;
    TextView txtResult;
    Button btnTure;
    Button btnFalse;

    // ํ€ด์ฆˆ ์ •๋‹ต ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
    int count;


    //ํ€ด์ฆˆ 10๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  (๋ฐ์ดํ„ฐ ์ŠคํŠธ๋Ÿญ์ณ)์–ด๋ ˆ์ด๋ฆฌ์ŠคํŠธ ๋งŒ๋“ ๋‹ค.
    ArrayList<Quiz> quizArrayList = new ArrayList<>();
    private int currentQuizIndex;


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

        txtQuiz = findViewById(R.id.txtQuiz);
        progressBar = findViewById(R.id.progressBar);
        txtResult = findViewById(R.id.txtResult);
        btnTure = findViewById(R.id.btnTrue);
        btnFalse = findViewById(R.id.btnFalse);

        // 1. ํ€ด์ฆˆ๋ฅผ ๋งŒ๋“ ๋‹ค !!
        setQuiz();

        // 2. ํ™”๋ฉด์— ํ€ด์ฆˆ๋ฅผ ์ถœ์ œํ•œ๋‹ค.

        Quiz quiz = quizArrayList.get(currentQuizIndex);
        txtQuiz.setText( quiz.getQuestion() );

        // 3. ํ”„๋กœ๊ทธ๋ ˆ์Šค๋ฐ”์˜ ์ง„ํ–‰์ƒํ™ฉ์„ ํ‘œ์‹œํ•œ๋‹ค
        progressBar.setProgress(currentQuizIndex + 1);

        // 4. ์ฐธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ
        btnTure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Quiz quiz = quizArrayList.get(currentQuizIndex);
                if (quiz.isAnswer() == true){
                    txtResult.setText("์ •๋‹ต์ž…๋‹ˆ๋‹ค~~");
                    count = count + 1;
                } else {
                    txtResult.setText("์˜ค๋‹ต์ž…๋‹ˆ๋‹ค~~");
                }

                // ๋‹ค์Œ ๋ฌธ์ œ๋ฅผ ํ™”๋ฉด์— ์ถœ์ œํ•œ๋‹ค
                currentQuizIndex = currentQuizIndex + 1;

                if(currentQuizIndex == quizArrayList.size()){ //10๊ฐœ ๋‹คํ’€๋ฉด ํŒ์—…์ฐฝ ๋œจ๊ฒŒ ํ•˜๊ธฐ
                    showAlertDialog();
                    return;
                }

                quiz = quizArrayList.get(currentQuizIndex);
                txtQuiz.setText(quiz.getQuestion());

                // ํ”„๋กœ๊ทธ๋ ˆ์Šค ๋ฐ”๋„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
                progressBar.setProgress(currentQuizIndex + 1);
            }
        });

        btnFalse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Quiz quiz = quizArrayList.get(currentQuizIndex);
                if(quiz.isAnswer() == false){
                    txtResult.setText("์ •๋‹ต์ž…๋‹ˆ๋‹ค~~");
                    count = count + 1;
                } else {
                    txtResult.setText("์˜ค๋‹ต์ž…๋‹ˆ๋‹ค~~");
                }

                // ๋‹ค์Œ ๋ฌธ์ œ๋ฅผ ํ™”๋ฉด์— ์ถœ์ œํ•œ๋‹ค
                currentQuizIndex = currentQuizIndex + 1;

                if(currentQuizIndex == quizArrayList.size()){ //10๊ฐœ ๋‹คํ’€๋ฉด ํŒ์—…์ฐฝ ๋œจ๊ฒŒ ํ•˜๊ธฐ
                    showAlertDialog();
                    return;
                }

                quiz = quizArrayList.get(currentQuizIndex);
                txtQuiz.setText(quiz.getQuestion());

                // ํ”„๋กœ๊ทธ๋ ˆ์Šค ๋ฐ”๋„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
                progressBar.setProgress(currentQuizIndex + 1);

            }
        });


    }

    private void showAlertDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setCancelable(false);
        builder.setTitle("ํ€ด์ฆˆ ๋!");
        builder.setMessage("์ง€๊ธˆ๊นŒ์ง€ ๋งž์ถ˜ ๋ฌธ์ œ๋Š” " + count
                + "๊ฐœ ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œํ’€๊ธฐ๋ฅผ ๋ˆ„๋ฅด์‹œ๋ฉด ํ€ด์ฆˆ๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘๋˜๊ณ ,์ข…๋ฃŒ๋ฅผ ๋ˆ„๋ฅด์‹œ๋ฉด ํ€ด์ฆˆ๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.");
        builder.setPositiveButton("๋‹ค์‹œํ’€๊ธฐ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                // ๋‹ค์‹œํ’€๊ธฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ค˜์•ผ ํ•œ๋‹ค
                currentQuizIndex = 0;

                // ๋งž์ถ˜ ๋ฌธ์ œ ๊ฐฏ์ˆ˜๋„ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.
                count = 0;

                // 1. ์ฒซ๋ฒˆ์งธ ๋ฌธ์ œ๊ฐ€ ๋‹ค์‹œ ํ™”๋ฉด์— ๋‚˜์™€์•ผ ํ•œ๋‹ค.
                Quiz quiz = quizArrayList.get(currentQuizIndex);
                txtQuiz.setText(quiz.getQuestion());

                // 2. ํ”„๋กœ๊ทธ๋ ˆ์Šค๋ฐ”๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
                progressBar.setProgress(currentQuizIndex + 1);

                // 3. ๊ฒฐ๊ณผ ํ™”๋ฉด๋„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
                txtResult.setText("๊ฒฐ๊ณผ");

            }
        });
        builder.setNegativeButton("์ข…๋ฃŒ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // ํ˜„์žฌ์˜ ๋ฉ”์ธ์•กํ‹ฐ๋น„ํ‹ฐ(MainActivity)๋ฅผ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜ finish() (์ค‘์š”)
                finish();
            }
        });
        builder.show();
    }

    private void setQuiz() {
        Quiz q = new Quiz(R.string.q1 , true);
        quizArrayList.add(q);

        q = new Quiz(R.string.q2 , false);
        quizArrayList.add(q);

        q = new Quiz(R.string.q3 , true);
        quizArrayList.add(q);

        q = new Quiz(R.string.q4 , false);
        quizArrayList.add(q);

        q = new Quiz(R.string.q5 , false);
        quizArrayList.add(q);

        q = new Quiz(R.string.q6 , true);
        quizArrayList.add(q);

        q = new Quiz(R.string.q7 , true);
        quizArrayList.add(q);

        q = new Quiz(R.string.q8 , true);
        quizArrayList.add(q);

        q = new Quiz(R.string.q9 , false);
        quizArrayList.add(q);

        q = new Quiz(R.string.q10 , true);
        quizArrayList.add(q);
    }
}

 

 

 

 

 

๋‹ค ํ’€๋ฉด ( ์ฆ‰, currentQuizIndex๋Š” ํ˜„์žฌ ํ’€๊ณ ์žˆ๋Š” ๋ฌธ์ œ ๊ฐœ์ˆ˜๊ฐ€ quizArrayList.size()์ธ 10์— ๋„๋‹ฌํ•˜๋ฉด )

 

๊ฒฝ๊ณ  ํŒ์—…์ฐฝ์ด ๋œจ๋„๋ก AlertDialog ๊ธฐ๋Šฅ์„ ๋„ฃ์—ˆ๋‹ค.