Activity间的数据传递

一.res/layout文件夹中添加两个布局文件

1.receive_result.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent" android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/receive_result_instructions"/>

<!--用于接收另外一个Activity的返回结果-->

<TextView android:id="@+id/results"

android:layout_width="match_parent" android:layout_height="10dip"

android:layout_weight="1"

android:paddingBottom="4dip"

android:background="@drawable/green">

</TextView>

<!--通过按钮的点击事件,启动另外一个Activit -->

<Button android:id="@+id/get"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:layout_weight="0"

android:text="@string/receive_result_result">

<requestFocus />

</Button>

</LinearLayout>

2. send_result.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent" android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="8dip"

android:text="@string/pick_result"/>

<!-- 点击按钮,退出当前Activity,并返回按钮的文本(Corky!) -->

<Button android:id="@+id/corky"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/corky">

<requestFocus />

</Button>

<!-- 点击按钮,退出当前Activity,并返回按钮的文本(Violet!) -->

<Button android:id="@+id/violet"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/violet">

</Button>

</LinearLayout>

二.res/values文件夹中定义字符和颜色资源

1.strings.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

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

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

<string name="activity_receive_result">App/Activity/Receive Result</string>

<string name="pick_result">Pick a result to send, or BACK to cancel.</string>

<string name="corky">Corky</string>

<string name="violet">Violet</string>

<string name="receive_result_instructions">Press the button to get an activity result, which will be displayed here:</string>

<string name="receive_result_result">Get Result</string>

</resources>

2.colors.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<drawable name="green">#7700ff00</drawable>

</resources>

三.定义项目清单文件(AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

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

package="my.android.test"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<!-- 应用启动的主界面 -->

<activity android:name=".ReceiveResult"

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 -->

<activity android:name=".SendResult">

</activity>

</application>

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

</manifest>

四.定义Activity的类

1. ReceiveResult.java

package my.android.test;

import android.app.Activity;

import android.content.Intent;

import android.text.Editable;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class ReceiveResult extends Activity {

//显示返回结果的TextView

private TextView mResults;

//定义用户接收返回结果的请求编码,

staticfinalprivateintGET_CODE = 0;

/**

* get按钮监听器,用户点击该按钮时,会启动返回结果的Activity

*/

private OnClickListener mGetListener = new OnClickListener(){

publicvoid onClick(View v){

//启动能够返回结果的Activity,返回结果中会带有请求编码GET_CODE.

Intent intent = new Intent(ReceiveResult.this, SendResult.class);

//startActivityForResult()方法要与返回结果的ActivitysetResult()方法配合使用

//startActivityForResult()启动Activity,当被启动的Activity调用finish()方法退出

//时,系统会调用onActivityResult()回调方法。

startActivityForResult(intent, GET_CODE);

}

};

/**

* 当返回结果的Activity调用finish()方法退出时,这个回调方法会被调用,并带有返回结果。

* @param requestCode 调用startActivityForResult()方法时传入的请求编码;

* @param resultCode 返回结果的Activity退出时,调用setResult()方法的设置结果编码;

* @param data 返回结果的Activity退出时,调用setResult()方法设置返回结果

* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)

*/

@Override

protectedvoid onActivityResult(int requestCode, int resultCode, Intent data){

//使用requestCode来区分是由那个Activity返回的结果,本例中只有一个Activity

if(requestCode == GET_CODE){

//获取准备显示返回结果的文本对象。

Editable text = (Editable)mResults.getText();

//使用resultCode来区分返回结果的Activity中的操作结果,操作结果在setResult方法的第一个参数中设定。

if(resultCode == RESULT_CANCELED){

text.append("(cancelled)");

}else{

//Activity返回的数据被放在data参数中,从data中获取设定的返回数据。

text.append("(okay");

text.append(Integer.toString(resultCode));

text.append(")");

if(data != null){

text.append(data.getAction());

}

}

text.append("\n");

}

}

/** Activity在首次创建时,调用这个方法*/

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//把布局填充到Activity

setContentView(R.layout.receive_result);

//从布局中获取显示返回结果的TextView对象。

mResults = (TextView)findViewById(R.id.results);

//TextView设置显示文本,并且显示文本是可编辑的。

mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);

//从布局中获取Button对象

Button getButton = (Button)findViewById(R.id.get);

//Button对象设计事件监听器

getButton.setOnClickListener(mGetListener);

}

}

2. SendResult.java

package my.android.test;

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;

publicclass SendResult extends Activity {

/**

* Activity被首次创建时,调用这个方法

*/

@Override

protectedvoid onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

//使用布局填充Activity

setContentView(R.layout.send_result);

//获取布局中的corky按钮对象,并该按钮设置监听器

Button button = (Button)findViewById(R.id.corky);

button.setOnClickListener(mCorkyListener);

//获取布局中的violet按钮对象,并该按钮设置监听器

button = (Button)findViewById(R.id.violet);

button.setOnClickListener(mVioletListener);

}

/**

*corky 按钮的监听器,用户点击该按钮时,设置返回结果,并退出本Activity

*/

private OnClickListener mCorkyListener = new OnClickListener(){

publicvoid onClick(View v){

//设置返回结果

setResult(RESULT_OK, (new Intent()).setAction("Corky!"));

//退出本Activity

finish();

}

};

/**

* violet按钮监听器,用户点击该按钮时,设置返回结果,并退出本Activity

*/

private OnClickListener mVioletListener = new OnClickListener(){

publicvoid onClick(View v){

//设置返回结果

setResult(RESULT_OK, (new Intent()).setAction("Violet!"));

//退出本Activity

finish();

}

};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值