• Android开发学习日记--页面间传递数据


    一、效果展示

    二、实现步骤

    1.向下一个页面传递数据

        Intent对象的setData方法只指定到达目标的路径,不能携带参数信息。真正的参数存在Extras,我么可以直接使用setExtra方法,传递包括整型、浮点型、字符串等基本数据类型,甚至还包括Serializable这样的序列化结构。但是这样调用显然数据随便丢进去不好管理,所以Android引入了Bundle概念。

        Bundle 内部用于存放消息的数据结构是 Map 映射,既可添加或删除元素,还可判断元素是否存在。开发者若要把Bundle 数据全部打包好,只需调用一次意图对象的putExtras 方法;若要把 Bundle 数据全部取出来,也只需调用一次意图对象的getExtras 方法。 Bundle 对象操作各类型数据的读写方法说明见表。

     XML代码:

    1. //主界面
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. android:gravity="center"
    9. tools:context=".MainActivity">
    10. <EditText
    11. android:id="@+id/message"
    12. android:layout_width="match_parent"
    13. android:layout_height="wrap_content"
    14. android:hint="你想说的话:"
    15. />
    16. <Button
    17. android:id="@+id/next"
    18. android:layout_width="match_parent"
    19. android:layout_height="wrap_content"
    20. android:text="发送"
    21. />
    22. </LinearLayout>
    23. //界面二
    24. <?xml version="1.0" encoding="utf-8"?>
    25. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    26. xmlns:tools="http://schemas.android.com/tools"
    27. android:layout_width="match_parent"
    28. android:layout_height="match_parent"
    29. tools:context=".Main2Activity">
    30. <TextView
    31. android:id="@+id/two"
    32. android:layout_width="match_parent"
    33. android:layout_height="wrap_content"
    34. />
    35. </LinearLayout>

    Java代码:

    1. //主界面
    2. import android.content.Intent;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.EditText;
    6. import androidx.annotation.Nullable;
    7. import androidx.appcompat.app.AppCompatActivity;
    8. import com.example.helloworld.util.DateUtil;
    9. public class MainActivity extends AppCompatActivity{
    10. EditText e;
    11. @Override
    12. protected void onCreate(@Nullable Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_main);
    15. e=findViewById(R.id.message);
    16. findViewById(R.id.next).setOnClickListener(new View.OnClickListener()
    17. {
    18. @Override
    19. public void onClick(View v) {
    20. String date=DateUtil.getNowTime(); //获取当前时间
    21. String message=e.getText().toString(); //获取编辑框内信息
    22. Bundle bundle=new Bundle();
    23. bundle.putString("date", date); //将时间信息放在Bundle中,key为date
    24. bundle.putString("message", message); //将编辑框信息放在Bundle中,key为message
    25. Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    26. intent.putExtras(bundle); //将Bundle添加到意图中
    27. startActivity(intent); //跳转到意图指定活动界面
    28. }
    29. });
    30. }
    31. }
    32. //界面二
    33. import android.os.Bundle;
    34. import android.widget.TextView;
    35. import androidx.appcompat.app.AppCompatActivity;
    36. public class Main2Activity extends AppCompatActivity {
    37. @Override
    38. protected void onCreate(Bundle savedInstanceState) {
    39. super.onCreate(savedInstanceState);
    40. setContentView(R.layout.activity_main2);
    41. TextView t=findViewById(R.id.two);
    42. Bundle b= getIntent().getExtras(); //从上一个页面传来的意图中获取Bundle包裹
    43. String request_date=b.getString("date"); //从包裹中取出名为date的字符串
    44. String message=b.getString("message"); //从包裹中取出名为message的字符串
    45. t.setText(String.format("请求时间为:%s\n发送内容为:%s", request_date,message));
    46. }
    47. }

    2.向上一个页面返回数据

    数据传递经常是相互的,上一个页面不但把请求数据发送到下一个页面,有时候还要处理下一
    个页面的 应答数据,所谓应答发生在下一个页面返回到上一个页面之际。如果只把请求数据发下一个页面, 上一个页面调用startActivity 方法即可;如果还要处理下一个页面的应答数据,此时就得分多步处理,详细步骤说明如下
    步骤一,上一个页面打包好请求数据,调用startActivityForResult 方法执行跳转动作,表示需要处理下 一个页面的应答数据,该方法的第二个参数表示请求代码,它用于标识每个跳转的唯一性。
    步骤二,下一个页面接收并解析请求数据,进行相应处理。
    步骤三,下一个页面在返回上一个页面时,打包应答数据并调用setResult 方法返回数据包裹。 setResult 方法的第一个参数表示应答代码(成功还是失败),第二个参数为携带包裹的意图对象。
    步骤四,上一个页面重写方法onActivityResult ,该方法的输入参数包含请求代码和结果代码,其中请求 代码用于判断这次返回对应哪个跳转,结果代码用于判断下一个页面是否处理成功。如果下
    一个页面处 理成功,再对返回数据解包操作。
    XML代码:
    1. //主界面
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. android:gravity="center"
    9. tools:context=".MainActivity">
    10. <EditText
    11. android:id="@+id/message"
    12. android:layout_width="match_parent"
    13. android:layout_height="wrap_content"
    14. android:hint="你想说的话:"
    15. />
    16. <Button
    17. android:id="@+id/next"
    18. android:layout_width="match_parent"
    19. android:layout_height="wrap_content"
    20. android:text="发送"
    21. />
    22. <TextView
    23. android:id="@+id/call_back_t"
    24. android:layout_width="match_parent"
    25. android:layout_height="wrap_content"
    26. android:textColor="#000000"
    27. />
    28. </LinearLayout>
    29. //界面二
    30. <?xml version="1.0" encoding="utf-8"?>
    31. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    32. xmlns:tools="http://schemas.android.com/tools"
    33. android:layout_width="match_parent"
    34. android:layout_height="match_parent"
    35. tools:context=".Main2Activity">
    36. <TextView
    37. android:id="@+id/two"
    38. android:layout_width="match_parent"
    39. android:layout_height="wrap_content"
    40. android:textColor="#000000"
    41. />
    42. </LinearLayout>

    Java代码:

    1. //主页面Java代码
    2. import android.app.Activity;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.EditText;
    7. import android.widget.TextView;
    8. import androidx.annotation.Nullable;
    9. import androidx.appcompat.app.AppCompatActivity;
    10. import com.example.helloworld.util.DateUtil;
    11. public class MainActivity extends AppCompatActivity{
    12. EditText editText;
    13. TextView call_back_t;
    14. @Override
    15. protected void onCreate(@Nullable Bundle savedInstanceState) {
    16. super.onCreate(savedInstanceState);
    17. setContentView(R.layout.activity_main);
    18. editText =findViewById(R.id.message);
    19. call_back_t=findViewById(R.id.call_back_t);
    20. findViewById(R.id.next).setOnClickListener(new View.OnClickListener()
    21. {
    22. @Override
    23. public void onClick(View v) {
    24. String date=DateUtil.getNowTime();
    25. String message= editText.getText().toString();
    26. Bundle bundle=new Bundle();
    27. bundle.putString("date", date);
    28. bundle.putString("message", message);
    29. Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    30. intent.putExtras(bundle);
    31. //请求代码大于等于0则代表需要返回数据,请求代码表示每个跳转的唯一性,请求代码<=65535
    32. startActivityForResult(intent,0);
    33. }
    34. });
    35. }
    36. @Override
    37. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    38. super.onActivityResult(requestCode, resultCode, data);
    39. //返回数据data不为空 请求代码为0 结果代码为RESULT_OK 则代表返回成功
    40. if(data!=null&&requestCode==0&&resultCode== Activity.RESULT_OK)
    41. {
    42. Bundle bundle=data.getExtras();
    43. String message="";
    44. if(bundle.containsKey("receive_date"))
    45. message=bundle.getString("receive_date");
    46. call_back_t.setText("数据返回时间:"+message);
    47. }
    48. }
    49. }
    50. //页面二Java代码
    51. import android.app.Activity;
    52. import android.content.Intent;
    53. import android.os.Bundle;
    54. import android.widget.TextView;
    55. import androidx.appcompat.app.AppCompatActivity;
    56. import com.example.helloworld.util.DateUtil;
    57. public class Main2Activity extends AppCompatActivity {
    58. @Override
    59. protected void onCreate(Bundle savedInstanceState) {
    60. super.onCreate(savedInstanceState);
    61. setContentView(R.layout.activity_main2);
    62. TextView t=findViewById(R.id.two);
    63. Bundle b= getIntent().getExtras(); //打开上个页面传来的包裹
    64. String request_date=b.getString("date"); //取出名为date的字符串
    65. String message=b.getString("message"); //取出名为message的字符串
    66. //将信息显示到文本框内
    67. t.setText(String.format("请求时间为:%s\n发送内容为:%s", request_date,message));
    68. }
    69. @Override
    70. protected void onResume() {
    71. super.onResume();
    72. //准备返回数据
    73. String receive_date = DateUtil.getNowTime();
    74. Intent intent = new Intent();
    75. intent.putExtra("receive_date", receive_date);
    76. setResult(Activity.RESULT_OK, intent);
    77. }
    78. }

  • 相关阅读:
    【高并发基石】多线程、守护线程、线程安全、线程同步、互斥锁
    卷积神经网络(包含代码与相应图解)
    用户画像的设计准则以及美团外卖用户画像的设计案例
    【MySQL】了解并操作MySQL的缓存配置与信息
    window+anaconda+pytorch+vscode+python调试
    文心一言 VS 讯飞星火 VS chatgpt (285)-- 算法导论21.2 4题
    【毕业设计】中文对话问答机器人系统 - python 深度学习
    二叉树的建立及线索二叉树
    IDEA插件开发(3)---捆绑插件 API 源
    模板函数和模板类的特例化
  • 原文地址:https://blog.csdn.net/m0_52238102/article/details/125467177