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

(代码)GeoQuiz应用初步开发

不展示编译器自动完成的代码,仅提供手动修改或者编写的代码。

一、组成:初步由一个activity和一个layout组成

二、界面:

GeoQuiz界面

三、开发:

1. 新建Android项目:

应用名称为:GeoQuiz
活动名称为:QuizActivity
活动对应布局名称:activity_quiz

2. 用户界面设计:

设计图:
用户界面设计图
代码:(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">
    <!--android:orientation="vertical"垂直布局-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp" 
        android:text="@string/question_text" 
        /> 
    <!--android:padding="24dp"内边距-->
    <!--android:text="@string/question_text"对字符串资源的引用-->

    <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>
    <!--android:orientation="horizontal"水平布局-->
</LinearLayout>

3. 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.Toast;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton; //添加两个按钮成员变量,m开头表示menmber成员变量
    private Button mFalseButton;

    @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.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).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.correct_toast,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

4. 创建字符串资源(strings.xml):

<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="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
</resources>

(笔记)第1章 Android开发初体验

1. 组件是用户界面的构造模块,Android SDK 内置了多种组件,每一个组件都是View类或其子类的一个具体实例。
2. 组件属性:
A. android:orientation 决定子组件水平放置(horizontal)或者垂直放置(vertical)
B. 本案例中使用的android:text的属性值不是字符串值,而是对字符串资源(string resources)的引用。字符串资源包含在一个独立的名为strings的XML文件中,虽然可以硬编码设置组件的文本属性,但这通常不是个好主意,更好的做法是:将文字内容放置在独立的字符串资源XML中,然后引用它们。strings.xml在app/res/values中。
3. 布局是一种资源,资源是应用非代码形式的内容。
4. findViewById(R.id.ID名)引用组件
5. 调用来自Toast类的方法,可创建一个toast
public static Toast makeText(Context context,int resId,int duration)
Context参数通常是Activity的一个实例(Activity是Context的子类),resId参数是toast要显示的字符串消息的资源ID,第三个参数duration则是两个Toast常量中的一个(Toast.LENGTH_SHORT && Toast.LENGTH_LONG)用来指定toast消息显示的持续时间。
6. dp 即density-independent pixel 与密度无关像素 单位


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

猜你喜欢

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