展会信息港展会大全

Android开发-Intent和Activity
来源:互联网   发布日期:2016-01-14 09:34:16   浏览:2141次  

导读:Intent的基本作用: 一个Intent对象包含了一组信息: 1. Component name 2. Action 3. Date 4. Category 5. Extras 6. Flags Intent 概述 Intent是Android的核心组件,利用消息实现应用程序间的交互机制,这种消息描述了应用中一次操作的动作、数据以及附加...

Intent的基本作用:

一个Intent对象包含了一组信息:

1.Component name

2.Action

3.Date

4.Category

5.Extras

6.Flags

Intent 概述

Intent是Android的核心组件,利用消息实现应用程序间的交互机制,这种消息描述了应用中一次操作的动作、数据以及附加数据,系统通过该Intent的描述负责找到对应的组件,并将Intent传递给调用的组件,完成组件的调用。

Intent由动作、数据、分类、类型、组件和扩展信息等内容组成,每个组成都由相应的属性进行表示,并提供设置和获取相应属性的方法

属性

设置属性方法

获取属性方法

动作

Action

setAction()

getAction()

数据

Data

setData()

getData()

分类

Category

setCategory()

类型

Type

setType()

getType()

组件

Component

setComponent()

setClass()

setClassName()

getComponent()

扩展信息

Extra

putExtra()

getXXXExtra()获取不同数据类型的数据,如int类型则使用getIntExtra(),字符串则使用getStringExtra()

getExtras()获取Bundle包

在Android中,Intent和Activity是直接相互操作的。Intent的最常见的用途是绑定应用程序组件。Intent用来在应用程序的Activity间启动、停止和传输。

为了打开应用程序中的不同的界面(对应一个Activity),调用startActivity,传入一个Intent,如:startActivity(myIntent);

由上图我们可以大致了解Activity和Intent之间的关系。

下面通过一个实例来说明如何利用Intent对Activity进行操作的:

1.创建一个Android项目(Activity02)

代码清单分别为:

1)Android02.java

2)otherActivity.java

3)main.xml

4)other.xml

5) string.xml

6)Android02Manifest.xml

=》》Android02.java

[java] <span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class Activity02 extends Activity {

/** Called when the activity is first created. */

private Button myButton = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myButton = (Button)findViewById(R.id.myButton);

myButton.setText("myButton");

myButton.setOnClickListener(new MyButtonListener());

}

class MyButtonListener implements OnClickListener{

public void onClick(View v) {

// TODO Auto-generated method stub

//生成一Intent对象

Intent intent = new Intent();

intent.setClass(Activity02.this,otherActivity.class);

Activity02.this.startActivity(intent);

}

}

}</span>

<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class Activity02 extends Activity {

/** Called when the activity is first created. */

private Button myButton = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myButton = (Button)findViewById(R.id.myButton);

myButton.setText("myButton");

myButton.setOnClickListener(new MyButtonListener());

}

class MyButtonListener implements OnClickListener{

public void onClick(View v) {

// TODO Auto-generated method stub

//生成一Intent对象

Intent intent = new Intent();

intent.setClass(Activity02.this,otherActivity.class);

Activity02.this.startActivity(intent);

}

}

}</span>

=》》otherActivity.java

[java]

<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class otherActivity extends Activity{

private TextView myTextView = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.other);

myTextView = (TextView)findViewById(R.id.myTextView);

myTextView.setText(R.string.other);

}

}

</span>

<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class otherActivity extends Activity{

private TextView myTextView = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.other);

myTextView = (TextView)findViewById(R.id.myTextView);

myTextView.setText(R.string.other);

}

}

</span>

=》》main.xml

[html]

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:id="@+id/myButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout></span>

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:id="@+id/myButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout></span>

=》》other.xml

[html]

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/myTextView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

</span>

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/myTextView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

</span>

=》》string.xml

[html]

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, Activity02!</string>

<string name="app_name">Activity02</string>

<string name="other">otherActivity</string>

</resources></span>

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, Activity02!</string>

<string name="app_name">Activity02</string>

<string name="other">otherActivity</string>

</resources></span>

=》》Android02Manifest.xml

[html]

<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.Activity02"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="4" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:name=".Activity02"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activityandroid:name=".otherActivity" android:label="@string/other"/>

</application>

</manifest></span>

模拟器运行结果截图:

按下Button按钮就执行otherActivity

摘自 wwj_748

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港