歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發之Intent傳值實例

Android開發之Intent傳值實例

日期:2017/3/1 10:45:15   编辑:Linux編程

今天我們來探討一下Android的傳值問題。

主要實現功能為第一個頁面實現信息的填寫,在第二個頁面實現第一個頁面信息的輸出

效果圖為:

第一個activity實現了對單選、復選、文本框值的獲取與傳遞

  1. ////////////////////UIZuoYeActivity///////////////
  2. //第一個activity
  3. package cn.class3g.activity;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.RadioButton;
  13. import android.widget.RadioGroup;
  14. import android.widget.Spinner;
  15. public class UIZuoYeActivity extends Activity implements OnClickListener {
  16. /** Called when the activity is first created. */
  17. RadioGroup rg = null;
  18. RadioButton manRB = null;
  19. RadioButton rb = null;
  20. Button btn = null;
  21. EditText nameET = null;
  22. CheckBox lan, zu, pai, ping;
  23. Spinner city;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28. findView();
  29. }
  30. private void findView() {
  31. btn = (Button) this.findViewById(R.id.putinId);
  32. nameET = (EditText) this.findViewById(R.id.nameId);
  33. manRB = (RadioButton) this.findViewById(R.id.manId);
  34. lan = (CheckBox) this.findViewById(R.id.lanId);
  35. zu = (CheckBox) this.findViewById(R.id.zuId);
  36. pai = (CheckBox) this.findViewById(R.id.paiId);
  37. ping = (CheckBox) this.findViewById(R.id.pingId);
  38. city = (Spinner) this.findViewById(R.id.cityId);
  39. btn.setOnClickListener(this);
  40. }
  41. @Override
  42. public void onClick(View v) {
  43. // TODO Auto-generated method stub
  44. // 封裝bundle對象
  45. Bundle bundle = new Bundle();
  46. // 獲取EditText文本框內容
  47. bundle.putString("name", "用戶名稱:" + nameET.getText().toString());
  48. // 獲取RadioGroup單選內容
  49. if (manRB.isChecked()) {
  50. bundle.putString("sex", "性別:男");
  51. } else {
  52. bundle.putString("sex", "性別:女");
  53. }
  54. // 獲取CheckBox復選框內容
  55. String temp = "愛好:";
  56. if (lan.isChecked()) {
  57. temp += lan.getText().toString();
  58. }
  59. if (zu.isChecked()) {
  60. temp += "";
  61. temp += zu.getText().toString();
  62. }
  63. if (pai.isChecked()) {
  64. temp += "";
  65. temp += pai.getText().toString();
  66. }
  67. if (ping.isChecked()) {
  68. temp += "";
  69. temp += ping.getText().toString();
  70. }
  71. bundle.putString("hobby", temp);
  72. // 獲取Spinner下拉菜單內容
  73. bundle.putString("city", "城市:" + city.getSelectedItem().toString());
  74. Intent intent = new Intent(UIZuoYeActivity.this, PutInActivity.class);
  75. // 傳遞
  76. intent.putExtras(bundle);
  77. startActivity(intent);
  78. }
  79. }
Copyright © Linux教程網 All Rights Reserved