安卓应用开发基础外文翻译资料

 2022-07-29 17:30:23

Android Application Fundamentals

Writer. Android.developer

An activity is an application component that allows a user to interact with the screen they provide to perform actions such as making a call, taking a photo, sending an email, or viewing a map. Each activity gets a window for drawing its user interface. Fill the screen, but it can also be smaller than the screen and float over other windows.

An application is usually composed of multiple activities that are loosely linked to each other. Typically, an activity in the app is specified as the 'main' activity, the activity that is presented to the user when the application is first launched. And each activity can start another activity in order to perform different operations. The activity stops when the new activity starts, but the activity is retained on the stack ('return stack'). When the new activity starts, the system will push it back to the stack and get the user#39;s focus. The return stack follows the basic 'last in first out' stack mechanism, so that when the user completes the current activity and presses the 'return' button, the system pops it from the stack and resets the previous activity (task and returns The document on the return stack has a more detailed description).

When an activity is stopped due to a new activity, the system will notify the state of the change through the activity#39;s life cycle callback method. Activity changes due to state - whether the system is creating activities, stopping activities, resuming activities, or destroying activities - and there are several types of callback methods that are received, each of which provides you with the opportunity to perform specific actions that correspond to the state change The For example, when you stop, your activity should release any large objects, such as network or database connections. When the activity is restored, you can regain the required resources and resume the execution of the interrupted operation. These state changes are part of the activity lifecycle.

The remainder of this article explains the basics of how to create and use activities, including a comprehensive description of the activities of the lifecycle, so that you can manage the transition between the various activities.

Create an activity

To create an activity, you must create an active subclass (or use its existing subclass). You need to implement the callback method of the system call in the subclass between the various states of its lifecycle (such as creating an activity, stopping an activity, restoring activity, or destroying the activity time). The callback method is the two most important callback methods:

OnCreate ()

You must implement this method. This method is called when you create your activity. You should initialize the required components within the activity. Most importantly, you must call setContentView () within this method to define the layout of the active user interface.

OnPause ()

The system calls this method as the first signal that the user leaves the activity (but does not always mean that the activity is destroyed). You should normally within this method confirm that any changes that are still valid after the current user session ends may not be returned).

You also use several other lifecycle callback methods to provide a smooth user experience between activities and to handle unexpected disruptions that cause your activity to stop or even be destroyed. The life cycle part of the management activities described later describes all the life cycle callbacks.

Implement the user interface

The user interface of the Activity is provided by the hierarchical view - derived from the object of the view class. Each view controls the specific rectangular space within the active window and responds to user interaction. For example, a view can start a button for an action when the user touches it.

You can design and organize your layout using many of the off-the-shelf views provided by Android. A 'widget' is a view that provides a screen visual (interactive) element such as a button, a text field, a checkbox, or just an image. Is a view derived from viewing a group that provides a unique layout model for its child view, such as a linear layout, a grid layout, or a relative layout. You can also view classes and view sets of class classes to create subclasses (or use their existing subclasses) to create widgets and layouts yourself and then apply them to your active layout.

The most common way to define a layout using views is to borrow an XML layout file that is stored within your application#39;s resources. In this way, you can separate the user interface from the source code that defines the behavior of the activity. You can set the layout to the active UI by settingContentView () to pass the resource ID of the layout. However, you can also create a new view in the active code and create the view hierarchy by inserting the new view into the ViewGroup and then use the layout by passing ViewGroup to setContentView ().

For information about creating user interfaces, see the user interface documentation.

Declare the activity in the manifest file

You must declare your activity in the manifest file so that the system can access it. To claim your activity, open your manifest file and add the lt;elementgt; element as a child of the lt;applicationgt; element.

lt;Manifest ...gt;

lt;Application ...gt;

lt;Activity android: name = '. ExampleActivity' /gt;

...

lt;/ Application ...gt;

...

lt;/ Manifestgt;

You can also add several other features to this element to define the properties of the active UI, such as the active tag, the active icon, or the style theme, that are used to set the active UI style. The android: name attribute is the only required attribute -

全文共32268字,剩余内容已隐藏,支付完成后下载完整资料


安卓应用开发基础

作者:Android developer

活动是一个应用组件,用户可以与其提供的屏幕进行交互,以执行拨打电话,拍摄照片,发送电子邮件或查看地图等操作。每个活动都会获得一个用于绘制其用户界面的窗口。充满屏幕,但也可小于屏幕并浮动在其他窗口之上。

一个应用通常由多个彼此松散联系的活动组成。一般会指定应用中的某个活动为“主”活动,即首次启动应用时呈现给用户的那个活动。而且每个活动均可启动另一个活动,以便执行不同的操作。每次新活动启动时,前一活动便会停止,但系统会在堆栈(“返回栈”)中保留该活动。当新活动启动时,系统会将其推送到回回栈上,并取得用户焦点。返回栈遵循基本的“后进先出”堆栈机制,因此,当用户完成当前活动并按“返回”按钮时,系统会从堆栈中将其弹出(并销),然后恢复前一活动(任务和返回文档中对对返回栈有更详细的阐述)。

当一个活动因某某新活动启动而停止时,系统会通过该活动的生命周期回调方法通知其这一状态变化。活动因状态变化 - 系统是创建活动,停止活动,恢复活动还是销毁活动 - 而收到的回调方法可能有几种种,每一种回调都会为提供执行与该状态变化相应的特定操作的机会。例如,停止时,活动应释放任何大型对象,例如网络或数据库连接。当活动恢复时,可以重新获得所需资源,并恢复执行中断的操作。这些状态转变都是活动生命周期的一部分。

本文的其余部分阐述有关如何创建和使用活动的基础知识(包括对活动生命周期工作方式的全面阐述),以便正确管理各种活动状态之间的转变。

创建活动

要创建活动,必须创建活动的子类(或使用其现有子类)。需要在子类中实现活动在其生命周期的各种状态之间转变时(例如创建活动,停止活动,恢复活动或销毁活动时间系统调用的回调方法是两个最重要的回调方法:

onCreate()

必须实现此方法。系统会在创建活动时调用此方法。应该在实现内初始化活动的必需组件。最重要的是,必须在此方法内调用setContentView(),以定义活动用户界面的布局。

onPause()

系统将此方法作为用户离开活动的第一个信号(但并不总是意味着活动会被销毁)进行调用。通常应该在此方法内确认在当前用户会话结束后仍然有效的任何更改用户可能不会返回)。

还使用几种其他生命周期回调方法,以便提供流畅的活动间用户体验,以及处理导致活动停止甚至被销毁的意外中断。后文的管理活动生命周期部分对所有生命周期回调方法进了阐述。

实现用户界面

Activity的用户界面是由层级式视图 - 衍生自查看类的对象 - 提供的。每个视图都控制活动窗口内的特定矩形空间,可对用户交互作出响应。例如,视图可以在用户触摸时启动某项操作的按钮。

可以使用Android提供的许多现成视图设计和组织布局。“小部件”是提供按钮,文本字段,复选框或仅仅是一幅图像等屏幕视觉(交互式)元素的视图。是衍生自查看群组的视图,为其子视图提供唯一布局模型,例如线性布局,网格布局或相对布局。还可以查看类和查看集类类创建子类(或使用其现有子类)来自行创建小部件和布局,然后将它们应用于活动布局。

利用视图定义布局的最常见的方法是借用保存在你的应用资源内的XML布局文件。这样一来,你就可以将用户界面的设计与定义活动行为的源代码分开维护你可以通过setContentView()将布局设置为活动的UI,从而传递布局的资源ID。不过,也可以在活动代码中创建新视图,并且通过将新视图插入ViewGroup来创建视图层次,然后通过将ViewGroup传递到setContentView()来使用该布局。

在清单文件中声明活动

必须在清单文件中声明活动,这样系统才能访问它。要求声明活动,请打开清单文件,并将lt;elementgt;元素添加为lt;applicationgt;元素的子项。

lt;manifest ...gt;

lt;application ...gt;

lt;activity android:name =“。ExampleActivity”/gt;

... ...

lt;/ application ...gt;

... ...

lt;/ manifestgt;

还可以在此元素中加入几个其他特性,以定义活动标签,活动图标或风格主题等用于设置活动UI风格的属性android:name属性是唯一必需的属性 - 它指定活动的类名。应用一旦发布,即不应更改此类名称,否则,可能会破坏诸如应用快捷方式等一些功能(请阅读博客文章不能更改的内容]。

使用Intent过滤器

lt;activitygt;元素还可指定各种Intent过滤器 - 使用lt;intent-filtergt;元素 - 以声明其他应用组件激活它的方法。

当使用Android SDK工具创建新应用时,系统自动为创建的存活活动包含一个Intent过滤器,其中声明了该活动响应“主”操作且应置于“launcher”类别内。Intent过滤器的内容如下图所示:

lt;activity android:name =“。ExampleActivity”android:icon =“@ drawable / app_icon”gt;

lt;intent-filtergt;

lt;action android:name =“android.intent.action.MAIN”/gt;

lt;category android:name =“android.intent.category.LAUNCHER”/gt;

lt;/ intent-filtergt;

lt;/ activitygt;

lt;actiongt;元素指定这是应用的“主”入口点。lt;categorygt;元素指定此活动应列入系统的应用启动器内(以便用户启动该活动)。

如果打算让应用成为独立应用,不允许其他应用激活其活动,则不需要任何其他Intent过滤器。正如前例所示,只有一个活动具有“主”操作和“launcher”类别。提供给其他应用的活动不应有任何意图过滤器,可以利用显式意志自行启动它们(下文对此做了阐述)。

不过,如果想让活动对衍生自其他应用(以及自有应用)的隐式意志作出响应,则必须为活动定义其他意图过滤器。对于想要作出响应的每一个意图类型,必须加入相应的lt;intent-filtergt;,其中包括一个lt;actiongt;元素,还可选择性包括一个lt;categorygt;元素和/或一个lt;datagt;元素。这些元素指定活动可以响应的Intent类型中。

启动活动

可以通过调用startActivity(),并将其传递给描述想启动的Activity的Intent来启动另一个Activity.Intent对象会指定想启动的具体Activity或描述想执行的操作类型(系统会为选择合适的活动,甚至是来自其他应用的活动。)Intent对象还可能携带少量供所启动活动使用的数据。

在自有应用内工作时,经常只需要启动某个已知活动。可以通过使用类名创建一个显式定义想启动的活动的意图对象来实现此目的。例如,可以通过以下代码让一个活动启动另一个名为SignInActivity的活动:

Intent intent = new Intent(this,SignInActivity.class);

startActivity(intent);

不过,应用可能还需要使用活动数据执行某项操作,例如发送电子邮件,短信或状态更新。在这种情况下,应用自身可能不具有执行此类操作所需的活动,因此可以改为利用设备上其他应用提供的活动为执行这些操作。这便是Intent对象的真正价值所在 - 可以创建一个Intent对象,对想执行的操作进行描述,系统会从其他应用启动相应的活动。如果有多个活动可以处理Intent,则用户可以选择要使用哪一个。例如,如果想允许用户发送电子邮件,可以创建以下Intent:

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_EMAIL,recipientArray);

startActivity(intent);

添加到Intent中的EXTRA_EMAIL extra是一个字符串数组,其中包含应该电子邮件发送到的电子邮件地址。当电子邮件应用响应此意向时,它会读取额外中提供的字符串数组,并将它们放入电子邮件撰写窗体的“收件人”字段。在这种情况下,电子邮件应用的活动启动,并且当用户完成操作时,活动会恢复执行。

启动活动以获得结果

有时,可能需要从启动的活动获得结果。在这种情况下,请通过调用startActivityForResult()(而非startActivity())来启动活动。要求在随后收到后续活动的结果,请实现onActivityResult )回调方法。当后续活动完成时,它会使用Intent向onActivityResult()方法返回结果。

Activity 是一个应用组件,用户可与其提供的屏幕进行交互,以执行拨打电话、拍摄照片、发送电子邮件或查看地图等操作。 每个 Activity 都会获得一个用于绘制其用户界面的窗口。窗口通常会充满屏幕,但也可小于屏幕并浮动在其他窗口之上。

一个应用通常由多个彼此松散联系的 Activity 组成。 一般会指定应用中的某个 Activity 为“主”Activity,即首次启动应用时呈现给用户的那个 Activity。 而且每个 Activity 均可启动另一个 Activity,以便执行不同的操作。 每次新 Activity 启动时,前一 Activity 便会停止,但系统会在堆栈(“返回栈”)中保留该 Activity。 当新 Activity 启动时,系统会将其推送到返回栈上,并取得用户焦点。 返回栈遵循基本的“后进先出”堆栈机制,因此,当用户完成当前 Activity 并按“返回”按钮时,系统会从堆栈中将其弹出(并销毁),然后恢复前一 Activity。 (任务和返回栈文档中对返回栈有更详细的阐述。)

当一个 Activity 因某个新 Activity 启动而停止时,系统会通过该 Activity 的生命周期回调方法通知其这一状态变化。Activity 因状态变化—系统是创建 Activity、停止 Activity、恢复 Activity 还是销毁 Activity— 而收到的回调方法可能有若干种,每一种回调都会为提供执行与该状态变化相应的特定操作的机会。 例如,停止时, Activity 应释放任何大型对象,例如网络或数据库连接。 当 Activity 恢复时,可以重新获取所需资源,并恢复执行中断的操作。 这些状态转变都是 Activity 生命周期的一部分。

创建 Activity

要创建 Activity,必须创建 Activity 的子类(或使用其现有子类)。需要在子类中实现 Activity 在其生命周期的各种状态之间转变时(例如创建 Activity、停止 Activity、恢复 Activity 或销毁 Activity 时)系统调用的回调方法。 两个最重要的回调方法是:

onCreate()

系统会在创建 Activity 时调用此方法。应该在实现内初始化 Activity 的必需组件。 最重要的是,必须在此方法内调setContentView(),以定义 Activity 用户界面的布局。

onPause()

系统将此方法作为用户离开 Activity 的第一个信号(但并不总是意味着 Activity 会被销毁)进行调用。 通常应该在此方法内确认在当前用户会话结束后仍然有效的任何更改(因为用户可能不会返回)。

实现用户界面

Activity 的用户界面是由层级式视图 — 衍生自 View 类的对象 — 提供的。每个视图都控制 Activity 窗口内的特定矩形空间,可对用户交互作出响应。 例如,视图可以是在用户触摸时启动某项操作的按钮。

利用 Android 提供的许多现成视图设计和组织布局。“小部件”是提供按钮、文本字段、复选框或仅仅是一幅图像等屏幕视觉(交互式)元素的视图。 “布局”是衍生自 ViewGroup 的视图,为其子视图提供唯一布局模型,例如线性布局、网格布局或相对布局。 还可以为 View 类和 ViewGroup 类创建子类(或使用其现有子类)来自行创建小部件和布局,然后将它们应用于 Activity 布局。

利用视图定义布局的最常见方法是借助保存在应用资源内的 XML 布局文件。这样一来,就可以将用户界面的设计与定义 Activity 行为的源代码分开维护。 可以通过 setContentView() 将布局设置为 Activity 的 UI,从而传递布局的资源 ID。不过,也可以在 Activity 代码中创建新 View,并通过将新 View 插入 ViewGroup 来创建视图层次,然后通过将根 ViewGroup 传递到 setContentView() 来使用该布局。

在清单文件中声明 Activity

必须在清单文件中声明 Activity,这样系统才能访问它。 要声明 Activity,请打开清单文件,并将

全文共18647字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[143279],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。