大概过程

编写demo

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <TextView
  10. android:id="@+id/top"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:textSize="18sp"
  14. android:textColor="#ff0000"
  15. android:text="@string/intruduce"/>
  16. <EditText
  17. android:id="@+id/name"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:textSize="16sp"
  21. android:hint="请输入姓名"
  22. android:textColor="#000"
  23. android:layout_below="@+id/top"/>
  24. <EditText
  25. android:id="@+id/city"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:textSize="16sp"
  29. android:hint="请输入所在城市"
  30. android:textColor="#000"
  31. android:layout_below="@+id/name"/>
  32. <EditText
  33. android:id="@+id/sex"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:textSize="16sp"
  37. android:hint="请输入性别"
  38. android:textColor="#000"
  39. android:layout_below="@+id/city"/>
  40. <EditText
  41. android:id="@+id/phone"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:textSize="16sp"
  45. android:hint="请输入电话号码"
  46. android:textColor="#000"
  47. android:layout_below="@+id/sex"/>
  48. <EditText
  49. android:id="@+id/age"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:textSize="16sp"
  53. android:hint="请输入年龄"
  54. android:textColor="#000"
  55. android:layout_below="@+id/phone"/>
  56. <Button
  57. android:id="@+id/btn_submit"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_alignParentBottom="true"
  61. android:layout_alignParentEnd="true"
  62. android:layout_alignParentRight="true"
  63. android:layout_marginBottom="10dp"
  64. android:layout_marginEnd="10dp"
  65. android:layout_marginRight="10dp"
  66. android:background="#ff5000"
  67. android:text="提交"/>
  68. </RelativeLayout>

activity_result.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".ResultActivity">
  8. <TextView
  9. android:id="@+id/name"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:textSize="18sp"
  13. android:text="朱归强"
  14. android:textColor="#000" />
  15. <TextView
  16. android:id="@+id/sex"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:textSize="18sp"
  20. android:textColor="#000"
  21. android:text="男"
  22. android:layout_below="@+id/name"/>
  23. <LinearLayout
  24. android:id="@+id/agel"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:orientation="horizontal"
  28. android:layout_below="@+id/sex">
  29. <TextView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:textSize="18sp"
  33. android:textColor="#000"
  34. android:text="年龄:"/>
  35. <TextView
  36. android:id="@+id/age"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:textSize="18sp"
  40. android:text="23"
  41. android:textColor="#000"
  42. />
  43. </LinearLayout>
  44. <LinearLayout
  45. android:id="@+id/cityl"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:orientation="horizontal"
  49. android:layout_below="@+id/agel">
  50. <TextView
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:textSize="18sp"
  54. android:textColor="#000"
  55. android:text="所在城市:"/>
  56. <TextView
  57. android:id="@+id/city"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:textSize="18sp"
  61. android:text="长沙"
  62. android:textColor="#000"
  63. />
  64. </LinearLayout>
  65. <LinearLayout
  66. android:id="@+id/phonel"
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:orientation="horizontal"
  70. android:layout_below="@+id/cityl">
  71. <TextView
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:textSize="18sp"
  75. android:textColor="#000"
  76. android:text="电话:"/>
  77. <TextView
  78. android:id="@+id/phone"
  79. android:layout_width="wrap_content"
  80. android:layout_height="wrap_content"
  81. android:textSize="18sp"
  82. android:text="13155115510"
  83. android:textColor="#000"
  84. />
  85. </LinearLayout>
  86. <TextView
  87. android:id="@+id/result"
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:layout_alignParentBottom="true"
  91. android:layout_alignParentEnd="true"
  92. android:layout_alignParentRight="true"
  93. android:layout_marginBottom="10dp"
  94. android:layout_marginEnd="10dp"
  95. android:layout_marginRight="10dp"
  96. android:textSize="26sp"
  97. android:textColor="#F31D0B"
  98. android:text="今日幸运值:60%"/>
  99. </RelativeLayout>

MainActivity.java

  1. package com.example.bundledemo;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Button btnSubmit = (Button) findViewById(R.id.btn_submit);
  15. btnSubmit.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. String name = ((EditText)findViewById(R.id.name)).getText().toString();
  19. String sex = ((EditText) findViewById(R.id.sex)).getText().toString();
  20. String age = ((EditText) findViewById(R.id.age)).getText().toString();
  21. String city = ((EditText) findViewById(R.id.city)).getText().toString();
  22. String phone = ((EditText) findViewById(R.id.phone)).getText().toString();
  23. if(!"".equals(name)&&!"".equals(sex)&&!"".equals(age)&&!"".equals(city)&&!"".
  24. equals(phone)){
  25. Intent intent = new Intent(MainActivity.this, ResultActivity.class);
  26. Bundle bundle = new Bundle();
  27. bundle.putCharSequence("name",name);
  28. bundle.putCharSequence("sex",sex);
  29. bundle.putCharSequence("age",age);
  30. bundle.putCharSequence("city",city);
  31. bundle.putCharSequence("phone",phone);
  32. intent.putExtras(bundle);
  33. startActivity(intent);
  34. }else {
  35. Toast.makeText(MainActivity.this, "请将信息填写完整", Toast.LENGTH_SHORT).show();
  36. }
  37. }
  38. });
  39. }
  40. }

ResultActivity.java

  1. package com.example.bundledemo;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.EditText;
  6. import android.widget.TextView;
  7. public class ResultActivity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_result);
  12. Intent intent = getIntent();
  13. Bundle bundle = intent.getExtras();
  14. String name = bundle.getString("name");
  15. String sex = bundle.getString("sex");
  16. String age = bundle.getString("age");
  17. String city = bundle.getString("city");
  18. String phone = bundle.getString("phone");
  19. TextView tv_name = (TextView) findViewById(R.id.name);
  20. TextView tv_sex = (TextView) findViewById(R.id.sex);
  21. TextView tv_age = (TextView) findViewById(R.id.city);
  22. TextView tv_city = (TextView) findViewById(R.id.age);
  23. TextView tv_phone = (TextView) findViewById(R.id.phone);
  24. tv_name.setText(name);
  25. tv_sex.setText(sex);
  26. tv_age.setText(age);
  27. tv_city.setText(city);
  28. tv_phone.setText(phone);
  29. }
  30. }

效果:

使用Bundle在Activity中交换数据的更多相关文章

  1. 使用 Bundle 在 Activity 之间交换数据

    [toc] 使用 Bundle 在 Activity 之间交换数据 场景 当一个 Activity 启动另一个 Activity 时,常常会有一些数据需要传过去.因为两个 Activity 之间本来就 ...

  2. 建立、配置和使用Activity——使用Bundle在Activity之间交换数据

    当一个Activity启动另一个Activity时,常常会有一些数据需要传过去——这就像Web应用从一个Servlet跳到另一个Serlvet时,Web应用习惯把需要交换的数据放入requestSco ...

  3. 使用Bundle在Activity之间交换数据

    一:在main.xml文件中设置布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  4. 【Android 复习】:从Activity中返回数据

    在实际的应用中,我们不仅仅要向Activity传递数据,而且要从Activity中返回数据,虽然返回数据和传递类似,也可以采用上一讲中的四种方式来传递数据,但是一般建议采用Intent对象的方式的来返 ...

  5. 从Activity中返回数据

    从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...

  6. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  7. Android笔记——Activity中的数据传递案例(用户注冊)

    1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  8. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  9. Activity之间传递数据的方式及常见问题总结

    Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...

随机推荐

  1. Socket - TCP编程

    Socket是网络编程的一个抽象概念. 通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可 socket参数及常用功能 ...

  2. UIAutomator2安装及连接

    记录一下自己的偿试过程,内容来自:https://github.com/openatx/uiautomator2 d.service("uiautomator").stop()是因 ...

  3. [Flutter] 音频播放插件 audioplayers 的一个路径坑

    pubsepc.yaml 文件 flutter: assets: video/video-01.mp4 video/video-02.mp4 audio/bg.mp3 以上关于视频的文件的配置,都能正 ...

  4. 附件1:setTimeout与闭包

    我在详细图解作用域链与闭包一文中的结尾留下了一个关于setTimeout与循环闭包的思考题. 利用闭包,修改下面的代码,让循环输出的结果依次为1, 2, 3, 4, 5 for (var i=1; i ...

  5. SpringBoot工程创建的三种方式

    一. 通过IDEA的spring Initializer创建 1. 打开创建项目面板 File->New->Project->Spring Initializr 2. 填写Maven ...

  6. PAT1065 单身狗 (25分) 思路记录——参考大神柳婼

    1065 单身狗 (25分)   “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000), ...

  7. Rocket - debug - TLDebugModuleInner - ABSTRACTAUTO

    https://mp.weixin.qq.com/s/adSB7lmKcqmwVd80-gmdIw 简单介绍TLDebugModuleInner中ABSTRACTAUTO寄存器的实现. 1. ABST ...

  8. FreeRTOS-为什么关中断之后切换进程?

    https://mp.weixin.qq.com/s/S5HBH3RTo0B2irr8sGwDdw   一. 基本问题   FreeRTOS会在关键区即taskENTER_CRITICAL()和tas ...

  9. Java并发编程 (八) J.U.C组件拓展

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.J.U.C-FutureTask-1 FutureTask组件,该组件是JUC中的.但该组件不是 A ...

  10. Java实现 LeetCode 368 最大整除子集

    368. 最大整除子集 给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0. 如果有多个目标子集, ...