์ฐธ ๋๋ ๊ฑฐ์ง์ ๋๋ฅด๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ๋ฌธ์ ๊ฐ ์ฐธ์ธ์ง ๊ฑฐ์ง์ธ์ง ์๋ ค์ฃผ๋ ์ดํ์ ๋ง๋ค๋ ค๊ณ ํ๋ค.
๋ฌธ์ : ์ง๊ตฌ๋ ํ์ ์ฃผ์๋ฅผ ๋๋ค.
์ ๋ต: ์
๋ฌธ์ : ์์ธ์ ๋ํ๋ฏผ๊ตญ์ ์๋๊ฐ ์๋๋ค.
์ ๋ต: ์๋์ค
๋ฌธ์ : ๋ฌผ์ ํํ์์ผ๋ก 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 ๊ธฐ๋ฅ์ ๋ฃ์๋ค.