ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

第一次开发安卓app——你需要知道的一切事项 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

第一次开发安卓app——你需要知道的一切事项

Writing your first Android app – everything you need to know

大家好 我是Gary Simtion 来自Android Authority
Hi, everybody, my name is Gary Simtion, from Android Authority.
开发安卓应用的原因有很多
Now there are many reasons to want to write an Android app.
或许当你使用安卓的时候 有更好的点子
Maybe you’ve got great idea when you want to approach type it on Android,
或许你了解电脑编程 想向手机方面发展
Maybe you konw about PC programming, you want to move over to mobile.
或者这只是你大学课程的一部分
Maybe it’s part of a college or university course.
无论什么原因 开发安卓应用 有趣又有益
Whatever the reason is, writing Android apps can be fun and rewarding.
今天我将教你如何开发你的首个安卓APP
And today I’m goona show you how to write your first Android app.
Android Authority[音效]
Android Authority
好的 在我们开始之前 我要提醒几点
Ok, before we begin, we need to mention a couple of things.
首先 你需要知道一些Java编程语言
First of all, you really need to know a little bit about the Java programming language
帮助你理解这个特定教程
to get you through this particular tutorial.
关于Java基础知识 我已做过一个视频
Now I’ve already done a video on Java basics.
你可以在Android Authority的Youtube的频道找到这个视频
You can find that video on the Android Authority Youtube channel.
也可以在Android Authority网站上找到相关文章
You can also find the accompanying article on the Android Autority Websites.
我建议你去浏览
I do recommend that you go and read that.
现在 有人在我做的那个教程评论区留言
Now, when I made that tutorial, some people left in their comments
他们认为我在基础上讲的太快了
that they thought I was going too fast through the material.
因此 在这个视频中 我要声明两点
So, for this video, I’m gonna say two things.
首先 我会多次重复特定要素
First of all, I’m gonna repeat certain elements a couple of time
当它们尤其重要时 我要确保你们能理解
just to make sure you’ve got them when they are particular important.
第二 如果看一遍你不太明白
And secondly, please do use the pause button and go back watch things again
请暂停并倒回重放
if you didn’t quite get it on the first time round.
好的 我们开始吧
OK. So let’s start.
好 开发一个app你需要下载安装
OK.So to write an app you are going to need a downloading installed
谷歌提供的Android Studio
Android Studio that’s provided by Google,
如果你找不到你可以在Android Authority网站的视频同步指南内
and if you can’t find it, you’ll find the link to it
找到指向它的链接
in the written companion to this video on the Android Authority Website.
包含在下载内的是安卓软件开发工具包
Now included in the download is the software development kit for Android
包含你开发一个APP需要的工具
that all the bits and pieces you need to develop an app
以及安卓虚拟机
as well as the Android emulator.
因此你能在计算机上初步测试你的APP
So that you can initially test your app on a PC,
而无需安装在安卓设备上
without needing to install it on a real device.
然而 在你安装Android Stuido之前 你将
However, before you install Android Stuido, you’re going to
需要安装Oracle的Java开发工具
need to install the Java Development Kit from Oracle.
查阅视频同步指南 你会找到下载JDK的链接
Go over to the written companion and you’ll find the links to tell you where to download the JDK.
确保你下载了正确的版本 32位的或64位的
Now you need to make sure you download the right version either 32-bit or 64-bit.
确认你运行着哪个Windows版本 并下载正确的JDK
Check which version of Windows you’re running and download the right one.
因为如果你下载错了 运行Android Studio将遇到麻烦
Because if you don’t, you’re gonna have trouble running Android Studio.
安装JDK 然后你就可以安装Android Studio了
Install that and then you can install Android Studio.
在安装过程中 你需要确认
During the install, you’ll need to configure
为安卓虚拟机预留多少存储空间
how much memory to reserve for the Android Emulator.
虚拟机以一种虚拟机器的形式运行安卓
Now the Emulator runs Android in a kind of virtual
就像一部搭载英特尔处理器的安卓手机
machine as an Android phone with a Intel processor.
这是在计算机上更快的模拟ARM处理器
This is faster emulating an ARM processer on your PC.
然而 运行此虚拟机需要占用一定内存
However, to do this the virtual machine needs to allocate some memory.
安装程序将提醒需要提供多少内存
The installation program will recommend how much memory to reserve,
最好接受默认设置
and it’s properly best to accept the default.
但是 注意 与Android Studio组合
However, be aware, that the combination of Android Studio,
Java和虚拟机同步运行 相当耗费内存
Java and the emulator all running together can be quite memory hungry.
你的电脑将变缓 如爬行般 除非你有大量内存
And your PC would slow to a crawl unless you have lots of RAM.
谷歌宣称你至少需要2G内存
Google says you need 2 gigabytes of RAM as a minimum
推荐4G内存
and that 4 gigabyes of RAM is recommanded.
然而我的电脑有8G内存 有时它仍然窘迫
However, my main PC has 8 gigabytes of RAM and sometims it still struggles.
当你初次运行Android Stuido 提示初始化
When you first run Android Stuido, it’ll perform some initialization
包括下载和安装最新版本的安卓SDK
including downloading and installing the latest Android SDK.
这将占用几分钟 你需要一点耐心
This can take several miniutes, so you will need to be a little bit patient.
当它最终启动 将为你呈现一幅漂亮的菜单
When it finally does all start up, you’ll be presented with a nice menu
它允许你开始一个新项目
that allow you to start a new project,
打开或导入一个项目 等等
open existing project and import a project and so on.
如你所愿 你点击开启一个新项目
And as you mignt expect, you to click on start a new Android Studio project.
然后你需要在“应用命名”区域 输入APP名称
You’ll then need to enter in a name for your app in the Application Name field.
我建议取名“我的第一个APP”
I will suggest something like My First App,
在“公司域名”处 你需要输入你的公司域名
and in the Company Domain field you need to enter the domain name of your company.
如果你是独立开发者或爱好者
If you’re an independant developer or hobbyist,
输入你自己的域名
enter your own domain name.
如果你仅仅为了体验安卓
If you’re just experimenting with Android,
从未打算在Google Play上发布你的App
you won’t be publishing your apps to Google Play anytime soon,
域名空缺不填
then just leave the domain as it is,
也许你可以改变用户名为你的名字
maybe you could just change the word user to your own name.
在下一页的对话框 确认“手机和平板电脑”勾选
On the next dialog, make sure the Phone and Tablet is selected and
SDK最低版本为API 15和Android 4.0.3
that the minimun SDK is set to API 15 Android 4.0.3.
确认没有勾选“穿戴设备”和“电视”
Make sure the Wear and TV are not ticked.
点击为手机表单“添加一个活动”
On the Add an Activity to Mobile dialog,
选用默认空白活动 然后再点“下一步”
use the default blank activity and then click Next again.
在“自定义活动”对话框 使用默认值并按“完成”
On the Customize the Activity dialog, just use the default values and click finish.
集成开发环境将启动
The Integrated Development Environment (IDE) will now start.
这会花费几分钟 尤其是我们初次创建一个项目
This can take several minutes, especially it is the first time we’ve created a project.
当IDE显现 如果你看到错误信息
When the IDE appears, if you see the error messages,
渲染问题 已知的渲染失败问题
rendering problems, rendering failed with a known bug,
只要点击显示在错误旁的重建链接
then just click the rebuild link as indicated next to the error.
默认IDE工作空间 不包括工具条 分为三个部分组成
The default workspace for the IDE is split into three parts excluding the toolbars.
左上方为项目树 它右侧为代码编辑器和设计器
On the upper left is a project tree, to its right is the code editor and designer,
在它们的下方 你看到的是来自编译器的各种信息 等等
and beneath them both is where you’ll see the various messages from the complier and so on.
此时此刻 你能够实际编译和运行自动集成代码
At this point you can actually compile and run the auto generated code.
尽管它不是十分有趣
However it’s not very interesting.
那么我们将要做的就是添加一点东西
So what we are going to do is we’re going to add a few things,
不算多但是足矣使你一尝安卓应用开发的快感
not very much but just enough to give you a taste of Android app development.
“项目树”
The project tree
项目树包含你建立安卓应用需要的不同文件和资源
The project tree holds the different files and resources that you need to build your Android app.
在项目树中的“App”节点下 你将看到几个其它节点
Under the App node in the project tree, you’ll see several other nodes
就像文件夹和你能展开的子文件夹
like folders, subfolders which you can expand.
顶层节点为“manifests” “java”和“res”
The top-level nodes are manifests, Java and res.
“res”代表资源
And “res” stands for resources.
在清单下 你会发现文件“AndroidManifest.xml”
Under manifests you’ll find the file AndroidManifest.xml
每个应用都必须有一个此类文件
Every application must have one of these files.
这是一个XML文件 包含有APP及其名字的信息
It’s an XML file with information about the app including its name.
在这个文件中添加的最常见的东西之一
One of the most common things that you’ll add to this file
就是你的APP的许可列表
is the list of permissions needed by your app.
对于这个简单的测试 在这里我们不需要任何改变
For this simple test, we won’t need to chang anything here.
在Java文件夹下 你会发现APP的Java代码
Under the Java folder, you will find the Java code for your app.
实际上 你将在子文件夹下发现它 一些代码
In fact, you’ll find it under a subfolder, some code,
像是com.example.user.myfirstapp
something like com.example.user.myfirstapp.
即你早前启动项目时输入的域名
Whatever is the domain name you entered earlier on when we started the project.
在那个文件夹下 你将找到一个文件“MainActivity.Java”
Under that folder you will find a file called MainAtivity.Java.
这是你的APP和我们的示例APP的入口
This is the entry point for your app and for our example app,
是我们唯一必须的Java文件
this is the only Java file we’re going to need,
但我们需要在这个文件中添加一些Java
but we are going to need to add some Java into this file.
再来一次 你得到Java文件夹下
So just again, so you got that the under the Java folder
你将发现一个带公司域名的子文件夹
you’re going to find a subfolder with a domain name of
APP名称与之关联
your company with the app name appended to it.
在它下面 你将发现“MainActivity.java”文档
And under that you are going to find MainActivity.java
那就是这个非常简单的APP的主要入口点
and that’s the main entry point for this very simple app.
在资源文件夹下的“res”文件夹
Under the resources folder, the res folder,
你能发现大量的其它子文件夹
you can find lots of other subfolders,
此处保留项目的不同资源
now this is where the different resources for the project are kept
指图形 字符串值 菜单 以及最重要地是
that means graphics, string values, menus and most importantly,
用户界面的定义存储位置
where the definition for the user interface is stored.
现在 我们对这个APP的感兴趣的两个是“layout”和“values”
Now the two that are of interest to us for this example app are layout and values.
在“layout”下有一个文件
Under layout, you’ll find a file
叫做“activity_main.xml” 这是一个描述用户界面的XML文件
called activity_main.xml, it is an XML file that describe the user interface.
编辑此文档有两个途径 第一种是直接编辑XML代码
There are two ways to edit this file, the first is to edit the XML code directly,
第二种是使用内嵌的用户界面设计器
and the second is to use the built-in UI designer.
在/res/layout下有一个文件“activity_main.xml”
So under /res/layout you’ll find a file called activity_main.xml
这是为安卓应用描述用户界面的文件
and this is the file that describes the user interface for your Android app.
一旦你精通安卓应用开发
Now once you become proficient an expert Android app development,
你可能发现自己经常直接编辑XML文件
you may found that yourself often editing the XML file directly.
但庆幸 Android Studio具有内嵌的UI设计器
But thankfully, Android Studio has a built-in UI designer,
当你在UI中使用设计器改变元素时
and as you change elements on the UI using the designer,
它会自动创建相应的XML文件
it automatically creates the corresponding XML file.
这就是我们示例APP中将要做的事情
And that’s what we’re going to do in our sample app in a few moments.
文件夹“values”包含几个不同的XML文件
The values folder contains several different XML files.
这个例子中最重要的是”strings.xml”
And the most important one for this example app is strings.xml.
而不是将硬代码字符串值到Java代码中
Rather than hard coding string valued into the Java code,
赋值置于“string.xml” 然后使用ID引导它们
values are placed into strings.xml and then they’re referenced using an ID.
这个系统的优点是如果一个字符串需要在多个地方进行修改
Now the advantage of this system is if a string needs to be changed in multiple places,
你所要做的就是在strings.xml中改变它一次
all you have to do is change it once in strings.xml,
它会在所有的代码中被改变
and it will be changed throughout all of the code,
在整个应用程序中 它还支持多种语言
it also makes supporting multiple languages in the app that whole much easier,
只要重复“strings.xml”
so just to repeat that strings.xml
字符串值存储在Java代码中
is where the string values stored and in the Java code
在XML文件中的布局
and in the XML file for the layout,
你只需使用ID的引用字符串
you just reference the string using the ID
几分钟后我们会讲到
and we’ll see about that in a few minutes.
总的来说 创建我们今天要做的示例APP
So just to sum up then, to create the sample app that we’re going to do today,
你需要修改“MainActivity.java” 添加一些Java代码
you need to modify MainActivity.java to add some Java code.
需要修改“activity_main.xml”
We’re going to need to modify activity_main.xml
因为我们将改变用户界面
because we’re going to change the UI,
需要修改“strings.xml”
and we’re going to need to modify strings.xml
因为我们想要添加一些硬代码的字符串 如
because we want to add some hard-coded strings like
“OK”或“Tap me”到文件里
OK or Tap me into that file.
编写APP
Writing the app
那么我们开始修改标签的文本 并改变其对齐方式
So let’s start by altering the text of the lable and changing its alignment.
首先 找到项目树中的文件“activity_main.xml”
First, find the activity_main.xml file in the project tree
双击 记住文档“activity_main.xml”是
and double click, remember that activity_main.xml is
包含用户界面定义的文件
the file which holds the user interface definition.
在代码窗口底部 有两个选项卡“design”和“text”
At the bottom of the code window, there are two tabs design and text,
确认你用的是“design”选项卡
make sure you’re using the design tab.
单击“Hello world!” 这是在手机的渲染上显示的
Now click on the text “Hello world!” that is shown on the rendering of the phone,
如果字符太小 用调焦按钮 加号放大
if it’s too small, use zoom buttons, the plus sign,
放大镜放大手机的渲染效果
the magnifying glass to enlarge the rendering of the phone.
在属性窗口 就在手机图像的右边 向下滚动
In the property windows, just to the right of the phone image scroll down
直到你找到“layout:centerInParent”
until you find “layout:centerInParent”,
单击它旁边的空白处 选择“horizontal”
click the space next to it and selecte “horizontal”.
“Hello world!”文字就显现在水平中央
The “Hello world!” text will now jump to the horizontal center.
现在修改文字 字符串“Hello world!”
Now to change the text, the string “Hello world!” is
被存储在我们之前提过的/res/values的“strings.xml”
held in that file we mentioned earlier strings.xml under /res/values
双击文件 你会看到“XML”文件的几行
If you double click on the file, you’ll see a few lines of XML
这定义了APP使用的一些字符串 找到
that defined a few String used by the app, find the
那行在尖角括号里声明字符串等于“Hello World!”代码
still line that says string equal to “Hello World!” in angle bracket
“Hello World!”然后关闭字符串选项卡并改变
then “Hello world!” and then the closing string tab and change the
“Hello World!”宣称指向“如果你想 请按我”
“Hello world!” exclamation point to “Tap me if you dare”.
现在添加按钮 返回“”activity_main.xml”的设计选项卡
Now to add the button, back on the design tab of activity_main.xml,
在手机渲染的左手边的托盘列表中找到并点击按钮
find and click on the button in the pallet list on the left hand side of the phone rendering.
现在点击文本“Tap me if you dare”下面某处
Now click somewhere beneath the text “Tap me if you dare”
在手机画面上 确保它在中间某处
on the phone image make sure it’s somewhere in the middle.
双击按钮 你就可以修改文字了
Now double click on the button, so that you can change the text,
快速直接的方法就是改变文字并将其硬编码在Java文件中
the quick and dirty way is just change the text and leave it hard-coded in the Java file.
然而 由于我们已经习惯了使用“strings.xml”
However, since we’ve already been used to using strings.xml,
它很不错 我们继续用它
it’s best that we carry on using it.
在文本框的末尾有一个有三个点的按钮点击它
At the end of the text field is a button with three dots, click on it.
在“资源”窗口 显现点击“新资源” 然后出现新字符串值
In the resources window that appears click on new resource and then new string value.
在资源命名框输入“Tap me” 并且
In the resource name enter “tap me” and
在资源值框输入“Tap Me”感叹号
in the resource value enter “Tap Me“ exclamation point.
点击OK,你将注意到按钮现在显示“Tap me”
Now click OK and you’ll notice the button now says “Tap Me”.
最后一步是添加一些Java代码 这些代码对被选中的按钮做出响应。
The final step is to add some Java code which reacts to the button being tapped.
安卓的一个UI的元素叫做“toast”
One of the UI elements of Android is called a toast,
它在一个小的弹出窗口中提供简单的反馈,然后消失
a toast provides simple feedback in a small pop-up that appears for a moments, and then disappears.
当你曾使用安卓设备时 你肯定见过这种状况
You’ve definitely seen it when you’ve being using Android.
例如 在Gmail中 在发送邮件之前使用导航
For example, in Gmail, navigating away from an email before you send it,
触发一条讯息被存为“toast”
triggers a message saved as a draft Toast.
“toast”会自动出现 然后在预定义超时后消失
Toast appear automatically and then disappear after a predefined timeout.
对我们的示例APP来说 我们每次点击按钮,都需要显示“toast”
For our sample app, we’ll need display a toast every time the button is tapped.
第一步 添加一些Java代码
The first step is to add some Java code,
找到“MainActivity.java”文件 在“onCreate”下方添加后续代码
find MainActivity.java and add the following code beneath onCreate.
“View v”中的字“view” 可能显示为红色的信息气泡 显示在它附近
The word view as in “View v” will likely be in red with a message bubble displayed near it,
这是Android Studio告诉你使用一个新的联系视图
this is Android Studio telling you have used a new contract view
在Java代码顶部的import部分中不导入它
without importing it in the import section of the top of the Java code.
这实际上很容易修复 点击“View” 和按“Alt+Enter”键
This is actually very easy to fix, click on the word “View” and press “Alt+Enter”,
Android Studio将为你修复
Android Studio will fix it for you.
如果“toast”是红色 那么重复做
If the word “toast” is in red then do exactly the same thing,
点击“View” 和按“Alt+Enter”键
click on the word “toast” and then press “Alt+Enter”.
现在回到activity_main.xml的设计器中
Now back in the designer for activity_main.xml,
单击按钮 向下滚动属性列表直到找到“onClick”
click on the button and scroll down through the property list until you find “onClick”.
点击它右侧的组合框 会看到一个函数列表
Click on the combo box to the right of it and it will see a list of functions,
单击按钮“tap” 那就是我们添加至Java代码的函数
click on on button tap, that’s the function we just added to the Java code.
因此 只要点击按钮 函数“onButtonTap”就会被调用
So now the “onButtonTap” function will be called whenever the button is tapped.
当它被访问时 会创建一个toast叫做“myToast”
When it is called, it creates a toast called “myToast”
将显示信息“Ouch!”
that will display the message “Ouch!”.
为了展示toast 我们调用“myToast.show()”
To show the toast, we just call “myToast.show()”.
就这些 依照编写APP
And that’s it, in terms of writing the app.
现在是时候在虚拟机内测试它了
Now it’s time to test it in the emulator.
测试APP
Testing the app
在工具菜单下 导向Android AVD管理器
Under the tools menu, navigate to Android AVD manager.
这个工具显示了当前配置的Android虚拟设备列表
This tool shows you the current list of configured Android Virtual Devices.
你有一个默认配置的设备 可以是Nexus 5
You will have one device configured by default, probably a Nexus 5.
单击“play”图标 按actions列下的三角
Click on the play icon, the triangle under the actions column
将启动虚拟机
and this will start the emulator.
现在取决于你的电脑性能和你有多少内存
Now depending on the performance of your PC and how much RAM you have
虚拟机需要几分钟才能启动
the emulator can take several minutes to start up.
一旦虚拟机启动并运行 切换到tools 点击运行app
Once the emulator is up and running, go to tools and click run app,
这将编译你的APP 并发送到虚拟机
this will compile your app and send it to the emulator.
在这个过程里Android Studio将会问你使用哪个虚拟机
During this process, Android Studio will ask you which emulator to use,
你将在列表中看到你的虚拟机 它应为默认选项
you will see your emulator in the list, it should be the default option,
所以只需点击”OK”
so just click OK.
最终 APP将显现在虚拟机上
The app will appear in the emulator eventually.
你现在可以点击“Tap me”键 并观察toast的出现
You can now click the “Tap Me” button and watch for the toast to appear
在虚拟机设备的下方
towards the buttom of emulated device.
恭喜
Congratulations!
在一个真正的安卓设备上运行你的APP是可行的
It’s actually possible to run your app on a real Android device,
最简单的方法就是在你的手机上进行USB调试
and the easiest way to do that is to enable USB debugging on your phone
把它连接到你的电脑上
and connect it to your PC.
在“设置-开发人员”下 可以找到“USB调试”选项
The USB debugging option can be found under Settings-Developers.
如果你的“设置”菜单没有“开发人员选项”
If you don’t have a Developers option in your Settings menu,
就去“设置” “关于”再点击“Build Number”几次
then go to Settings-About and tap the Build Number seven times.
在设备连接的情况下点击工具菜单下的“run out”
With the device connected, click on run out under Tools menu.
但这次不要发布APP到虚拟器,发布到真的安卓设备上
But this time don’t send the app to a virtual device, send it to a real device.
如果你的设备未被授权 也就意味着你无法运行USB调试
If your device isn’t listed, either means that you haven’t enabled USB debugging,
你需要为你的设备安装新版的USB开发驱动程序
or you need to install the appropriate USB development drivers for your device.
谷歌有关于这方面的文档
Google has some documentation on this,
在同步指南中你能找到它的链接
and you’ll find links to it in the written companion.
好了 显而易见 这仅仅是个开端 你今天为自己编写了
Well, obviously this is just the beginning but you have today written yourself
有一点点用户界面的第一个APP
your first Android app with a little bit of user interaction.
你现在应该做的就是写你的第二个APP 然后第三个
And what you should now do is write your second app and then your third app.
持续添加 持续学习谷歌有大量的培训材料
Keep adding to it, keep learning. Google has lots of lots of training material.
这里有大量文档和示例代码 但是务必记得
There’s lots of documentation, there’s lots of sample code, but the thing to remember is
坚持享受 坚持体验 坚持享受你将会发现它的回馈
just keep having fun, keep experimenting and keep having fun, you’ll find it very rewarding.
现在请继续回访androidauthority.com网站
Now do keep coming back to androidauthority.com,
因为我们将持续提供开发者相关内容给你
because we’re gonna keep posting developer ralated content for you,
新的教程 未来新的方面 所以务必坚持
new tutorials, new things in the future, so do keep tuning in.
你应该也注册了Android Authority
You should probably also sign up to the Android Authority
每周开发资讯 在#网址#你会发现
weekly developer newsletter, you’ll find that at devweekly.androidauthority.com.
如果你喜欢这个视频 请点赞
If you enjoyed the video, please give it a thumbs up
不要忘记订阅Android Authority的YouTube频道
also don’t forget to subscribe to our Android Authority YouTube channel.
如果你遇到任何问题 请使用下方的评论区
If you’ve got any questions, please use the comments below and we’ll see if we can help you,
假如你在开发第一个APP时有任何问题 我们会设法提供帮助
if you had any problems developing this first app.
下个视频再见
And as for me, I’ll see you in my next video.

发表评论

译制信息
视频概述

不论你是从PC端开发转到安卓端,还是在校学习的大学生,学习安卓开发都是一件非常有趣的事情。现在跟着我,开发属于你自己的第一个安卓APP吧

听录译者

BinWeber

翻译译者

GreenT

审核员

审核团审核员XD

视频来源

https://www.youtube.com/watch?v=mAJeK283j0I

相关推荐