65.9K
CodeProject 正在变化。 阅读更多。
Home

创建您的第一个Android应用程序

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7投票s)

2014 年 7 月 30 日

CPOL

5分钟阅读

viewsIcon

25226

downloadIcon

1

通过分步指南帮助初学者创建他们的第一个Android应用程序

引言

本文将详细解释 Android 项目的所有组件以及如何使用或自定义它们。文章为初学者 Android 开发人员提供了分步指南,教您如何创建 Android 项目并在模拟器或 Android 设备上运行它,以及如何调试您的代码。这是继前两篇介绍 Android 基础知识的文章之后的第三篇文章。[文章 #3]

有关 Android 内核的介绍,请参阅这篇我认为最好的文章 https://codeproject.org.cn/Articles/802449/Article-Introduction-to-Android

现在回到我们的文章,它由以下几个部分组成:

  1. 使用 Eclipse IDE 创建 Android 项目
  2. 主要组件的基本解释
  3. 在模拟器上进行测试
  4. 在实体设备上进行测试
  5. 如何调试你的代码

1. 创建 Android 项目

     1.1 打开 Eclipse IDE

      Android Development Tool

     1.2 前往 文件->新建->Android 应用程序项目

 

     

     1.3 新建 Android 应用程序屏幕

     

           (1) 设置您的应用程序名称,该名称将显示在 Android 设备上

           (2) 设置您的项目名称,该名称将显示在 Eclipse 项目资源管理器中

           (3) 设置您的包名,该名称将显示在 Eclipse 包资源管理器中,您可以在同一项目中创建多个包

           (4) 设置您的最低 SDK 版本,您的应用程序将支持该版本

           (5-6) 设置您的目标 SDK 和编译 SDK 版本

           (7) 从提供的其中一个主题中选择您的主题,目前最新的主题称为 Holo

 

     1.4 点击“下一步”

     1.5 配置启动器图标。您可以从您的库中选择任何图片

     1.6 创建一个空白 Activity,然后点击“下一步”,再点击“完成”。现在我们可以开始更详细的介绍了

2. 各种组件的基本解释

     2.1 src/包名

     这里是您可以添加 .java 类文件和所有逻辑的地方。

     正如我们在下面的代码中看到的,我们有一个名为 MainActivity 的 Java 类,它继承自 Activity,并且该类位于 src/com.example.helloworld 包中。

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

     2.2 res

    您可以使用此库来访问所有资源,例如图片、声音文件、菜单和布局

         2.2.1 Drawables (图片)

         您有用于小/中/大/超大/极超大设备的各种 drawable 文件夹,您应该添加各种尺寸的图片,以确保您的应用程序设计适合所有设备屏幕尺寸。

         2.2.2 Layout (布局)

        此文件夹包含所有屏幕的布局。在创建我们的项目并选择 MainActivity.java 后,会自动创建一个名为 activity_main.xml 的 .xml 布局文件。每创建一个 Activity 类型的类,都必须关联一个 xml 布局文件,并且不同的类可以关联同一个布局文件。

这是布局 .xml 文件 (activity_main.xml) 的示例,它代表了 MainActivity 的布局/外观。

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

        2.2.3 Menu (菜单)

        这是一个 .xml 文件,代表用户点击菜单时出现的菜单项,如下图所示 

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

        2.2.4 Values (值)

         您可以添加不同的 xml 文件,如 color.xml、dimens.xml 和 strings.xml,以添加一些默认值供以后在我们的应用程序中使用。

//styles.xml
/// AppBaseTheme is represening our selected default Theme "android:Theme.Light" and we extend it by another Theme "AppTheme" to customize the default theme.

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>
//dimens.xml
<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>


//strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HelloWorld</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

     2.3 AndroidManifest.xml

     此文件包含您的配置,如当前版本、最低 SDK 和目标 SDK,以及对所有类的引用(必须将所有 Activity 类包含在此文件中)并设置哪个类作为您的默认类启动您的应用程序。此外,您还可以通过在此文件中添加权限来添加任何必需的权限,例如访问互联网。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3. 使用模拟器进行测试

     3.1 右键单击项目->运行方式->Android 应用程序,然后等待模拟器打开并启动您的应用程序,如下面的图片所示。

4. 使用实体设备进行测试

现在,我们将应用程序运行在实体设备上,这比模拟器更受欢迎,因为它更快、更好,而且模拟器不允许测试所有功能,例如录音或摄像头。

     4.1 准备好您的 USB 数据线和手机,连接到您的 PC

     4.2 点击“打开透视图”

     4.3 选择第二个选项“DBMS”,如果您有任何已连接的设备,它将显示在此处。

注意:您必须在设备的“设置/开发者选项”中启用“USB 调试”。

     4.4 返回到 Java 选项卡并运行您的应用程序,将会出现类似以下的窗口,选择您的设备然后点击“确定”,它就会运行。

 

5. 如何调试你的代码

     5.1 在您想要调试代码的地方添加一个断点

     5.2 选择“调试”而不是“运行”,并选择您的设备

     5.3 每当执行到选定的行时,它就会将您切换到调试模式,您就可以进行调试并监视您的变量。

摘要

希望这篇文章能帮助任何有兴趣开始开发 Android 应用程序的人,并找到他们最容易入门的方式。

随时提出任何问题。

© . All rights reserved.