android实例教程
In our last Android Tutorial, we discussed the Android Hello World Application. In this Android tutorial we’ll delve into a basic building block of Android called Intent.
在上一个Android教程中,我们讨论了Android Hello World应用程序。 在本Android教程中,我们将深入研究称为Intent的Android基本构建块。
什么是Android Intent? (What is Android Intent?)
Android intent is a data structure containing a description of a to-be-performed operation.
One of the most powerful features of Intent is that you can send asynchronously messages to other activities and services.
An intent is always handled by an Android component, namely an activity, a service or a broadcast receiver. In this tutorial, we will focus on activities as intent handlers.
Android意图是一种数据结构,其中包含要执行的操作的描述。
Intent最强大的功能之一是,您可以将消息异步发送到其他活动和服务。
意图始终由Android组件(即活动,服务或广播接收器)处理。 在本教程中,我们将重点放在作为意图处理程序的活动上。
Android意向类型 (Android Intent Types)
- Explicit intent specify the component to start by name (the fully-qualified class name). You’ll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. When you create an explicit intent to start an activity or service, the system immediately starts the app component specified in the Intent object. 明确的意图指定组件以名称(全限定类名)开头。 通常,您将使用显式意图在自己的应用程序中启动组件,因为您知道要启动的活动或服务的类名。 创建显式意图以启动活动或服务时,系统会立即启动Intent对象中指定的应用程序组件。
- Implicit intent do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. 隐式意图不命名特定组件,而是声明要执行的常规操作,该操作允许另一个应用程序中的组件进行处理。 创建隐式意图时,Android系统会通过将意图的内容与设备上其他应用的清单文件中声明的意图过滤器进行比较,找到合适的组件来开始。 如果意图与意图过滤器匹配,则系统将启动该组件并将其交付给Intent对象。
我们如何使用android intent启动活动? (How do we use android intent to launch activities?)
To create an intent and launch an activity with it, we can write the following code within our caller activity:
要创建意图并使用它启动活动,我们可以在调用者活动中编写以下代码:
Intent intent = new Intent(CallerActivity.this, CallingActivity.class);
startActivity(intent);
Running this code snippet has the following consequences:
运行此代码段具有以下后果:
- A new instance of CallingActivity is created 创建了CallingActivity的新实例
- The instance is pushed on top of the current task’s stack, which is the one the caller activity is in. 该实例被推入当前任务堆栈的顶部,这是调用者活动所在的堆栈。
- The activity is started and brought to the foreground. 该活动已启动并带到前台。
使用Android意向标志 (Using Android Intent Flags)
Flags defined in the Intent class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity and how to treat it after it’s launched.
The described behaviour can be modified by specifying intent flags on the intent instance before passing the intent to startActivity(Intent), for example:
在Intent类中定义的标志,用作标志的元数据。 这些标志可以指示Android系统如何启动活动以及在启动活动后如何对其进行处理。
可以通过在将意图传递给startActivity(Intent)之前在意图实例上指定意图标志来修改所描述的行为,例如:
intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This will bring an existing instance of the called activity type present in the current stack to the foreground.
这会将当前堆栈中存在的调用活动类型的现有实例带到前台。
项目结构 (Project Structure)
This project consists of two activities named FirstActivity.java and SecondActivity.java. Leaving aside the regulars, one distinguishable directory in the resource folder is anim. It contains Animation resource files which we’ll see at length later in this tutorial.
该项目包含两个名为FirstActivity.java和SecondActivity.java的活动。 撇开常规,在资源文件夹中的一个可区分目录是动画。 它包含动画资源文件,我们将在本教程的后面部分详细介绍。
使用按钮 ()
In this tutorial we will bring buttons into use to show Intents between Activities.
A Button is a Push-button which can be pressed, or clicked, by the user to perform an action.
It is inherited from android.widget.TextView Class.
android.widget.Button class is used to display a normal button. To make a button react when user clicks it, a click event must be registered.For this a OnClickListener() method is added. Here we’ll invoke the intent object inside the method. Following snippet shows how its done:
在本教程中,我们将使用按钮来显示“活动之间的意图”。
按钮是用户可以按下或单击以执行操作的按钮。
它继承自android.widget.TextView类。
android.widget.Button类用于显示普通按钮。 为了使按钮在用户单击时做出React,必须注册一个click事件。为此,添加了一个OnClickListener()方法。 在这里,我们将在方法内部调用intent对象。 以下代码片段显示了它是如何完成的:
FirstActivity.java
FirstActivity.java
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}});
Saw that? That’s nothing. That’s just 5 minutes of coding.
看见了? 没什么。 只需5分钟的编码。
添加动画 (Adding Animation)
Now let’s add a transition effect when the Intent is performed.
To do this, add two animation resource files named activity_in.xml and activity_out.xml under res->anim. These contain the transition effects when an Intent occurs. Following screenshot shows activity_in and activity_out correspondingly.
现在让我们在执行Intent时添加过渡效果。
为此,请在res-> anim下添加两个名为activity_in.xml和activity_out.xml的动画资源文件。 这些包含发生意图时的过渡效果。 以下屏幕截图分别显示activity_in和activity_out。
activity_in.xml
activity_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%" android:toXDelta="0" android:duration="200" />
</set>
activity_out.xml
activity_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%" android:duration="200" />
</set>
As you can see,the deltaX values are complementary in both the pics. This is obvious since the new activity moves in and the previous activity has to move out.
Now just add the following snippet after startActivity(intent) :
如您所见,这两张照片中的deltaX值是互补的。 这是显而易见的,因为新活动将移入而先前活动必须移出。
现在,只需在startActivity(intent)之后添加以下代码段:
overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
Build and run the project in android emulator, you will get something like below.
在android模拟器中构建并运行项目,您将获得类似以下的内容。
In the next article we’ll discuss at length on Spinners, Toasts and Passing Data through Bundles. You can download android intent project from below link.
在下一篇文章中,我们将详细讨论Spinners,Toasts和通过Bundles传递数据 。 您可以从下面的链接下载android intent项目。
翻译自: https://www.journaldev.com/9044/android-intent-handling-between-activities-example-tutorial
android实例教程