<resources>
<string name="app_name">True Citizen</string>
<string name="false_button_text">false</string>
<string name="true_button_text">True</string>
<string name="prev_button_text">prev</string>
<string name="next_button_text">next</string>
<string name="placeholder">Question goes here</string>
<string name="image_description">A droid icon image</string>
<string name="question_declaration">The (U.S.) Declaration of Independence was Adopted in 1776.</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>
</resources>
2) 기능 구현하기
목표에서 설명한 순서대로 다음과 같이 기능을 구현해보겠다.
2-1 화면에 퀴즈 출력
Question.java
public class Question {
private int answerResId;
private boolean answerTrue;
public Question(int answerResId, boolean answerTrue) {
this.answerResId = answerResId;
this.answerTrue = answerTrue;
}
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;
}
}
Question 클래스를 새로 생성한다.
생성자를 생성하여 각 퀴즈의 id값(answerResId), 퀴즈의 정답(answerTrue)변수들을 초기화한다.
다른 클래스에서 변수를 수정할 수 있도록 getter/setter를 생성한다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private Question[] questionBank = new Question[] {
//create/instantiate/question object
new Question(R.string.question_amendments, false),
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);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
뷰의 id값을 해당 파일로 가져오기 위해서 findViewById()가 아닌 binding을 사용한다.
퀴즈들을 배열로 선언하기 위해서 배열형태의 questionBank를 생성한다.
string.xml에 있는 문제들의 id이름을 정답과 같이 하나씩 배열의 인스턴스로 생성한다.
setContentView로 화면을 디자인한 리소스 id를 해당 파일에서 사용할 수 있게 한다.