你的位置:首页 > 操作系统

[操作系统]两个Activity之间的交互startActivityForResult的使用


 

代码如下:

 1 package com.zzw.teststartintentforrequest; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.TextView;10 11 public class MainActivity extends Activity {12 13   private final int REQUESTCODE_MAIN = 1;14 15   private final String KEY_MAIN = "key_main";16   private final String KEY_B = "key_b";17 18   private TextView textView;19 20   @Override21   protected void onCreate(Bundle savedInstanceState) {22     super.onCreate(savedInstanceState);23     setContentView(R.layout.activity_main);24 25     textView = (TextView) findViewById(R.id.textView_main);26 27     findViewById(R.id.button_main).setOnClickListener(new OnClickListener() {28 29       @Override30       public void onClick(View v) {31         Intent intent = new Intent(MainActivity.this, ActivityB.class);32         int a=(int) (Math.random()*100);33         int b=(int) (Math.random()*100);34         int[] nums = { a, b };35         intent.putExtra(KEY_MAIN, nums);36         startActivityForResult(intent, REQUESTCODE_MAIN);37       }38     });39   }40 41   @Override42   protected void onActivityResult(int requestCode, int resultCode, Intent data) {43     if(data!=null){44       Log.d("-------", "requestCode:"+requestCode+"  resultCode:"+resultCode);45       textView.setText(data.getIntExtra(KEY_B, 0)+"");46     }47   }48 }

MainActivity.java
 1 package com.zzw.teststartintentforrequest; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.TextView; 9 10 public class ActivityB extends Activity {11   12   private final int RESULTCODE_B=2;13   14   private final String KEY_MAIN="key_main";15   private final String KEY_B="key_b";16   17   private TextView textView;18   19   20   @Override21   protected void onCreate(Bundle savedInstanceState) {22     super.onCreate(savedInstanceState);23     setContentView(R.layout.activityb);24     25     textView=(TextView) findViewById(R.id.textView_B);26     27     int nums[]=this.getIntent().getIntArrayExtra(KEY_MAIN);28     29     final int sum=nums[0]+nums[1];30     31     textView.setText(nums[0]+"+"+nums[1]+"="+sum);32     33     findViewById(R.id.button_B).setOnClickListener(new OnClickListener() {34       35       @Override36       public void onClick(View v) {37         Intent intent=new Intent(ActivityB.this,MainActivity.class);38         intent.putExtra(KEY_B,sum);39         ActivityB.this.setResult(RESULTCODE_B, intent);40         ActivityB.this.finish();41       }42     });43   }44   45   46 }

ActivityB.java
 1 <RelativeLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:background="@android:color/holo_blue_light" 6   tools:context="com.zzw.teststartintentforrequest.MainActivity" > 7  8   <TextView 9     android:id="@+id/textView_main"10     android:layout_width="wrap_content"11     android:layout_height="wrap_content"12     android:layout_centerInParent="true"13     android:text="0"14     android:textColor="@android:color/holo_red_light"15     android:textSize="30sp" />16 17   <Button18     android:id="@+id/button_main"19     android:layout_width="wrap_content"20     android:layout_height="wrap_content"21     android:layout_alignParentBottom="true"22     android:layout_centerHorizontal="true"23     android:layout_marginBottom="38dp"24     android:background="@android:color/holo_blue_light"25     android:text="点击跳转AcyivityB"26     android:textColor="@android:color/holo_red_light" />27 28 </RelativeLayout>

activity_main.
 1 <RelativeLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:background="@android:color/black" 6   tools:context="com.zzw.teststartintentforrequest.MainActivity" > 7  8   <TextView 9     android:id="@+id/textView_B"10     android:layout_width="wrap_content"11     android:layout_height="wrap_content"12     android:layout_centerInParent="true"13     android:textColor="@android:color/white"14     android:textSize="30sp" />15 16   <Button17     android:id="@+id/button_B"18     android:layout_width="wrap_content"19     android:layout_height="wrap_content"20     android:textColor="@android:color/white"21     android:background="@android:color/black"22     android:layout_alignParentBottom="true"23     android:layout_centerHorizontal="true"24     android:layout_marginBottom="38dp"25     android:text="点击返回MainAcyivity" />26 27 </RelativeLayout>

activityb.