FActivity.java
package com.example.demo06;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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);
mContext=this;
bt1=(Button) findViewById(R.id.button1_first);
bt2=(Button) findViewById(R.id.button2_second);
tv=(TextView) findViewById(R.id.textView1);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//第一个参数,上下文对下this
//第二个参数,目标文件
Intent intent=new Intent(mContext,SActivity.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(mContext,SActivity.class);
startActivityForResult(intent,1);
}
});
}
/*
* 通过startActivityForresult跳转,接受返回数据的方法
* requestCode:请求的标识
* resultCode:第二个页面返回的标识
* data:第二个页面回传的数据
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&resultCode==2){
String content=data.getStringExtra("data");
tv.setText(content);
}
}
}
SActivity.java
package com.example.demo06;
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 SActivity extends Activity{
private Button bt;
private String content="你好";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent data=new Intent();
data.putExtra("data",content);
setResult(2, data);
finish();
}
});
}
}