main.xml如下:
[html] <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/mainActivity_tip"
android:textSize="25sp"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/button_tip"
android:textSize="25sp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/mainActivity_tip"
android:textSize="25sp"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/button_tip"
android:textSize="25sp"/>
</RelativeLayout>another.xml如下:
[html] <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="200dip"
android:layout_height="50dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:gravity="center"
android:text="@string/anotherActivity_tip"
/>
<Button
android:id="@+id/closeButton"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:layout_marginTop="50dip"
android:gravity="center"
android:text="@string/closeActivity"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="200dip"
android:layout_height="50dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:gravity="center"
android:text="@string/anotherActivity_tip"
/>
<Button
android:id="@+id/closeButton"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:layout_marginTop="50dip"
android:gravity="center"
android:text="@string/closeActivity"
/>
</RelativeLayout>MainActivity如下:
[java] package cn.com.bravesoft.testactivity3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 利用startActivityForResult跳转到其余Activity
* 且当第二个Activity关闭时将数据返回给第一个Activity
*/
public class MainActivity extends Activity {
private Button mButton;
private int requestCode=9527;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new ButtonOnClickListener());
}
private class ButtonOnClickListener implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), AnotherActivity.class);
startActivityForResult(intent, requestCode);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("调用onActivityResult方法 requestCode="+requestCode+",resultCode="+resultCode);
String name = data.getStringExtra("name");
int age = data.getIntExtra("age", 0);
System.out.println("回传过来的参数:name="+name+",age="+age);
}
}
package cn.com.bravesoft.testactivity3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 利用startActivityForResult跳转到其余Activity
* 且当第二个Activity关闭时将数据返回给第一个Activity
*/
public class MainActivity extends Activity {
private Button mButton;
private int requestCode=9527;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new ButtonOnClickListener());
}
private class ButtonOnClickListener implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), AnotherActivity.class);
startActivityForResult(intent, requestCode);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("调用onActivityResult方法 requestCode="+requestCode+",resultCode="+resultCode);
String name = data.getStringExtra("name");
int age = data.getIntExtra("age", 0);
System.out.println("回传过来的参数:name="+name+",age="+age);
}
}
AnotherActivity如下:
[java] package cn.com.bravesoft.testactivity3;
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 AnotherActivity extends Activity {
private Button mButton;
private int resultCode=23;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
mButton=(Button) findViewById(R.id.closeButton);
mButton.setOnClickListener(new ButtonOnClickListenerImpl());
}
private class ButtonOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("name", "小明");
intent.putExtra("age", 12);
setResult(resultCode, intent);
finish();
}
}
}
package cn.com.bravesoft.testactivity3;
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 AnotherActivity extends Activity {
private Button mButton;
private int resultCode=23;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
mButton=(Button) findViewById(R.id.closeButton);
mButton.setOnClickListener(new ButtonOnClickListenerImpl());
}
private class ButtonOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("name", "小明");
intent.putExtra("age", 12);
setResult(resultCode, intent);
finish();
}
}
}