TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

[Android智慧型手機設計_傳遞資料給其他活動以Intent6_2為例]

2013-7-10 19:57| 發佈者: woff| 查看: 1118| 評論: 0|原作者: woff

摘要: 完成圖 1.來源_First.java /* //1.建立Intent物件 Intent myIntent = new Intent(); //2.藉由setClass()方法設定來源與目的地類別 myIntent.setClass(First.this, Second.class); //3.建立Bundle物件,利用putStr ...
 

完成圖


1.jpg



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

  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. public class First extends Activity {
  11. EditText et1,et2,et3;
  12. Button btn1;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.first);
  17. et1=(EditText)findViewById(R.id.editText1);
  18. et2=(EditText)findViewById(R.id.editText2);
  19. et3=(EditText)findViewById(R.id.editText3);
  20. btn1=(Button)findViewById(R.id.button1);
  21. btn1.setOnClickListener(new OnClickListener(){
  22. @Override
  23. public void onClick(View v) {
  24. Intent i=new Intent();
  25. i.setClass(First.this, Second.class);
  26. Bundle b1=new Bundle();
  27. b1.putString("1", et1.getText().toString());
  28. b1.putString("2", et2.getText().toString());
  29. b1.putString("3", et3.getText().toString());
  30. i.putExtras(b1);
  31. startActivity(i);
  32. }});
  33. }
  34. }
復制代碼



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

  1. package com.example.intent6_2;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;

  5. public class Second extends Activity {

  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.second);
  9. double n1,n2,n3,sum,avg;
  10. Bundle b2=this.getIntent().getExtras();
  11. if(b2!=null){
  12. n1=Double.parseDouble(b2.getString("1"));
  13. n2=Double.parseDouble(b2.getString("2"));
  14. n3=Double.parseDouble(b2.getString("3"));
  15. sum=n1+n2+n3;
  16. avg=sum/3;
  17. TextView output=(TextView)findViewById(R.id.output);
  18. output.setText(getString(R.string.s1)+n1+getString(R.string.s2)+n2+getString(R.string.s3)+n3
  19. +"\n"+getString(R.string.s4)+sum+getString(R.string.s5)+avg);
  20. }
  21. }
  22. }
復制代碼

最新評論



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

GMT+8, 2024-7-27 08:44 , Processed in 0.047867 second(s), 21 queries .

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

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部