学习意图将数据传递给目标活动; 初学者需要好好研究的
1,将下面的代码添加到main.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/btn_SecondActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to go to Second Activity"
android:onClick="onClick"
/>
</LinearLayout>
2,将下面的代码添加到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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcom to Second Activity"
/>
<Button
android:id="@+id/btn_MainActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to return to mian activity"
android:onClick="onClick"
/>
</LinearLayout>
3,在src中创建信的class文件mainBundle.java
package com.example.listener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/**
*
* @author Administrator 意图返回值的使用联系 返回码,结果码, Bundle对象传递数据
*/
public class mainBundle extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// 按钮监听器
public void onClick(View v) {
// 意图传递参数
Intent intent = new Intent();
intent.setAction("com.example.listenersecondBundle");
intent.putExtra("str1", "This is a String");
intent.putExtra("agel", 25);
// 使用Bundle对象来传递参数
Bundle bundle = new Bundle();
bundle.putString("str2", "This is a Bundle");
bundle.putInt("age2", 35);
intent.putExtras(bundle);// 将bundle对象放入意图中
startActivityForResult(intent, 1);// 启动意图返回码为1
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
//字符串
Toast.makeText(this, Integer.toString(data.getIntExtra("age3", 0)),
Toast.LENGTH_SHORT).show();
//数字
Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
}
}
}
4,src中创建一个新的class文件 secondBundle
package com.example.listener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/**
*
* @author Administrator
*
*/
public class secondBundle extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Toast.makeText(this, getIntent().getStringExtra("str1"),
Toast.LENGTH_SHORT).show();
Toast.makeText(this,
Integer.toString(getIntent().getIntExtra("age1", 0)),
Toast.LENGTH_SHORT).show();
Bundle bundle = getIntent().getExtras();// 意图对象
Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT)
.show();
Toast.makeText(this, Integer.toString(bundle.getInt("age2")),
Toast.LENGTH_SHORT).show();
}
// 按钮监听器
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("age3", 45);
intent.setData(Uri.parse("返回的值"));
setResult(RESULT_OK, intent);
finish();
}
}
5,在AndroidManifest.xml中添加下面代码;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listener"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.listener.mainBundle"
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=".secondBundle"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.listenersecondBundle" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!--
<activity
android:name=".SecondActivity">
<intent-filter>
<action android:name="com.newer.baihe"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.baihe.aaa" />
<category android:name="com.baihe.bbb" />
<category android:name="com.baihe.ccc" />
<data
android:scheme="baihe"
android:host="baidu.com"
android:path="/index.html"
android:mimeType="text/*"
/>
</intent-filter>
</activity>
-->
</application>
</manifest>
6,代码分析;
1), Intent方法添加数据,使用键值对 putExtra()方法
intent.putExtra("str1","This is a String");
intent.putExtra("age1",25);
2),除了使用putExtra()方法,还可以创建一个Bundle对象,并使用putExtras将Bundle对象添加给Intent对象
Bundle bundle = new Bundle();
bundle.putString("str2","This is a Bundle");
bundle.putInt("age2",35);
intent.putExtras(bundle);
3),在活动中,为了获得Intent对象发送的数据,使用getIntent()来获取Intent对象,然后再使用getStringExtra获取发送的字符串,获取int型数据
获取字符串;
Toast.makeText(this,getIntent().getStringExtra("str1"),Toast.LENGTH_SHORT).show();
获取数字
Toast.makeText(this,Integer.toString(getIntent().getIntExtra("age1",0)),Toast.LENGTH_SHORT).show();
4),为了获取Bundle数据需要使用getExtras()方法
Bundle bundle= getIntent().getExtras();
根据不同的数据类型来获取数据,字符串使用toString获取,
Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT).show();
类似,使用getInt()方法获取整数值
Toast.makeText(this,Integer.tostring(( bundle.getInt("age2")), Toast.LENGTH_SHORT).show();
5), 最后一种就是使用setData()方法传递数据;
传递数据
intent.setData(Uri.parse("返回的值"));
获取数据
Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();