Intent在活动的操作
作用:
Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互 同时Intent启动往往可以启动活动,启动服务,以及发送广播等场景
分类
1.显示Intent和隐示Intent
显示Intent操作
1.首先我们新建一个Android工程

在分别讲解显示意图和隐示意图时我们要在rec目录 -layout资源新建活动 。

然后右键点击新建一个activity_second.xml布局文件


然后在该文件下添加一个按钮组件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout> 2.新建一个活动
在我们添加完按钮组件之后同时给按钮取个名字叫button2和唯一id名,然后在src 目录工程包下新建一个活动SecondActivity继承Activity类让其成为活动类
然后重写onCreate()方法,同时给该活动设置上下文布局文件
package com.example.intentdome;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);//读取我们刚才在layout资源文件新建的布局文件
}
} 3.然后在AndroidManifest.xml工程配置文件中注册一个活动
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentdome"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
>
</activity>
</application>
</manifest> 以上步骤已经操作完毕,那我们开始下一步操作
给按钮绑定事件
1.给定MainActivit视图文件绑定一个点击事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取按钮对象
Button btn1 = (Button)findViewById(R.id.button1);
//给该按钮绑定一个监听事件
btn1.setOnClickListener(new OnClickListener() {
@Override
//当鼠标点击操作事着触发该操作
public void onClick(View v) {
}
});
} 2.创建意图对象
Intent的构 该类有多个构造函数其中一个Intent(Context packageCntext,Class<?> cls) 第一个参数:指定要启动的上下文 第二个参数:指定要启动的目标活动
//获取一个意图对象 Intent intent = new Intent(MainActivity.this,SecondActivity.class);