008 Android programming 3rd 学习写android程序以及MVC模型

这里主要记录android programming the big nerd ranch guide的学习笔记。

是英文版的但是决定后面的全部用中文解释,不理解的也需要理解,可能几章会合成一章,有兴趣的同学也可以看看书。

steep 陡峭的,险峻的。

预备知识

To use this book, you need to be familiar with Java, including classes and objects, interfaces, listeners, packages, inner classes, anonymous inner classes, and generic classes.

需要理解类和对象,包,监听,内部类,泛型类等Java基础。

讲了其它一些需要注意的知识,比如怎么寻求帮助之类的话题,然后就直接进入我们的主题,写代码。


第一章 第一个程序

虽然已经写了好多个第一个程序了,但是还是决定再写一遍,做到前后连贯,也正好熟悉作者的写作模式。

Your GeoQuiz application will consist of an activity and a layout:
第一个GeoQuiz程序包括一个activity和一个layout文件
An activity is an instance of Activity, a class in the Android SDK. An activity is responsible for managing user interaction with a screen of information.
GeoQuiz包括一个叫做QuizActivity的程序管理用户接口,UI显示。
A layout defines a set of UI objects and their positions on the screen. A layout is made up of definitions written in XML. Each definition is used to create an object that appears on screen, like a button or some text.
GeoQuiz程序包括一个叫做activity_quiz.xml的文件。

图中的文字很重要,就是Activity管理layout定义的东西。


开始写程序。

Widgets是什么。
Widgets are the building blocks you use to compose a UI. A widget can show text or graphics, interact with the user, or arrange other widgets on the screen.

介绍ViewGroup

包含四个 LinearLayout, FrameLayout, TableLayout, and RelativeLayout.


显示xml的方法

public void setContentView(int layoutResID)
This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes.

如何使用按钮

This is a two-step process:

get references to the inflated View objects
set listeners on those objects to
respond to user actions

创建一个通知消息

To create a toast, you call the following method from the Toast class:
public static Toast makeText(Context context, int resId, int duration)


编写的程序:

package com.audio.android.geoquiz;

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

public class QuizActivity extends AppCompatActivity {
    private Button mTrueButton;
    private Button mFalseButton;

    //Toast correctToast = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);

    @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 view) {
                Toast correctToast = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);
                correctToast.setGravity(Gravity.TOP, 0, 0);
                correctToast.show();
            }
        });

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


    }
}

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:padding = "20dp"
        android:text = "@string/question_text" />

    <LinearLayout
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:padding = "20dp"
        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>

</LinearLayout>



按TRUE键的时候toast会打印在上面,按FALSE键的时候toast会打印在下面,就是课后的习题修改。



第二章 MVC模式

下面这个表格是需要的功能的展示

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

添加资源的时候有个神奇的功能,就是不是不是Android目录下添加,而是切换到project下的res目录下拷贝四个已经命名好的文件。

drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi,

然后再切换到android目录时可以看到有两个文件命令的目录arrow_left.png arrow_right.png,就是我们想要添加的文件。

xml中引用资源文件的方式是
A reference to a string resource begins with @string/. A reference to a drawable resource begins with @drawable/.

3个挑战,一个是textview, 一个是会到前一个问题按钮,一个是将button改变成imagebutton.

第一个已经完成,比较简单。

全部完成,粘贴代码。

package com.audio.android.geoquiz;

/**
 * Created by wang on 17-6-23.
 */

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

<?xml version="1.0" encoding="utf-8"?>
<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">

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

    <LinearLayout
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:padding = "20dp"
        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>

    <LinearLayout
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:orientation = "horizontal" >
        <Button
            android:id="@+id/prev_button"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            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:text = "@string/next_button"
            android:drawableRight = "@drawable/arrow_right"
            android:drawablePadding = "4dp"/>

        </LinearLayout>

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

        <ImageButton
            android:id="@+id/prev_img_button"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:src = "@drawable/arrow_left"
            android:contentDescription="@string/previous_question"/>

        <ImageButton
            android:id="@+id/next_img_button"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:src = "@drawable/arrow_right"
            android:contentDescription="@string/next_question"/>

    </LinearLayout>

</LinearLayout>

package com.audio.android.geoquiz;

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

public class QuizActivity extends AppCompatActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mPrevButton;
    private Button mNextButton;
    private TextView mQuestionTextView;

    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_africa, false),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true),
    };

    private int mCurrentIndex = 0;

    //Toast correctToast = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);

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

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

        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });

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

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

        mPrevButton = (Button) findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = mCurrentIndex - 1 < 0 ? (mCurrentIndex - 1 + mQuestionBank.length) : mCurrentIndex - 1;
                mCurrentIndex = mCurrentIndex % mQuestionBank.length;
                updateQuestion();
            }
        });

        mNextButton = (Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });
    }
    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();
    }
}

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_australia">Canberra is the capital of Australia.</string>
    <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
    <string name="question_mideast">The Suez Canal conencts 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 Ameriacas.</string>
    <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
    <string name="prev_button">Prev</string>
    <string name="next_button">Next</string>
    <string name="previous_question">previous question</string>
    <string name="next_question">next question</string>

</resources>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值