https://play.google.com/store/apps/details?id=com.jw.quizshow
안드로이드 앱: 국기 맞추기 퀴즈 앱 만들기 소스코드
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_flag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/b_answer1"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/b_answer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/b_answer2"
android:layout_centerHorizontal="true"
android:text=""/>
<Button
android:id="@+id/b_answer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/b_answer3"
android:text=""/>
<Button
android:id="@+id/b_answer3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/b_answer4"
android:layout_centerHorizontal="true"
android:text="" />
<Button
android:id="@+id/b_answer4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text=""/>
</RelativeLayout>
Java Code - CountryItem
package com.jwlee.flagquiz;
public class CountryItem {
String name;
int image;
public CountryItem(String name, int image){
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
}
}
Java Code - Database
package com.jwlee.flagquiz;
public class Database {
Integer[] flags = {
R.drawable.china,
R.drawable.uk,
R.drawable.korea,
R.drawable.usa
};
String[] answers = {
"china",
"uk",
"korea",
"usa"
};
}
https://play.google.com/store/apps/details?id=com.jw.quizshow
Java Code - MainActivity
package com.jwlee.flagquiz;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button b_answer1, b_answer2, b_answer3, b_answer4;
ImageView iv_flag;
List<CountryItem> list;
Random r;
int turn = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = new Random();
iv_flag = findViewById(R.id.iv_flag);
b_answer1 = findViewById(R.id.b_answer1);
b_answer2 = findViewById(R.id.b_answer2);
b_answer3 = findViewById(R.id.b_answer3);
b_answer4 = findViewById(R.id.b_answer4);
list = new ArrayList<>();
// 국기와 국명 넣기
for (int i = 0; i < new Database().answers.length; i++) {
list.add(new CountryItem(new Database().answers[i], new Database().flags[i]));
}
//랜덤
Collections.shuffle(list);
newQuestion(turn);
b_answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 정답 체크
if (b_answer1.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You finished the game!", Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(MainActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You lost the game!", Toast.LENGTH_SHORT).show();
finish();
}
}
}
});
b_answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 정답 체크
if (b_answer2.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You finished the game!", Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(MainActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You lost the game!", Toast.LENGTH_SHORT).show();
finish();
}
}
}
});
b_answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 정답 체크
if (b_answer3.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You finished the game!", Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(MainActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You lost the game!", Toast.LENGTH_SHORT).show();
finish();
}
}
}
});
b_answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 정답 체크
if (b_answer4.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You finished the game!", Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(MainActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
//마지막 문제 체크
if (turn < list.size()) {
turn++;
newQuestion(turn);
} else {
Toast.makeText(MainActivity.this, "You lost the game!", Toast.LENGTH_SHORT).show();
finish();
}
}
}
});
}
private void newQuestion(int number) {
// 국기 이미지를 스크린에 세팅한다.
iv_flag.setImageResource(list.get(number - 1).getImage());
int correct_answer = r.nextInt(4) + 1;
int firstButton = number - 1;
int secondButton;
int thirdButton;
int forthButton;
switch (correct_answer) {
case 1:
b_answer1.setText(list.get(firstButton).getName());
do {
secondButton = r.nextInt(list.size());
} while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
forthButton = r.nextInt(list.size());
} while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);
b_answer2.setText(list.get(secondButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
b_answer4.setText(list.get(forthButton).getName());
break;
case 2:
b_answer2.setText(list.get(firstButton).getName());
do {
secondButton = r.nextInt(list.size());
} while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
forthButton = r.nextInt(list.size());
} while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);
b_answer1.setText(list.get(secondButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
b_answer4.setText(list.get(forthButton).getName());
break;
case 3:
b_answer3.setText(list.get(firstButton).getName());
do {
secondButton = r.nextInt(list.size());
} while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
forthButton = r.nextInt(list.size());
} while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);
b_answer2.setText(list.get(secondButton).getName());
b_answer1.setText(list.get(thirdButton).getName());
b_answer4.setText(list.get(forthButton).getName());
break;
case 4:
b_answer4.setText(list.get(firstButton).getName());
do {
secondButton = r.nextInt(list.size());
} while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
forthButton = r.nextInt(list.size());
} while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);
b_answer2.setText(list.get(secondButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
b_answer1.setText(list.get(forthButton).getName());
break;
}
}
}
https://play.google.com/store/apps/details?id=com.jw.quizshow
'기타 ETC > Android Studio' 카테고리의 다른 글
안드로이드 LinearLayout 오른쪽 정렬하기, 왼쪽 정렬하기 (0) | 2020.07.02 |
---|---|
안드로이드 사운드 재생 방법 및 버튼 색깔 바꾸기입니다. (0) | 2020.06.24 |
안드로이드 형 변환하기 (0) | 2020.03.27 |
안드로이드 영수증 PDF 만들기 (0) | 2020.03.19 |
EditText에 숫자만 입력받기 (0) | 2020.03.19 |