您的位置:首页 > 移动开发 > Android开发

Android学习笔记(2)——使用Intent在Activity间传递

2013-06-23 20:50 369 查看
本例将说明如何在不同的Activity间切换,并可借助Intent进行值的传递:

Activity(一个Android程序由多个Activity组成):


布局文件res/layout/*.xml,前台页面布局


Activity类函数src/com*包/*.java,后台业务逻辑


AndroidManifest.xml程序配置文件,注册Activity

为了演示多个Activity间的切换,我们首先new几个Activity:

————————————————MainActivity:————————————————
布局(res/layout/activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />

<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:layout_centerVertical="true"
android:text="@string/hello_world" />"

</RelativeLayout>


button的id为myButton,

TextView和Button的text属性赋值为R.java中对应string/hello_world键的值:(R.java记录了所有键值对的引用,包括布局layout,图片drawable,string键值等等,R.java不可手动更改其内容,在操作res下的文件时,系统自动生成



后台业务逻辑(src/com.example.activity_02.MainActivity.java):

package com.example.activity_02;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
* 主Activity
*/
public class MainActivity extends Activity {

private Button myButton=null;

@Override
/**
* 重写onCreate
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);    //设置布局文件

//根据R.java中登记的id查找控件
myButton=(Button)findViewById(R.id.myButton );
//绑定监听到button上
myButton.setOnClickListener(new MyButtonListener());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

/**
* button的点击监听类
*/
class MyButtonListener implements OnClickListener{

@Override
public void onClick(View v) {

//生成一个Intent对象
Intent intent =new Intent();

//附加数据Extras
intent.putExtra("textIntent", "123");

//Intent请求的是OtherActivity.class
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);

}

}

}


使用findViewById获取和设置页面布局中的控件(需要有id),

使用内部类的方式,设置button控件的点击事件监听setOnClickListener

重写button的Click事件,生成一个Intent对象,使用他的Extra附带一个键值对("textIntent","123"),设置要切换去的Activity是OtherActivity(稍后new),动作是startActivity启动另一个Activity,

Intent: 一个请求(包含很多信息)

Component name:决定去启动哪一个Activity

Action:启动了那个Activity之后,我要做什么

Data:从一个Activity想要向另一个Activity传送什么数据

Category:

Extras:额外信息(键值对),可从一个Activity传送到另一个Activity取出来

Flags:

配置文件AndroidManifest.xml

<activity
android:name="com.example.activity_02.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>


注册MainActivity的信息(系统会默认生成主Activity的注册,并设置为启动Activity)

————————————————OtherActivity:————————————————
布局(res/layout/other.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</LinearLayout>


TextView的id为myTextView,

后台业务逻辑(src/com.example.activity_02.OtherActivity.java):

package com.example.activity_02;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

/**
* 另一个Activity
*/
public class OtherActivity extends Activity {

private TextView myTextView=null;

@Override
/**
* 重写onCreate
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//设置布局文件,根据R.java中登记的layout布局文件id
setContentView(R.layout.other);

//获取由上一个Activity传递过来的Intent对象
Intent intent=getIntent();
//获取这个Intent对象的Extra中对应键的值
String value=intent.getStringExtra("textIntent");

//获取、设置TextView的文本值
myTextView=(TextView)findViewById(R.id.myTextView);
myTextView.setText(value);

}

}


使用setContentView来设置布局文件,
在getIntent对象之后,获取Intent从上一个Activity传过来的键值"textIntent"的值,放在myTextView中,

配置文件AndroidManifest.xml

<activity
android:name=".OtherActivity"
android:label="@string/other"
/>


注册OtherActivity

启动ADV模拟器,运行效果如下:



点击MainActivity主界面中的button按钮,



显示另一个Activity界面,并显示从前一个界面传来的值"123"

不同的Activity间切换,可以是不同程序的不同Activity,例如修改主界面MainActivity中button点击处理如下:

/**
* button的点击监听类
*/
class MyButtonListener implements OnClickListener{

@Override
public void onClick(View v) {

//            //生成一个Intent对象
//            Intent intent =new Intent();
//
//            //附加数据Extras
//            intent.putExtra("textIntent", "123");
//
//            //Intent请求的是OtherActivity.class
//            intent.setClass(MainActivity.this, OtherActivity.class);
//            MainActivity.this.startActivity(intent);

Uri uri=Uri.parse("smsto://0800000123");
Intent intent =new  Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);

}


需要导入包import android.net.Uri;

重新Debug,效果如下:



当点击主界面MainActivity中的button后,程序跳转到发信息界面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: