将上一张程序改为有多个问答题目

创建Question

geoquiz包中添加Question类,右键New - Java Class

package com.avalon.ash.geoquiz;

public class Question{
  private int mTextResId;
  private boolean mAnswerTrue;

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

Question类中包含问题内容和答案。但是问题不是直接保存在类中,而是只保存问题文本的资源id

自动生成gettersetter

File - Settings打开配置对话框。Editor - Code Style - Java。在右侧窗口中选择Code Generation标签选项
选中Inner classes,在Field / Name prefix中填写mStatic field / Name prefix中填写s。点击OK结束配置

在打开的Question.java文件中,在构造函数后面鼠标右键, 点击Generate,选择Getter and Setter, 然后选择内部变量mTextResIdmAnswerTrue,点击生成
code generation

Android and Model-View-Controller

  • model layer 包含 model 对象,通常对应一个物理类,用于存储和管理物理数据
  • view layer 用于定义用户界面并响应用户指令
  • controller layer用于连接 model 和 view。管理业务逻辑。在Android中它通常是Activity, Fragment, Service的子类

update the view layer

activity_quiz.xml中添加next_button,修改显示问题的TextView

<TextView
  android:id="@+id/question_text_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="24dp"/>

<Button
  android:id="@+id/next_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/next_button"/>

strings.xml中添加相关字符串

<string name="next_button">Next</string>
<string name="question_oceans">Question 1</string>
<string name="question_mideast">Question 2</string>
<string name="question_african">Question 3</string>

update the controller layer

QuizActivity.java中添加变量,并绑定显示问题的TextViewNext按钮

public class QuizActivity extends AppCompatActivity{
  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_african, false),
  };

  private int mCurrentIndex = 0;

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

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

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

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

    // ...
  }
}

增加答案校验功能

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();
}

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

  // ...

  mTrueButton = (Button)findViewById(R.id.true_button);
  mTrueButton.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v){
          checkAnswer(true);
      }
  });
  mFalseButton = (Button)findViewById(R.id.false_button);
  mFalseButton.setOnClickListener(new View.OnClickListener(){
      @Override
      public  void onClick(View v){
          checkAnswer(false);
      }
  });
}

在硬件设备上运行

首先配置手机,打开开发者选项中的USB调试功能 (Settings - Developer options - USB debugging)

使用数据线连接手机与电脑(Mac上自动连接,而Windows上需要安装驱动
如果驱动为自动安装则需要手动安装,在计算机 - 设备管理中可以看到有个其它设备-ADB Interface
abd driver uninstalled

右键 - 更新驱动程序,在弹出的对话框中手动选择驱动所在目录,然后点击安装即可
安装成功后可以看到Android Device - Android Composite ADB Interface
abd driver installed

如果连接成功可以在Android StudioTerminal中运行adb devices进行检测
运行目录为$(sdk)/platform-tools,该命令可以列出所有已连接设备
adb devices

运行程序,选择你所连接的硬件设备而非模拟器,则可以你所连接的设备上测试你的程序。
之后就可以Android monitor中看到你设备的状态
android monitor