TShopping

 找回密碼
 註冊
搜索
查看: 1509|回復: 0

[教學] [Android智慧型手機設計_由其他活動回傳資料以Intent6_3為例]

[複製鏈接]
發表於 2013-6-10 20:15:16 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
1.jpg

/*

1.想要由被啟動的Activity取得回傳資料,那麼啟用端必須改用startActivityForResult ()方法來啟動Activity

2.被啟動的Activity端要回傳資料時,亦是使用putExtra ()方法設定name-value pair,之後再使用setResult()方法回傳資料

3.啟用端Activity必須實作onActivityResult ()方法來處理被啟動之A
ctivity所回傳的資料
*/
1.來源_First.java
/*
//1.建立Intent物件
Intent myIntent = new Intent();
//2.藉由setClass()方法設定來源與目的地類別
myIntent.setClass(First.this, Second.class);
//3.建立Bundle物件,利用putString(鍵值,資料)方法新增字串資料
Bundle bundle = new Bundle();
bundle.putString("HEIGHT", height.getText().toString());
bundle.putString("WEIGHT“, weight.getText().toString());
//4.使用Intenet物件的puExtras()方法,附加Bundle物件
myIntent.putExtras(bundle);
//6.啟動活動
startActivity(myIntent);
*/


  1. package com.example.inten6_3;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;

  11. public class First extends Activity {
  12. EditText et1,et2,et3;
  13. Button btn1;
  14. static final int SET_SECOND=1;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.first);
  19. et1=(EditText)findViewById(R.id.editText1);
  20. et2=(EditText)findViewById(R.id.editText2);
  21. et3=(EditText)findViewById(R.id.editText3);
  22. btn1=(Button)findViewById(R.id.button1);
  23. btn1.setOnClickListener(new OnClickListener(){

  24. @Override
  25. public void onClick(View v) {
  26. Intent i=new Intent();
  27. i.setClass(First.this, Second.class);
  28. Bundle b1=new Bundle();
  29. b1.putString("1", et1.getText().toString());
  30. b1.putString("2", et2.getText().toString());
  31. b1.putString("3", et3.getText().toString());
  32. i.putExtras(b1);
  33. startActivityForResult(i,SET_SECOND);
  34. }});
  35. }
  36. protected void onActivityResult (int requestCode, int resultCode, Intent data){
  37. super.onActivityResult(requestCode, resultCode, data);
  38. switch(requestCode)
  39. {
  40. case SET_SECOND:
  41. if(resultCode==RESULT_OK){
  42. Bundle b=data.getExtras();
  43. String s=b.getString("sum");
  44. TextView op=(TextView)findViewById(R.id.op);
  45. op.setText(s);
  46. }
  47. }
  48. }
  49. }
複製代碼




2.目的地_Second.java
/*在目標活動可以呼叫Activity物件的getIntent()方法取得Intent物件,
然後呼叫Intent物件的getExtras()方法取得攜帶的Bundle物件*/
package com.example.inten6_3;

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.TextView;

public class Second extends Activity {
Button btn2;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
double n1,n2,n3,sum,avg;
Bundle b2=this.getIntent().getExtras();
if(b2!=null){
n1=Double.parseDouble(b2.getString("1"));
n2=Double.parseDouble(b2.getString("2"));
n3=Double.parseDouble(b2.getString("3"));
sum=n1+n2+n3;
avg=sum/3;
TextView output=(TextView)findViewById(R.id.output);
output.setText(getString(R.string.s1)+n1+getString(R.string.s2)+n2+getString(R.string.s3)+n3
+"\n"+getString(R.string.s4)+sum+getString(R.string.s5)+avg);
result=output.getText().toString();
}
btn2=(Button)findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent reply=new Intent();
Bundle c=new Bundle();
c.putString("sum",result);
reply.putExtras(c);
setResult(RESULT_OK, reply);
finish();
}});
}
}
IntentBundle_return.zip (1.32 MB, 下載次數: 996)

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-18 12:56 , Processed in 0.083529 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表