https://velog.io/@jojo_devstory/안드로이드-아키텍쳐-패턴-MVC가-뭘까
MVC : model - view -controller ......design pattern같은
model : sort of data
예시) Quiz app의 MVC
model : quiz(question).................question class - java class(main activity ->new class) | |
view : ImageView TextView Buttons...............activity_main.xml? | |
conteroller : MainActivity...........출력() |
M : quextion class 만들기
C : string들 question add(array) - call constructor(=setter)
V : showing
(> another next button
or ImageButton 추가 : 보통 contentDescription도 같이 추가: 해당 이미지 버튼이 뭔지 설명해주는
>버튼 누르면 question나오게 설정)
>Index = (Index+1)%array.length : 마지막 index도달하면 다시 0 index로 돌아간다 : keep going inside.
1%8....1
2%8....2
8%8....0-> 원래 시작점으로 돌아감.
currentQuestionIndex=(currentQuestionIndex + 1) % questionBank.length; // 무한반복
1. M : quextion 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;
}
}
2. C : string들 question add(array) - call constructor(=setter)
V : showing
<sting xml>에 string data 추가
<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>
</resources>
MainActivity (string array defind/ imagebutton xml showing)
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 int currentQuestionIndex = 0; // V
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
falseButton.setOnClickListener(this); //click 한다면 this로 -> make implement 클릭
trueButton.setOnClickListener(this);
nextButton.setOnClickListener(this); // V
}
@Override //click시 method(->this) implement 설정.
public void onClick(View view) {
switch(view.getId()){ //ID가 있다면
case R.id.false_button:
Toast.makeText(MainActivity.this,"False",Toast.LENGTH_SHORT).show();
break;
case R.id.true_button:
Toast.makeText(MainActivity.this, "true", Toast.LENGTH_SHORT).show();
case R.id.next_button: // V
//go to next question
currentQuestionIndex=(currentQuestionIndex + 1) % questionBank.length; // 무한반복
Log.d("Current","onClick : "+currentQuestionIndex);
questionTextView.setText(questionBank[currentQuestionIndex].getAnswerResId());
}
}
}
index 7 다음에 다시 index 0으로
3. 참고 ImageButton 추가한거 ... main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"
tools:ignore="ExtraText"
android:background="@color/colorPrimaryDark">
<ImageView
android:src="@drawable/ic_launcher_foreground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<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>
<!-- 이미지 버튼 추가 V-->
<ImageButton
android:contentDescription="@string/next_text"
android:id="@+id/next_button"
android:src="@android:drawable/ic_media_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
'AndroidStudio' 카테고리의 다른 글
Music 1. MediaPlayer vs SoundPool (0) | 2021.01.08 |
---|---|
9.1 quiz app / Linear layout 내부에 추가(버튼추가) / 코딩 (0) | 2020.08.15 |
9. quiz app / Linear layout / 코딩 (0) | 2020.08.13 |
5.4 조건부 글자색 변경 / 코딩 (0) | 2020.08.09 |
2. 클릭하면 name 입력값 나오는 코딩. (0) | 2020.08.01 |