Activty之间的传递数据
之前学习过如何启动一个activity,在一些场合不单需要启动另一个activity,还需要得到返回的结果.而且有些时候更经常的操作时启动另一个activity时带上一些值.
1.定义一个Intent,并为该Intent指定即将被启动的Activity
2.调用startActivityForResult(Intent intent,int requestCode)跳转并标记requestCode
3.重写onActivityResult(int requestCode,int resultCode,intent data)解析返回的数据
4.在另一个Activity中调用setResult(RESULT_OK,backintent)
Note:在Activity之间传递数据
Bundle:一个类型安全的容器,只能存储基本数据类型或基本类型的数组,如string,int,byte和boolean等
代码:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:text="下一步" android:id="@+id/Next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > </Button> </RelativeLayout>
MainActivity.java:
package com.YZR.activtydatapass; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { final int NEXT=0x1010; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=(Button)findViewById(R.id.Next); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent nextIntent=new Intent(MainActivity.this,NextActivity.class); Bundle bundle=new Bundle(); bundle.putString("性别", "MAN"); bundle.putString("姓名", "YZR"); nextIntent.putExtras(bundle); startActivityForResult(nextIntent, NEXT); } }); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data) { if(resultCode==RESULT_OK && requestCode== NEXT) { Bundle bundle=data.getExtras(); int age=bundle.getInt("年龄"); String work=bundle.getString("职业"); setTitle("Age:"+age+",Work:"+work); } super.onActivityResult(requestCode, resultCode, data); } }
activity_next.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:text="返回" android:id="@+id/Back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > </Button> </RelativeLayout>
NextActivity.java:
package com.YZR.activtydatapass; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class NextActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); Bundle bundle=getIntent().getExtras(); String name=bundle.getString("姓名"); String Sex=bundle.getString("性别"); setTitle("姓名:"+name+",性别:"+Sex); Button btn=(Button)findViewById(R.id.Back); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent backIntent=new Intent(); Bundle bundle=new Bundle(); bundle.putInt("年龄", 25); bundle.putString("职业", "IT"); backIntent.putExtras(bundle); setResult(RESULT_OK,backIntent); finish(); } }); } }
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.YZR.activtydatapass" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NextActivity" android:label="@string/title_activity_next" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
以上仅供于学习<Android系统应用开发实战详解>
END
时间: 2024-10-10 07:26:38