startActivityForResult方法解决Activity之间数据的保存问题

res/layout/ex03_10.xml

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <RadioGroup android:layout_height="wrap_content" 

    			android:layout_width="wrap_content" 

    			android:id="@+id/rgSex" 

    			android:orientation="horizontal" 

    			android:layout_x="100dip" 

    			android:layout_y="60dip">

        <RadioButton android:text="@string/sex_man" 

        		     android:layout_height="wrap_content" 

        		     android:layout_width="wrap_content" 

        		     android:checked="true" 

        		     android:id="@+id/radio_man"></RadioButton>

        <RadioButton android:text="@string/sex_woman" 

        			 android:layout_height="wrap_content" 

        			 android:layout_width="wrap_content" 

        			 android:id="@+id/radio_woman"></RadioButton>

    </RadioGroup>

    <EditText android:layout_height="wrap_content" 

    		  android:layout_width="wrap_content" 

    		  android:text="EditText" 

    		  android:id="@+id/etWeight" 

    		  android:layout_x="97dip" 

    		  android:layout_y="133dip"></EditText>

    <Button android:text="Button" 

    		android:layout_height="wrap_content" 

    		android:layout_width="wrap_content" 

    		android:id="@+id/btn" 

    		android:layout_x="120dip" 

    		android:layout_y="212dip"></Button>

</AbsoluteLayout>
res/layout/ex03_10_01.xml
<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

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

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

    <TextView android:layout_height="wrap_content" 

    	      android:layout_width="wrap_content" 

    	      android:text="TextView" 

    	      android:layout_x="126dip" 

    	      android:layout_y="144dip" 

    	      android:id="@+id/tvResult"></TextView>

    <Button android:layout_width="wrap_content" 

    		android:layout_x="125dip" 

    		android:layout_height="wrap_content" 

    		android:layout_y="248dip" 

    		android:id="@+id/btnBk" 

    		android:text="@string/back"></Button>

</AbsoluteLayout>
src/EX03_10.java
package gphone.ex03_10;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.RadioGroup;

public class EX03_10 extends Activity {

	Button btn=null;

	EditText etWeight=null;

	RadioGroup rgSex=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.ex03_10);

        btn=(Button)findViewById(R.id.btn);

        etWeight=(EditText)findViewById(R.id.etWeight);

        rgSex=(RadioGroup)findViewById(R.id.rgSex);

        btn.setOnClickListener(new Button.OnClickListener(){



			@Override

			public void onClick(View v) {

				// TODO Auto-generated method stub

				String strWeight=etWeight.getText().toString();

				

				String strSex="女";

				if(rgSex.getCheckedRadioButtonId()==R.id.radio_man)

				{

					strSex="男";

				}

				else

				{

					strSex="女 ";

				}

				Bundle b=new Bundle();

				b.putString("Weight", strWeight);

				b.putString("Sex", strSex);

				Intent intent=new Intent();

				intent.putExtras(b);

				intent.setClass(EX03_10.this,EX03_10_01.class);

				startActivityForResult(inte

src/EX03_10_02.java

package gphone.ex03_10;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;



public class EX03_10_01 extends Activity{

	TextView tvResult=null;

	Button btnBk=null;

	Intent intent=null;

	@Override

	protected void onCreate(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);

		setContentView(R.layout.ex03_10_01);

		tvResult=(TextView)findViewById(R.id.tvResult);

		intent=this.getIntent();

		Bundle b=intent.getExtras();

		String strWeight=b.getString("Weight");

		String strSex=b.getString("Sex");

		tvResult.setText("结果为:"+strWeight+","+strSex);

		

		btnBk=(Button)findViewById(R.id.btnBk);

		btnBk.setOnClickListener(new Button.OnClickListener(){



			@Override

			public void onClick(View v) {

				// TODO Auto-generated method stub

				EX03_10_01.this.setResult(RESULT_OK, intent);

				EX03_10_01.this.finish();

			}

			

		});

		

	}



}

运行结果

魔乐截屏 魔乐截屏(1) 魔乐截屏(2)

你可能感兴趣的:(startActivityForResult方法解决Activity之间数据的保存问题)