除了能从一个Activity返回数据结果之外,向一个Activity传递数据也是很常用的。
1.新建一个名为PassData的工程。
2.main.xml中的代码。
[
java] view plaincopy<?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/btn_SecondActivity"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:onClick="onClick"
Android:text="Click to go to Second Activity" />
</LinearLayout>
3.在res/layout文件夹下,创建secondactivity.xml文件。[
java] view plaincopy<?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:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="Welcome to Second Activity" />
<Button
Android:id="@+id/btn_MainActivity"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:onClick="onClick"
Android:text="Click to return to main activity" />
</LinearLayout>
4.新建一个Activity子类:SecondActivity.
java。[
java] view plaincopypublic class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
// ---get the data passed in using getStringExtra()---
Toast.makeText(this, getIntent().getStringExtra("str1"),
Toast.LENGTH_SHORT).show();
// ---get the data passed in using getIntExtra()---
Toast.makeText(this,
Integer.toString(getIntent().getIntExtra("age1", 0)),
Toast.LENGTH_SHORT).show();
// ---get the Bundle object passed in---
Bundle bundle = getIntent().getExtras();
// ---get the data using the getString()---
Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT)
.show();
// ---get the data using the getInt() method---
Toast.makeText(this, Integer.toString(bundle.getInt("age2")),
Toast.LENGTH_SHORT).show();
}
public void onClick(View view) {
// ---use an Intent object to return data---
Intent i = new Intent();
// ---use the putExtra() method to return some
// value---
i.putExtra("age3", 45);
// ---use the setData() method to return some value---
i.setData(Uri.parse("Something passed back to main activity"));
// ---set the result with OK and the Intent object---
setResult(RESULT_OK, i);
// ---destroy the current activity---
finish();
}
}
5.
AndroidManifest.xml中的代码。[
java] view plaincopy<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:
Android="http://schemas.
Android.com/apk/res/
Android"
package="net.horsttnann.PassingData"
Android:versionCode="1"
Android:versionName="1.0" >
<uses-sdk
Android:minSdkVersion="10" />
<application
Android:icon="@drawable/ic_launcher"
Android:label="@string/app_name" >
<activity
Android:name=".PassingDataActivity"
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="net.horsttnann.PassingData.SecondActivity"
Android:label="Second Activity" >
<intent-filter>
<action
Android:name="net.horsttnann.PassingDataSecondActivity" />
<category
Android:name="
Android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
6.PassDataActivity中的代码。
[
java] view plaincopyprint?public class PassingDataActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
Intent i = new
Intent("net.learn2develop.PassingDataSecondActivity");
//---use putExtra() to add new key/value pairs---
i.putExtra("str1", "This is a string");
i.putExtra("age1", 25);
//---use a Bundle object to add new key/values
// pairs---
Bundle extras = new Bundle();
extras.putString("str2", "This is another string");
extras.putInt("age2", 35);
//---attach the Bundle object to the Intent object---
i.putExtras(extras);
//---start the activity to get a result back---
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode,
int resultCode, Intent data)
{
//---check if the request code is 1---
if (requestCode == 1) {
//---if the result is OK---
if (resultCode == RESULT_OK) {
//---get the result using getIntExtra()---
Toast.makeText(this, Integer.toString(
data.getIntExtra("age3", 0)),
Toast.LENGTH_SHORT).show();
//---get the result using getData()---
Toast.makeText(this, data.getData().toString(),
Toast.LENGTH_SHORT).show();
}
}
}
}
public class PassingDataActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
Intent i = new
Intent("net.learn2develop.PassingDataSecondActivity");
//---use putExtra() to add new key/value pairs---
i.putExtra("str1", "This is a string");
i.putExtra("age1", 25);
//---use a Bundle object to add new key/values
// pairs---
Bundle extras = new Bundle();
extras.putString("str2", "This is another string");
extras.putInt("age2", 35);
//---attach the Bundle object to the Intent object---
i.putExtras(extras);
//---start the activity to get a result back---
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode,
int resultCode, Intent data)
{
//---check if the request code is 1---
if (requestCode == 1) {
//---if the result is OK---
if (resultCode == RESULT_OK) {
//---get the result using getIntExtra()---
Toast.makeText(this, Integer.toString(
data.getIntExtra("age3", 0)),
Toast.LENGTH_SHORT).show();
//---get the result using getData()---
Toast.makeText(this, data.getData().toString(),
Toast.LENGTH_SHORT).show();
}
}
}
}
7.按F11调试。
效果图:
程序第一次启动:
跳转到SecondActivity:
返回PassDataActivity:
作者 manoel的专栏