读书人

两个Activity经过Intent传递数据

发布时间: 2012-07-04 19:33:55 作者: rapoo

两个Activity通过Intent传递数据
http://blog.csdn.net/hongshan50/article/details/6585396
这里有一份很好的文章:
[url]http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html
[/url]
1.无参数Activity跳转

Intent it = new Intent(Activity.Main.this, Activity2.class);startActivity(it);  


2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)
Intent it = new Intent(Activity.Main.this, Activity2.class);
Bundle bundle=new Bundle();bundle.putString("name", "This is from MainActivity!");it.putExtras(bundle);       // it.putExtra(“test”, "shuju”);startActivity(it);            // startActivityForResult(it,REQUEST_CODE);


对于数据的获取可以采用:
Bundle bundle=getIntent().getExtras();String name=bundle.getString("name");


3.向上一个Activity返回结果(使用setResult,针对
startActivityForResult(it,REQUEST_CODE)启动的Activity)        Intent intent=getIntent();        Bundle bundle2=new Bundle();        bundle2.putString("name", "This is from ShowMsg!");        intent.putExtras(bundle2);        setResult(RESULT_OK, intent);

4.回调上一个Activity的结果处理函数(onActivityResult)
@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        if (requestCode==REQUEST_CODE){            if(resultCode==RESULT_CANCELED)                  setTitle("cancle");            else if (resultCode==RESULT_OK) {                 String temp=null;                 Bundle bundle=data.getExtras();                 if(bundle!=null)   temp=bundle.getString("name");                 setTitle(temp);            }        }    }





我的例子源码:
main.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/factorone"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/symbol"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/symbol" />    <EditText        android:id="@+id/factortwo"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/calculate"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/calculate" /></LinearLayout>



result.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/totalRsult"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/symbol" /></LinearLayout>



strings.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, Android_TwoActivityActivity!</string>    <string name="app_name">Android_TwoActivity</string>    <string name="firstActivity">第一个Activity</string>    <string name="secondActivity">第二个Activity</string>    <string name="symbol">乘以</string>    <string name="calculate">计算</string></resources>


FirstActivity.java
----------------------------------------------
package com.pandy.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;import android.widget.EditText;import android.widget.TextView;public class FirstActivity extends Activity {private EditText factorOne;private EditText factorTwo;private TextView symbol;private Button calculate;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);factorOne = (EditText) findViewById(R.id.factorone);factorTwo = (EditText) findViewById(R.id.factortwo);symbol = (TextView) findViewById(R.id.symbol);calculate = (Button) findViewById(R.id.calculate);calculate.setOnClickListener(new MyListener());}class MyListener implements OnClickListener {@Overridepublic void onClick(View arg0) {Intent intent = new Intent();intent.setClass(FirstActivity.this, SecondActivity.class);intent.putExtra("factorOneStr", factorOne.getText().toString());intent.putExtra("factorTwoStr", factorTwo.getText().toString());startActivity(intent);}}}



SecondActivity.java
----------------------------------------------
package com.pandy.test;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class SecondActivity extends Activity {private TextView resultView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.result);resultView = (TextView) findViewById(R.id.totalRsult);Intent intent = getIntent();String factorOneStr = intent.getStringExtra("factorOneStr");String factorTwoStr = intent.getStringExtra("factorTwoStr");int factorOneInt = Integer.parseInt(factorOneStr);int factorTwoInt = Integer.parseInt(factorTwoStr);resultView.setText(factorOneInt * factorTwoInt + "");}}


R.java
----------------------------------------------
/* AUTO-GENERATED FILE.  DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found.  It * should not be modified by hand. */package com.pandy.test;public final class R {    public static final class attr {    }    public static final class drawable {        public static final int ic_launcher=0x7f020000;    }    public static final class id {        public static final int calculate=0x7f050003;        public static final int factorone=0x7f050000;        public static final int factortwo=0x7f050002;        public static final int symbol=0x7f050001;        public static final int totalRsult=0x7f050004;    }    public static final class layout {        public static final int main=0x7f030000;        public static final int result=0x7f030001;    }    public static final class string {        public static final int app_name=0x7f040001;        public static final int calculate=0x7f040005;        public static final int firstActivity=0x7f040002;        public static final int hello=0x7f040000;        public static final int secondActivity=0x7f040003;        public static final int symbol=0x7f040004;    }}




AndroidManifest.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.pandy.test"    android:versionCode="1"    android:versionName="1.0" >    <application        android:label="@string/app_name" >        <activity            android:name=".FirstActivity"            android:label="@string/firstActivity" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".SecondActivity"            android:label="@string/secondActivity" />    </application>    <uses-sdk android:minSdkVersion="4" /></manifest>

读书人网 >移动开发

热点推荐