Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 兩個Activity之間的交互startActivityForResult的使用,startactivityresult

兩個Activity之間的交互startActivityForResult的使用,startactivityresult

編輯:關於android開發

兩個Activity之間的交互startActivityForResult的使用,startactivityresult


 

代碼如下:

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 @Override 21 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 @Override 30 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 @Override 42 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 @Override 21 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 @Override 36 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 xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="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 <Button 18 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.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="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 <Button 17 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.xml

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved