Android 简明教程
Android - Hello World Example
让我们使用 Android Framework 开始实际编程。在开始使用 Android SDK 编写您的第一个示例之前,您必须确保按照 Android - Environment Set-up 教程中所述正确设置 Android 开发环境。我还假设您对 Android Studio 有一些操作知识。
Let us start actual programming with Android Framework. Before you start writing your first example using Android SDK, you have to make sure that you have set-up your Android development environment properly as explained in Android - Environment Set-up tutorial. I also assume that you have a little bit working knowledge with Android studio.
因此,让我们继续编写一个简单的 Android 应用程序,它将打印“Hello World!”。
So let us proceed to write a simple Android Application which will print "Hello World!".
Create Android Application
第一步是使用 Android Studio 创建一个简单的 Android 应用程序。当您单击 Android Studio 图标时,它将显示如下所示的屏幕
The first step is to create a simple Android Application using Android studio. When you click on Android studio icon, it will show screen as shown below
您可以通过调用启动新 Android Studio 项目来开始应用程序开发。在一个新安装框架中应该询问应用程序名称、软件包信息和项目的位置。−
You can start your application development by calling start a new android studio project. in a new installation frame should ask Application name, package information and location of the project.−
在输入应用程序名称后,它将被称为选择您的应用程序运行的形式因素,此时需要指定 最低 SDK,在我们的教程中,我已经声明为 API23: Android 6.0(棉花糖)−
After entered application name, it going to be called select the form factors your application runs on, here need to specify Minimum SDK, in our tutorial, I have declared as API23: Android 6.0(Mashmallow) −
下一级安装应包含选择移动的活动,它指定了应用程序的默认布局。
The next level of installation should contain selecting the activity to mobile, it specifies the default layout for Applications.
在最后阶段,它将打开开发工具来编写应用程序代码。
At the final stage it going to be open development tool to write the application code.
Anatomy of Android Application
在运行应用程序之前,您应该了解 Android 项目中的几个目录和文件−
Before you run your app, you should be aware of a few directories and files in the Android project −
Sr.No. |
Folder, File & Description |
1 |
Java This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon. |
2 |
res/drawable-hdpi This is a directory for drawable objects that are designed for high-density screens. |
3 |
res/layout This is a directory for files that define your app’s user interface. |
4 |
res/values This is a directory for other various XML files that contain a collection of resources, such as strings and colours definitions. |
5 |
AndroidManifest.xml This is the manifest file which describes the fundamental characteristics of the app and defines each of its components. |
6 |
Build.gradle This is an auto generated file which contains compileSdkVersion, buildToolsVersion, applicationId, minSdkVersion, targetSdkVersion, versionCode and versionName |
以下部分将简要概述重要的应用程序文件。
Following section will give a brief overview of the important application files.
The Main Activity File
主活动代码是一个 Java 文件 MainActivity.java 。这是实际的应用程序文件,最终被转换为 Dalvik 可执行文件并运行您的应用程序。以下是应用程序向导为 Hello World 生成的默认代码! 应用程序 −
The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application −
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
此处,R.layout.activity_main 指的是位于 res/layout 文件夹中的 activity_main.xml 文件。当加载活动时,onCreate() 方法是所列出的许多方法之一。
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The onCreate() method is one of many methods that are figured when an activity is loaded.
The Manifest File
无论您作为应用程序的一部分开发什么组件,都必须在位于应用程序项目目录根目录的 manifest.xml 中声明其所有组件。此文件充当 Android 操作系统和应用程序之间的接口,因此,如果您未在此文件中声明组件,则操作系统将不考虑该组件。例如,默认清单文件看起来像以下文件 −
Whatever component you develop as a part of your application, you must declare all its components in a manifest.xml which resides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file −
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint7.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
此处 <application>…</application> 标记所包含的组件与应用程序相关。属性 android:icon 将指向 res/drawable-hdpi 中的应用程序图标。该应用程序使用 drawable 文件夹中名为 ic_launcher.png 的图像
Here <application>…</application> tags enclosed the components related to the application. Attribute android:icon will point to the application icon available under res/drawable-hdpi. The application uses the image named ic_launcher.png located in the drawable folders
<activity> 标记用于指定某个活动,而 android:name 属性指定某个 Activity 子类的完全限定类名,而 android:label 属性指定一个字符串用作活动标签。你可以使用 <activity> 标记指定多个活动。
The <activity> tag is used to specify an activity and android:name attribute specifies the fully qualified class name of the Activity subclass and the android:label attributes specifies a string to use as the label for the activity. You can specify multiple activities using <activity> tags.
意图筛选器的 action 被命名为 android.intent.action.MAIN,它表示该活动作为应用程序的入口点。意图筛选器的 category 被命名为 android.intent.category.LAUNCHER,它表示该应用程序可以通过设备启动器图标启动。
The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry point for the application. The category for the intent-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device’s launcher icon.
@string 引用了下面说明的 strings.xml 文件。因此,@string/app_name 引用了 strings.xml 文件中定义的 app_name 字符串,即 “HelloWorld”。类似地,其他字符串也会填充到应用程序中。
The @string refers to the strings.xml file explained below. Hence, @string/app_name refers to the app_name string defined in the strings.xml file, which is "HelloWorld". Similar way, other strings get populated in the application.
下面是你将在清单文件中用来指定不同 Android 应用程序组件的标记列表:
Following is the list of tags which you will use in your manifest file to specify different Android application components −
-
<activity>elements for activities
-
<service> elements for services
-
<receiver> elements for broadcast receivers
-
<provider> elements for content providers
The Strings File
strings.xml 文件位于 res/values 文件夹中,它包含应用程序所使用的所有文本。例如,按钮、标签、默认文本和类似类型的字符串进入此文件。此文件负责其文本内容。例如,默认的字符串文件如下所示:
The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content. For example, a default strings file will look like as following file −
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
The Layout File
activity_main.xml 是一个布局文件,在应用程序构建界面时由 res/layout 目录引用。你将频繁修改此文件来更改应用程序布局。对于你的 “Hello World!”应用程序,此文件将具有与默认布局相关的以下内容:
The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface. You will modify this file very frequently to change the layout of your application. For your "Hello World!" application, this file will have following content related to default layout −
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
这是一个简单 RelativeLayout 的示例,我们将在单独的一章中研究它。TextView 是一种用于构建 GUI 的 Android 控件,它具有各种属性,如 android:layout_width、android:layout_height 等,这些属性用于设置其宽度和高度等。@string 引用了 res/values 文件夹中的 strings.xml 文件。因此,@string/hello_world 引用了 strings.xml 文件中定义的 hello 字符串,即 “Hello World!”。
This is an example of simple RelativeLayout which we will study in a separate chapter. The TextView is an Android control used to build the GUI and it have various attributes like android:layout_width, android:layout_height etc which are being used to set its width and height etc.. The @string refers to the strings.xml file located in the res/values folder. Hence, @string/hello_world refers to the hello string defined in the strings.xml file, which is "Hello World!".
Running the Application
让我们尝试运行我们刚刚创建的 Hello World! 应用程序。我假设你在进行环境设置时创建了 AVD 。要在 Android Studio 中运行应用程序,请打开一个项目活动文件,然后单击工具栏中的运行图标。Android Studio 在你的 AVD 上安装应用程序并启动它,如果你的设置和应用程序一切正常,它将显示以下模拟器窗口:
Let’s try to run our Hello World! application we just created. I assume you had created your AVD while doing environment set-up. To run the app from Android studio, open one of your project’s activity files and click Run icon from the tool bar. Android studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window −
恭喜你!你已经开发了你的第一个安卓应用程序,现在请按照教程的其余步骤,一步一步成为一名优秀的 Android 开发人员。祝你一切顺利。
Congratulations!!! you have developed your first Android Application and now just keep following rest of the tutorial step by step to become a great Android Developer. All the very best.