Android入门:HelloWorld程序详解

需积分: 9 6 下载量 121 浏览量 更新于2024-08-02 收藏 779KB PDF 举报
"Android程序员指南的部分翻译,涵盖了创建Android项目的步骤、使用View和TextView、修改main.xml文件以及在Android Emulator上运行应用等基础知识。" 在Android开发领域,"Android-A Programmer's Guide" 是一本非常重要的参考书籍,它为开发者提供了深入理解Android应用程序开发的路径。这篇译文主要摘录了书中的第五章,适合初学者入门。以下是该章节涉及的关键技术和概念的详细说明: 1. 创建新的Android项目: 在Eclipse或Android Studio这样的集成开发环境(IDE)中,你需要设置一个新的项目,选择Android工程模板,并指定目标API级别。这会生成一系列基础文件和目录,包括AndroidManifest.xml、res、src等。 2. 使用View: View是Android UI的基础组件,它是所有可见用户界面元素的父类。例如,TextView、Button、ImageView等都是View的子类。你可以通过XML布局文件或编程方式在活动中添加View。 3. 使用TextView: TextView用于显示文本,是Android中最常用的控件之一。你可以设置文本内容、字体样式、大小和颜色等属性,也可以实现动态文本更新。 4. 修改main.xml文件: main.xml是Android应用中的布局文件,通常位于res/layout目录下。这个XML文件定义了UI的结构和组件。你可以在这里添加、删除或修改View元素,调整它们的属性,以构建用户界面。 5. 在Android Emulator上运行应用程序: Android Emulator模拟真实设备的环境,让开发者可以在没有物理设备的情况下测试应用。通过IDE,你可以启动Emulator,部署应用并观察运行效果,调试可能出现的问题。 6. Activity与Application的关系: Activity是Android应用程序的基本单元,每个Activity代表用户界面的一个独立视图或窗口。一个Application可以包含多个Activity,它们之间可以通过Intent进行通信。每个应用程序至少需要一个Activity作为入口点。 7. HelloWorld示例: 传统的HelloWorld程序是学习新语言或平台的基本步骤。在Android中,你可以通过TextView显示文本“HelloWorld”,或者使用ImageView显示包含“HelloWorld”文字的图片,以此理解不同方式向用户呈现内容。 通过这个章节的学习,开发者将掌握Android开发的基本流程,理解Activity和View系统的运作机制,为后续更复杂的应用开发奠定基础。对于初学者来说,实践这些示例和概念是掌握Android开发的关键。随着技能的提升,开发者可以进一步探索Intent、BroadcastReceiver、Service等高级话题,以构建功能更丰富的应用程序。
2010-07-21 上传
1 What Is Android? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Brief History of Embedded Device Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Open Handset Alliance and Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Introduction to Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 Downloading and Installing Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Why Eclipse? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Downloading and Installing the JRE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Downloading and Installing Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3 Downloading and Installing the Android SDK . . . . . . . . . . . . . . . . . . . . . . . 21 Downloading the Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Downloading and Installing the Android Plugin for Eclipse . . . . . . . . . . . . . . . . . . . . . . 24 Configuring the Android Plugin for Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4 Exploring the Android SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 What Is in the Android SDK? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Android Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Android Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Try This: Run the API Demos Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . 41 Android Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Standard ASP Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Android Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 5 Application: Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Creating Your First Android Project in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Examining the Android-Created Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Referenced Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Hello World! Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Hello World! Using an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Hello World! Code-Based UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Hello World! XML-Based UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Try This: Use TextView and ImageView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 6 Using the Command-Line Tools and the Android Emulator . . . . . . . . . . . . 83 Creating a Shell Activity Using the Windows CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Running the ActivityCreator.bat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 The Project Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Creating the Hello World! Activity in the Windows CLI . . . . . . . . . . . . . . . . . . . . . . . . 95 Editing the Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Adding the JAVA_HOME Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Compiling and Installing the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Hello World! on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Configuring the PATH Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Try This: Create an Image-Based Hello World! in the CLI . . . . . . . . . . . . . . . . . . . 115 7 Using Intents and the Phone Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 What Are Intents? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Using the Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Placing a Call from Your Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Adding the Intent to Your Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Editing Activity Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Modifying the AndroidPhoneDialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Adding a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Implementing an EditText View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Try This: Modify the AndroidPhoneDialer Project . . . . . . . . . . . . . . . . . . . . . . . . . . 145 8 Lists, Menus, and Other Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Building the Activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Intent Code for the .xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Intent Code for the .java File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Modifying the AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Using the Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Creating the Activity for AutoComplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 EditText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 RadioGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Spinner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Try This: Modify More View Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 9 Using the Cell Phone’s GPS Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Using the Android Location-Based API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Creating a kml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 What Is a track File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Getting the nmea File in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Getting the nmea File in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Reading the GPS with the Android Location-Based API . . . . . . . . . . . . . . . . . . . . . . . . 212 Creating the AndroidLBS Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Passing Coordinates to Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Adding Zoom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Try This: Toggling Between MapView’s Standard and Satellite Views . . . . . . . . . . 232 10 Using the Google API with GTalk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Configuring the Android Emulator for GTalk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Implementing GTalk in Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Creating the Activity’s Layout in the GoogleAPI.xml . . . . . . . . . . . . . . . . . . . . . 245 Adding Packages to GoogleAPI.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Implementing the View.OnClickListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Compiling and Running GoogleAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Try This: Add a Settings Feature to Your GoogleAPI Activity . . . . . . . . . . . . . . . . 255 11 Application: Find a Friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Creating a SQLite Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Creating a Custom Content Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Editing the strings.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Creating Your Content Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Creating the FindAFriend Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Editing AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Creating the NameEditor Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Creating the LocationEditor Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Creating the FriendsMap Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Creating the FindAFriend Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Running the FindAFriend Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Try This: Real-Time Location Updating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 12 Android SDK Tool Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Android Emulator Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Android Debug Bridge Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313