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.example.testactivityresquest;
 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.Button;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends Activity {
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17 
18         Button btn = (Button) findViewById(R.id.button1);
19         btn.setOnClickListener(new OnClickListener() {
20 
21             @Override
22             public void onClick(View v) {
23 
24                 Intent intent = new Intent(MainActivity.this, Activityb.class);
25                 int[] nums = { 15, 15 };
26                 intent.putExtra(Changliang.KEY, nums);
27                 //有別於startActivity,如果啟動的其他Activity多了以後。相當於定一個特定KEY值,返回根據KEY值返回。
28                 startActivityForResult(intent, Changliang.requestCode);
29 
30             }
31         });
32     }
33    //Activityb傳回來的數據在這個方法中獲取
34     @Override
35     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
36 
37         int s = data.getIntExtra(Changliang.Activity_b_KEY, 1);
38 
39         Toast.makeText(getApplicationContext(), "傳遞兩個數得到的和是:" + s, 1).show();
40 
41     }
42 
43 }
 1 package com.example.testactivityresquest;
 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.Button;
 9 import android.widget.Toast;
10 
11 public class Activityb extends Activity {
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activityb);
17 
18         Intent intent = this.getIntent();
19         int[] n = intent.getIntArrayExtra(Changliang.KEY);
20 
21         final int nums = n[0] + n[1];
22 
23         Toast.makeText(this, n[0] + " " + n[1], 1).show();
24 
25         Button btn = (Button) findViewById(R.id.button2);
26         btn.setOnClickListener(new OnClickListener() {
27 
28             @Override
29             public void onClick(View v) {
30 
31                 Intent intent1 = new Intent(Activityb.this, MainActivity.class);
32 
33                 intent1.putExtra(Changliang.Activity_b_KEY, nums);
34                 // 將數據根據特定鍵值的意圖事件導入
35                 Activityb.this.setResult(Changliang.requestCode, intent1);
36                 //關閉後返回主Activity
37                 Activityb.this.finish();
38 
39             }
40         });
41     }
42 
43 }
package com.example.testactivityresquest;

public class Changliang {
    
public static final String KEY="key";

public static final String Activity_b_KEY="key1";

public static final int requestCode=1987;


}

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="#ff0000" >
 6 
 7     <Button
 8         android:id="@+id/button1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_alignParentTop="true"
12         android:layout_centerHorizontal="true"
13         android:layout_marginTop="88dp"
14         android:text="啟動Activityb" />
15 
16 </RelativeLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#00ff00"
 6     android:orientation="vertical" >
 7 
 8     <Button
 9         android:id="@+id/button2"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="返回Activity" />
13 
14 </LinearLayout>

別忘在AndroidManifast中注冊activityb。

運行效果圖:

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