Android Studio教程– Hello World App

Welcome to the Android Studio Tutorial. This is the first article in the android tutorial series and today I’ll show you how to setup your android development environment and create a Android Studio Hello World app.

欢迎使用Android Studio教程。 这是android教程系列的第一篇文章,今天我将向您展示如何设置android开发环境和创建Android Studio Hello World应用。

Android Studio教程 (Android Studio Tutorial)

Since Google has dumped Eclipse and further development will be done in it’s own IDE, we will be using Android Studio as our development environment IDE. We will learn how to install android studio IDE and create a simple hello world application step by step from scratch.

由于Google已抛弃Eclipse,并且将在其自己的IDE中进行进一步的开发,因此我们将使用Android Studio作为我们的开发环境IDE。 我们将逐步学习如何安装android studio IDE并创建一个简单的hello world应用程序。

Android Studio安装程序 (Android Studio Setup)

Before you get your hands on Android Studio, download and install it from the link. Note that you need to have JDK 6 or higher installed. If you’re on Windows, launch the .exe file and follow the steps of the setup wizard. If you’re running Mac OS X, mount the disk image by double-clicking it and drag Android Studio to your Applications folder.

在开始使用Android Studio之前, 请先从链接下载并安装它 。 请注意,您需要安装JDK 6或更高版本。 如果您使用的是Windows,请启动.exe文件,然后按照安装向导的步骤进行操作。 如果您运行的是Mac OS X,请双击磁盘映像并将Android Studio拖到“应用程序”文件夹中。

Android Studio Hello World (Android Studio Hello World)

You’re now ready to create your first Android application using Android Studio. When you launch Android Studio for the first time, you should be presented with a welcome screen, offering you a number of choices to get you started.

现在,您可以使用Android Studio创建第一个Android应用程序了。 首次启动Android Studio时,系统会显示一个欢迎屏幕,为您提供许多选择以帮助您入门。

We’ll go with the New Project option here. However, you can choose Import Project if you’d like to import a project from, for example, Eclipse, into Android Studio. Android Studio will convert the Eclipse project to an Android Studio project, adding the necessary configuration files for you.

我们将在此处使用“ 新建项目”选项。 但是,如果要将项目从Eclipse导入到Android Studio中,则可以选择Import Project 。 Android Studio会将Eclipse项目转换为Android Studio项目,并为您添加必要的配置文件。

If you select Open Project from the list of options, you can open projects created either from the list of recent projects or by browsing the filesystem.

如果从选项列表中选择“ 打开项目” ,则可以打开从最近项目列表或通过浏览文件系统创建的项目。

Android Studio教程–新项目 (Android Studio Tutorial – New Project)

Let’s create a new project using the wizard shown in the Quick Start section of the welcome screen. Enter some name for the application, let’s say “Hello World” and make sure your package name is unique.

让我们使用欢迎屏幕的“快速入门”部分中显示的向导创建一个新项目。 输入应用程序的名称,例如“ Hello World”,并确保您的软件包名称是唯一的。

If you own some domain say yourdomain.com, then your package could be com.yourdomain.helloworld. While uniqueness isn’t of concern here, it will be important for an application that needs to be published to play store. Google Play Store distinguishes apps by the package name only.

如果您拥有某个域名,例如yourdomain.com,则您的软件包可以是com.yourdomain.helloworld 。 尽管在这里不必担心唯一性,但对于需要发布才能在商店中使用的应用程序而言,这将很重要。 Google Play商店仅通过程序包名称来区分应用。

Click “Next” button again and you’ll be presented with a list of platforms where you want your application to run along with separate SDKs list since different platforms require different SDKs list.

再次单击“下一步”按钮,将为您提供希望您的应用程序在其中运行的平台列表,因为不同的平台需要不同的SDK列表。

On the next screen you’ll be presented with a list of activity types. The default Blank Activity is good enough for this project.

在下一个屏幕上,您将看到一个活动类型列表。 默认的空白活动对于该项目已经足够了。

Click Next. This screen is an Activity configuration screen. Since you asked ADT to create an activity for you, this screen helps you in configuring relevant options.

点击下一步。 该屏幕是活动配置屏幕。 由于您要求ADT为您创建活动,因此此屏幕可帮助您配置相关选项。

  1. Activity Name: Let’s stay with the default name MainActivity.

    活动名称 :让我们保留默认名称MainActivity。
  2. Layout Name: ADT that creates activity for you, will also create a layout for you to bind it with the activity.

    布局名称 :为您创建活动的ADT,还将为您创建一个将其绑定到活动的布局。
  3. Title: It specifies the title of the activity.

    标题 :指定活动的标题。
  4. Menu name: This is used to provide menu options. Keep default name only.

    菜单名称 :用于提供菜单选项。 仅保留默认名称。

Click finish and wait for a few seconds for Gradle to do its magic.

单击完成,等待几秒钟,以便Gradle发挥其魔力。

Gradle is an advanced build toolkit that manages dependencies and allows you to define custom build logic. Android Studio uses a Gradle wrapper to fully integrate the Android plugin for Gradle.

Gradle是一个高级构建工具包,它管理依赖关系,并允许您定义自定义构建逻辑。 Android Studio使用Gradle包装器完全集成了Gradle的Android插件。

The Android plugin for Gradle also runs independent of Android Studio. This means that you can build your Android apps from within Android Studio or from the command line on your machine or on machines where Android Studio is not installed.

Gradle的Android插件也独立于Android Studio运行。 这意味着您可以从Android Studio内或在计算机上或未安装Android Studio的计算机上从命令行构建Android应用程序。

Once you click Finish ADT will create your new project and will open Java code and Layout code for you.

单击完成后,ADT将创建您的新项目,并将为您打开Java代码和布局代码。

By now you might be wondering how do you change your default white theme to the dark theme shown in the snippets. Go to menu Android Studio > Preferences, choose IDE Settings > Appearance.  Change the ‘Theme’ from ‘Default’ to ‘Dracula’.

现在,您可能想知道如何将默认的白色主题更改为摘要中显示的黑暗主题。 进入菜单Android Studio>首选项,选择IDE设置>外观。 将“主题”从“默认”更改为“吸血鬼”。

Android SDK管理器 (Android SDK Manager)

Since this is the first time this IDE is being used, we need to download few more essential pieces using Android SDK Manager. There is a green Android icon in the toolbar on top. Click on it to open the SDK Manager. Choose the components of the latest API that aren’t yet installed. Similarly scroll down and choose all the “Extra” components that aren’t yet installed and then click the “Install … packages”.

由于这是第一次使用此IDE,因此我们需要使用Android SDK Manager下载更多基本内容。 顶部的工具栏中有一个绿色的Android图标。 单击它以打开SDK Manager。 选择尚未安装的最新API的组件。 同样,向下滚动并选择所有尚未安装的“其他”组件,然后单击“安装...软件包”。

This will throw a dialog with license information. Accept using “Accept license” radio button on bottom left. And then choose to Install.

这将引发一个对话框,其中包含许可证信息。 使用左下方的“接受许可证”单选按钮接受。 然后选择安装。

Download may take a long time to finish. Don’t close that window. But return to the IDE.

下载可能需要很长时间才能完成。 不要关闭那个窗口。 但是回到IDE。

Now before we jump into any programming, let’s talk about how to get this app running. It’s time to say “Hello world!”

现在,在进入任何编程之前,让我们讨论一下如何运行此应用程序。 现在该说“你好,世界!”

Android Studio教程–在模拟器或设备上运行应用 (Android Studio Tutorial – Running app on Emulator or Device)

Create a new AVD instance using the AVD manager and provide the details asked.

使用AVD管理器创建新的AVD实例,并提供要求的详细信息。

Android Hello World应用 (Android Hello World App)

Once finished with the creation of virtual device, run the app on the emulator. Congratulations on your first android hello world app. But have you noticed how slow the default emulator is?

完成虚拟设备的创建后,在模拟器上运行该应用程序。 恭喜您第一个android hello world应用程序。 但是您是否注意到默认仿真器有多慢?

Hence it is recommended to use Genymotion plugin or debugging through phone for testing purposes. You can find the steps to integrate Genymotion with Android Studio here. Sign up and create your own virtual devices for a speedy development.

因此,建议使用Genymotion插件或通过电话进行调试以进行测试。 您可以在此处找到将Genymotion与Android Studio集成的步骤。 注册并创建自己的虚拟设备以加快开发速度。

You should keep your phone connected to your PC through USB. While deploying the app, your phone would be shown as an option to which you can deploy. To do this change Edit Configuration  > Target Devices >Show Chooser Dialog.
If you are planning to deploy the Android app to your phone, it is essential to turn on USB Debugging in your Android phone.

您应该将手机通过USB连接到PC。 部署应用程序时,您的手机将显示为可以部署的选项。 为此,请更改“ 编辑配置”>“目标设备”>“显示选择器对话框”
如果您打算将Android应用程序部署到手机,则必须在Android手机中打开USB调试。

  • On most devices running Android, you can find the option under Settings > Applications > Development.

    在大多数运行Android的设备上,您可以在设置>应用程序>开发下找到该选项。
  • On Android 4.0 and newer, it’s in Settings > Developer options.

    在Android 4.0及更高版本上,它位于设置>开发者选项中

Click run. The following window should pop up to choose the device.

点击运行。 应弹出以下窗口选择设备。

Android Hello World App项目结构 (Android Hello World App Project Structure)

It seem almost unbelievable that the simplest Android app you can create involves so many files. Most of the files that have been created are auto-generated and most of the time you don’t need to know anything about them let alone open or edit them.

您创建的最简单的Android应用程序涉及这么多文件,这似乎令人难以置信。 已创建的大多数文件都是自动生成的,并且大多数时候您不需要了解任何有关文件的信息,更不用说打开或编辑它们了。

So let’s focus on the files that matter to us.

因此,让我们专注于对我们重要的文件。

For our simple program there are only two important files one that determines the Activity’s behaviour MainActivity.java and one that determines most of the visual appearance activity_main.xml.

对于我们的简单程序,只有两个重要的文件,一个确定Activity的行为MainActivity.java ,另一个确定大多数视觉外观activity_main.xml

You can set which activity is the one that the system starts but by default it is the single activity that you created and named when you set up the project.

您可以设置哪个活动是系统启动的活动,但默认情况下,它是设置项目时创建并命名的单个活动。

In this case we named the activity MainActivity and its layout file activity_main – but you could change these defaults.You can see the location of these two important files in the Project window:

在这种情况下,我们将活动命名为MainActivity及其布局文件activity_main ,但是您可以更改这些默认值。您可以在Project窗口中看到这两个重要文件的位置:

MainActivity class in MainActivity.java file extends Activity. It overrides onCreate() method with a custom call. The custom call starts with a call to the parent class’ (Activity’s) onCreate(). Then it sets the layout to show on the screen using

MainActivity.java文件中的MainActivity类扩展了Activity。 它使用自定义调用覆盖onCreate()方法。 自定义调用始于对父类(活动)的onCreate()的调用。 然后使用以下命令设置要在屏幕上显示的布局

setContentView(R.layout.activity_main)

setContentView(R.layout.activity_main)

This line is the most important of all and really the only one that actually does anything. It gets the resource object that represents the layout as defined by the XML file and makes it the current ContentView i.e. it is what’s displayed on the screen.

这条线是最重要的,实际上是唯一实际执行任何操作的线。 它获取表示XML文件定义的布局的资源对象,并使它成为当前的ContentView,即它是屏幕上显示的内容。

The layout is referenced using ‘R’, a resources class, that is generated by the build system. R has a reference field for every Android resource in use from this project’s resource directory.

使用由构建系统生成的资源类“ R”来引用布局。 R对于该项目的资源目录中正在使用的每个Android资源都有一个参考字段。

One last file we must look into before we complete this article is AndroidManifest.xml.

在完成本文之前,我们必须查看的最后一个文件是AndroidManifest.xml

It is from this file that the Android system invokes the MainActivity when the application is launched. Remember, there can be several activities in the same application. The ones that can be launched by the system are given a special category “android.intent.category.LAUNCHER“.

启动该应用程序时,Android系统就是从该文件调用MainActivity的。 请记住,同一应用程序中可以有多个活动。 可以由系统启动的驱动程序被赋予特殊类别“ android.intent.category.LAUNCHER ”。

Others are custom or “android.intent.category.DEFAULT“. Such activities are typically invoked by one of the launched activities generally through intents which we will cover in later tutorials. That’s it for android studio tutorial, let me know through comments if you have any queries.

其他则是自定义或“ android.intent.category.DEFAULT ”。 通常,这些活动由已启动的活动之一通过意图(在我们稍后的教程中将进行介绍)中进行调用。 android studio教程就是这样,如果您有任何疑问,请通过评论让我知道。

翻译自: https://www.journaldev.com/8988/android-studio-tutorial-hello-world-app

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值