Skip to content

Commit

Permalink
Complete chapter 2 witout challenges,完成第二章,未完成挑战
Browse files Browse the repository at this point in the history
  • Loading branch information
kniost committed Mar 13, 2017
1 parent 5c5aed2 commit ac96c63
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 6 deletions.
28 changes: 28 additions & 0 deletions GeoQuiz/app/src/main/java/com/kniost/geoquiz/Question.java
@@ -0,0 +1,28 @@
package com.kniost.geoquiz;

public class Question {

private int mTextResId;
private boolean mAnswerTrue;

public Question(int textResId, boolean answerTrue) {
mTextResId = textResId;
mAnswerTrue = answerTrue;
}

public int getTextResId() {
return mTextResId;
}

public void setTextResId(int textResId) {
mTextResId = textResId;
}

public boolean isAnswerTrue() {
return mAnswerTrue;
}

public void setAnswerTrue(boolean answerTrue) {
mAnswerTrue = answerTrue;
}
}
49 changes: 47 additions & 2 deletions GeoQuiz/app/src/main/java/com/kniost/geoquiz/QuizActivity.java
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {
Expand All @@ -12,27 +13,71 @@ public class QuizActivity extends AppCompatActivity {
//然后在 Acitvity 被创建,并在 OnCreate() 函数中关联起来
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;

private Question[] mQuestionBank = new Question[]{
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};

private int mCurrentIndex = 0;

private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}

private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

int messageResId = 0;

if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}

Toast.makeText(QuizActivity.this, messageResId, Toast.LENGTH_SHORT).show();
}

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

mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

mTrueButton = (Button) findViewById(R.id.true_button);
//本书均使用匿名内部类的方法实现监听器
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
checkAnswer(true);
}
});

mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
checkAnswer(false);
}
});

mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});

updateQuestion();
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion GeoQuiz/app/src/main/res/layout/activity_quiz.xml
Expand Up @@ -12,7 +12,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text"
/>

<LinearLayout
Expand All @@ -33,4 +32,13 @@
android:text="@string/false_button"/>
</LinearLayout>

<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"
android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp"
/>

</LinearLayout>
10 changes: 7 additions & 3 deletions GeoQuiz/app/src/main/res/values/strings.xml
@@ -1,11 +1,15 @@
<resources>
<string name="app_name">GeoQuiz</string>

<string name="question_text">
Constantinople is the largest city in Turkey.
</string>
<string name="true_button">TRUE</string>
<string name="false_button">False</string>
<string name="next_button">NEXT</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>

<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river in the Americas</string>
<string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
</resources>

0 comments on commit ac96c63

Please sign in to comment.