Android 学习之《Android编程权威指南》第二版 代码+笔记整理(二)

(代码)GeoQuiz应用升级开发

GeoQuiz应用初步开发

不展示编译器自动完成的代码,仅提供手动修改或者编写的代码。
升级内容:提供更多的地理知识测试题目,按NEXT按钮进入下一题。

一、组成:由一个activity、一个layout和一个Question类还有图片资源组成

二、界面:

GeoQuiz升级界面

三、开发:

1. Android项目:

应用名称为:GeoQuiz
活动名称为:QuizActivity
活动对应布局名称:activity_quiz
模型类:Question

2. 用户界面设计:

设计图:

GeoQuiz升级界面_布局

代码:(activity_quiz.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.example.thinkpad.geoquiz.QuizActivity">

    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp" />
    <!--删除了硬编码的地理知识问题字符串-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <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/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"/>
    <!--NEXT按钮,添加了"NEXT"字符串资源以及drawable图标资源-->
</LinearLayout>

3. Question.java 模型类编写

package com.example.thinkpad.geoquiz;

public class Question { //Question类封装两部分数据:问题文本和问题答案
    private int mTextResId;//用于保存地理知识问题字符串的资源ID,而资源ID总是int类型
    private boolean mAnswerTrue; //本应用中问题答案为true||false
    public Question(int textResId,boolean answerTrue){ //满参构造函数
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }
    //get&&set函数
    public int getTextResId() {
        return mTextResId;
    }

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

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }
}

4. 更新字符串资源(strings.xml):

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="true_button">TRUE</string>
    <string name="false_button">FALSE</string>
    <string name="next_button">NEXT</string>
    <!--NEXT按钮文字-->
    <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>

5. activity活动编写(QuizActivity.java):

package com.example.thinkpad.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton; //true选项按钮
    private Button mFalseButton; //false选项按钮
    private Button mNextButton; //next选项按钮
    private TextView mQuestionTextView; //textView文本显示
    private Question[] mQuestionBank = new Question[]{ 
            //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(); //获取资源ID
        mQuestionTextView.setText(question); //设置文本内容
    }

    private void checkAnswer(boolean userPressedTrue){ //检查问题答案函数
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue(); //获取对应问题的答案

        int messageResId = 0;
        //根据答案正确与否分配资源ID
        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); //加载布局
        //获取TextView对象
        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);   

        //获取trueButton按钮对象
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(true);
            }
        });

        mFalseButton = (Button) findViewById(R.id.false_button); 
        //获取falseButton按钮对象
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(false);
            }
        });

        //获取NextButton按钮对象
        mNextButton = (Button) findViewById(R.id.next_button); 
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length; //索引值增加1
                updateQuestion();
            }
        });
        updateQuestion(); //更新问题
    }
}

(笔记)第2章 Android与MVC设计模式

1. 设置java代码风格
设置前后缀
设置前缀的作用:(以本应用为例)
若需要Android Studio 为mTextResId生成获取方法时,它生成的是getTextResId()而不是getMTextResId()。
2. Android应用基于模型-控制器-视图(Model-View-Controller)MVC 的架构模式进行设计。
A. 模型对象:存储着应用的数据和业务逻辑。存在的唯一目的就是存储和管理应用数据。
B. 视图对象:凡是能够在屏幕上看到的对象,就是视图对象。
C. 控制对象:含有应用的逻辑单元,是视图与模型对象的联系纽带。响应视图对象出发的各类事件,管理着模型对象与视图间的数据流动。
3. 在XML中以@string/开头的定义是引用字符串资源,以@drawable/开头的定义是引用drawable资源。


来自一名刚刚开始学习Android的小菜鸟~

猜你喜欢

转载自blog.csdn.net/Nicholas1hzf/article/details/82688592