编写一个答题的程序,要求可以对所选答案进行反馈是否回答正确,并且能够前进后退选择前后问题。
首先构思整体格局,问题题设放在主界面的上方,选项放在中间,切换问题的按键分别放在左右下角。
下面编写各部分代码(相应功能用法写在了代码旁的注释里面,在这里就不再赘叙):
首先是QuizActivity.java:
QuizActivity.java
package com.bignerdranch.android.mygeoquiz;
//AppCompatActivity是一个Activity子类,能为Android就版本系统提供兼容支持
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
//设置主界面上的四个按键,每一个按键都称为一个成员
private Button mTurueButton;
private Button mFauseButton;
private Button mNextButton;
private Button mPrevButton;
private TextView mQuestionTextView;
private static final String TAG = "QuizActivity";
//问题的对象数组,数据结构是前面一段字符串存放问题,后面用布尔型变量answerTrue存放问题的答案
//在这里我们多次调用了Question类的构造方法,具体代码参见Question.java
private Question[] mQuestionBank = new Question[]{
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_aferica, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};
private int mCurrentIndex = 0;
@Override//检查这种标号的方法和父类的相应方法同名,同参数类型,同返回类型
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
// int question = mQuestionBank[mCurrentIndex].getTextResId();
// mQuestionTextView.setText(question);
mTurueButton = (Button) findViewById(R.id.true_button);
//设置一个监听器,当按钮mTrueButton被点击后,监听器会立即通知我们。
mTurueButton.setOnClickListener(new View.OnClickListener() {
@Override//使用匿名内部类,可以相对集中地实现监听器方法,且不用创建繁琐的命名类
public void onClick(View view) {
// Toast.makeText(QuizActivity.this,
// R.string.correct_toast,
// Toast.LENGTH_SHORT).show();
checkAnswer(true);
}
});
mFauseButton = (Button) findViewById(R.id.false_button);
mFauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(QuizActivity.this,
// R.string.incorrect_toast,
// Toast.LENGTH_SHORT).show();
checkAnswer(false);
}
});
mNextButton=(Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
// int question=mQuestionBank[mCurrentIndex].getTextResId();
// mQuestionTextView.setText(question);
updateQuestion();
}
});
mPrevButton=(Button) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex=(mCurrentIndex-1+mQuestionBank.length)%mQuestionBank.length;
// int question=mQuestionBank[mCurrentIndex].getTextResId();
// mQuestionTextView.setText(question);
updateQuestion();
}
});
updateQuestion();
}
@Override
protected void onStart(){
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
protected void onResume(){
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
protected void onPause(){
super.onPause();
Log.d(TAG,"onPause() called");
}
@Override
protected void onStop(){
super.onStop();
Log.d(TAG,"onStop() called");
}
protected void onDestory(){
super.onDestroy();
Log.d(TAG,"onPause() called");
}
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(this,messageResId, Toast.LENGTH_SHORT)
.show();
}
}
创建的新类Question.java在QuizActivity.java中会被调用:
package com.bignerdranch.android.mygeoquiz;
/**
* Created by cai on 4/18/18.
*/
//创建一个新类Question封装问题文本和答案两部分数据
public class Question {
private int mTextResId;//此变量用于保存问题字符串的ID,所以用int型而非string型
private boolean mAnswerTrue;
public Question(int textResId, boolean answerTrue) {
mTextResId = textResId;
mAnswerTrue = answerTrue;
}
//为每一个变量生成getter方法和setter方法
public int getTextResId() {
return mTextResId;
}
public void setTextResId(int textResId) {
mTextResId = textResId;
}
public boolean isAnswerTrue() {
return mAnswerTrue;
}
public void setAnswerTrue(boolean answerTrue) {
mAnswerTrue = answerTrue;
}
}
activity_quiz.xml为主界面的控制组件:
<!--
<RelativeLayout–>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_quiz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.bignerdranch.android.mygeoquiz.QuizActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello! World!"/>
</RelativeLayout>
-->
<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:gravity="center"-->
<!--android:orientation="vertical" >-->
//修改Layout属性使其在水平横置的时候也能正常显示
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
//问题的组件,控制问题部分的位置和所占用面积,horizontal表示水平布置
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"//在横置时置于屏幕中间
android:padding="24dp" ></TextView>
<!--android:text="@string/question_text"-->
//在XML资源文件中通过资源类型和资源名称,可以引用其他资源。以@string开头的定义是引用字符串资源
//以@drawable开头的定义是引用drawable资源
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
//几个Button控制按钮的组件,wrap_content表示视图将根据其显示的内容自动调整大小
//如果layout_*的值为match_parent则表示视图与其父视图大小相同,类似于继承的概念
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
<Button/>//回退按钮
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"//位置放在左下角
android:text="@string/prev_button"
android:drawableLeft="@drawable/arrow_left"//为了美观,给按钮加了一个左箭头
android:drawablePadding="4dp"/>
<Button//前进按钮
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="@string/next_button"
android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp"/>
<!--</LinearLayout>-->
</FrameLayout>
string.xml控制问题字符串的布局:
<resources>
<string name="app_name">MyGeoQuiz</string>
<!--<string name="question_text">CAI is a handsome boy.</string>-->
<string name="question_australia">Canberra 是澳大利亚的首都</string>
<string name="question_oceans">太平洋比大西洋面积大</string>
<string name="question_mideast">Suez Canal 连接红海和印度洋.</string>
<string name="question_aferica">Nile River 的源头是埃及.</string>
<string name="question_americas">The Amazon River 是美国最长的河流.</string>
<string name="question_asia">Lake Baika 是世界上最古老、最深的淡水湖.</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="correct_toast">Correct</string>//正确的提示消息,当所点击按钮的值为真时,显示Correct
<string name="next_button">Next</string>
<string name="prev_button">Prev</string>
<string name="incorrect_toast">Incorrect</string>//错误的提示消息,当所点击按钮的值为假时,显示Incorrect
</resources>
运行结果:
运行成功,并且可以作出反馈提示
编写了一个很简单的手机应用,主要是熟悉了如何对用户的动作产生反馈,一个简单的答题应用,与用户产生一些基本的交互。