1 FActivity.java
package com.wmjintent.demo10;
import java.net.ContentHandler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FActivity extends Activity{
private Button bt1;
private Button bt2;
private Context mContext ;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
//此方法作用,将factivity文件布局在JAVA文件中,方便进行运行
mContext = this ;
/* 使mContext 指向该环境下的对象this
* 通过点击bt1实现页面之间的跳转
* 1 startActivity的方式来实现
* 初始化Intent
* 初始化tv .
* */
tv=(TextView) findViewById(R.id.textView1);
bt1=(Button)findViewById(R.id.button1);
bt2=(Button)findViewById(R.id.button2);
//注册点击事件,在点击事件里面实现页面跳转的意图,利用Intent方法
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*
* 第一个参数packageContext代表上下文对象,如果直接用this,在匿名内部类里面是无法访问到FActivity的,
* 方法1 FActivity.this
* 方法2 建立全局变量mContext
* 第二个参数 目标文件
*/
Intent intent = new Intent(mContext,Secondactivity.class);
startActivity(intent);
// 通过第一种无返回值的Ietent 方法来实现页面之间的跳转
}
});
/* 通过startActivityForResult方法,该方法需要关联两个方法
* 一个是onActivityResult方法 ,一个是setResult方法
*
*/
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,Secondactivity.class);
/* 第一个参数是Intent对象
* 第二个参数是请求一个标识
*
*/
startActivityForResult(intent, 1);
}
});
}
/*
* 1 通过startActivityForResult跳转,接受返回的数据的方法,当前页面通过下面方法接受第二个页面返回的参数
* 2 requestcode 请求的标识,当该页面有多个button的时候,可以通过该标识通过startActivityForResult(intent, 1);方法
* 的标识requestcode接受不同的返回数据
* 3 resultcode :代表第二个页面返回的标识,可以标识是哪个页面返回的。因为通都是过 onActivityResult方法返回的数据
* 通过resultcode可以知道是哪个页面返回的数据。
* 4 第二个页面回传的数据,放在data当中。
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(1, 2, data);
if(requestCode == 1&& resultCode == 2){
String content = data.getStringExtra("data");
tv.setText(content);
}
}
}
2 factivity.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/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/firststart" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/secondstart" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview" />
</LinearLayout>
3 SecondActivity.java
package com.wmjintent.demo10;
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 Secondactivity extends Activity{
private Button bt;
private String content = "你好";
//通过点击第二个页面的Button,才能给第一个页面回传数据,所以在此初始化按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondfactivity);
/*
* 第二个页面什么时候给第一个页面回传数据
* 回传到第一个页面的实际上是一个Intent对象
*/
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent();
data.putExtra("data",content);
setResult(2, data);
finish();
//结束当前页面,返回第一个页面
}
});
}
4 secondactivity.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/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>