• Android : 页面之间的数据传递 intent+bundle


    Android : 页面之间的数据传递 intent+bundle

      一:Activity之间的传递——从当前页面直接传到跳转 的页面

    1:方法:利用Intent和Bundle实现

    2:目录:

         ​编辑

         一个activity对应一个xml对应一个界面;

    3:MainAcitivity.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout 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=".MainActivity"
    8. android:orientation="vertical">
    9. <EditText
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:id="@+id/et1"
    13. android:hint="数据填写行1"
    14. />
    15. <EditText
    16. android:layout_width="match_parent"
    17. android:layout_height="wrap_content"
    18. android:id="@+id/et2"
    19. android:hint="数据填写行2"
    20. />
    21. <Button
    22. android:layout_width="wrap_content"
    23. android:layout_height="wrap_content"
    24. android:text="传递"
    25. android:layout_gravity="center"
    26. android:id="@+id/bt"/>
    27. </LinearLayout>

    传递页

     js.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout 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=".MainActivity"
    8. android:orientation="vertical">
    9. <TextView
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. android:id="@+id/tv1"
    13. android:layout_gravity="center"
    14. />
    15. <TextView
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:id="@+id/tv2"
    19. android:layout_gravity="center"
    20. />
    21. </LinearLayout>

    接收页

    4:java

    MainAcitivity.java

    1. package com.jyt.woc1;
    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. public class MainActivity extends AppCompatActivity {
    9. EditText et1,et2;
    10. Button bt;
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_main);
    15. et1=findViewById(R.id.et1);
    16. et2=findViewById(R.id.et2);
    17. bt=findViewById(R.id.bt);
    18. bt.setOnClickListener(new View.OnClickListener() {
    19. @Override
    20. public void onClick(View view) {
    21. String x,y;
    22. x=et1.getText().toString();
    23. y=et2.getText().toString();
    24. Bundle bundle=new Bundle();//存各种数据
    25. bundle.putString("1:",x);
    26. bundle.putString("2:",y);
    27. Intent woc =new Intent(MainActivity.this,js.class);
    28. woc.putExtras(bundle);
    29. startActivity(woc);
    30. }
    31. });
    32. }
    33. }

    js.java

    1. package com.jyt.woc1;
    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 js extends AppCompatActivity {
    8. TextView tv1,tv2;
    9. @Override
    10. protected void onCreate(Bundle savedInstanceState) {
    11. super.onCreate(savedInstanceState);
    12. setContentView(R.layout.js);
    13. tv1=findViewById(R.id.tv1);
    14. tv2=findViewById(R.id.tv2);
    15. Intent woc=getIntent();//获取
    16. Bundle nb=woc.getExtras();//取出数据
    17. tv1.setText(nb.getString("1:"));//通过key取出
    18. tv2.setText(nb.getString("2:"));
    19. }
    20. }

    二:Activity之间的传递——回调到上一页

    1:结构两个xml对应两个Acitivity 

    ​编辑

    2:xml文件

    activity_main.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout 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=".MainActivity"
    8. android:orientation="vertical">
    9. <TextView
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. android:text="Who am i?"
    13. android:layout_gravity="center"
    14. android:textSize="50sp"
    15. android:textColor="@color/black"
    16. />
    17. <TextView
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content"
    20. android:hint="你自己决定"
    21. android:layout_gravity="center"
    22. android:textSize="30sp"
    23. android:id="@+id/et"/>
    24. <Button
    25. android:layout_width="wrap_content"
    26. android:layout_height="wrap_content"
    27. android:text="让我康康!"
    28. android:textSize="20sp"
    29. android:layout_gravity="center"
    30. android:id="@+id/bt"/>
    31. </LinearLayout>

    xz.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:orientation="vertical">
    6.     <RadioGroup
    7.         android:layout_width="wrap_content"
    8.         android:layout_height="wrap_content"
    9.         android:layout_gravity="center"
    10.         android:id="@+id/rg">
    11.        <RadioButton
    12.            android:layout_width="wrap_content"
    13.            android:layout_height="wrap_content"
    14.            android:text="我是阿伟!"
    15.            android:textSize="20sp"
    16.            android:id="@+id/rb1"
    17.            />
    18.         <RadioButton
    19.             android:layout_width="wrap_content"
    20.             android:layout_height="wrap_content"
    21.             android:text="我是杰哥啊!"
    22.             android:textSize="20sp"
    23.             android:id="@+id/rb2"
    24.             />
    25.         <RadioButton
    26.             android:layout_width="wrap_content"
    27.             android:layout_height="wrap_content"
    28.             android:text="在下邢道荣!"
    29.             android:textSize="20sp"
    30.             android:id="@+id/rb3"
    31.             />
    32.     </RadioGroup>
    33.     <Button
    34.         android:layout_width="wrap_content"
    35.         android:layout_height="wrap_content"
    36.         android:id="@+id/bt1"
    37.         android:layout_gravity="center"
    38.         android:text="我觉醒了"
    39.         />
    40. </LinearLayout>

    3:java

    MainAcitivity.java

    1. package com.jyt.woc2;
    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.ImageView;
    9. import android.widget.TextView;
    10. import org.w3c.dom.Text;
    11. public class MainActivity extends AppCompatActivity {
    12.     private TextView ed;
    13.     private Button bt;
    14.     @Override
    15.     protected void onCreate(Bundle savedInstanceState) {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.activity_main);
    18.         ed=findViewById(R.id.et);
    19.         bt=findViewById(R.id.bt);
    20.         bt.setOnClickListener(new View.OnClickListener() {
    21.             @Override
    22.             public void onClick(View view) {
    23.                 Intent woc=new Intent(MainActivity.this,xz.class);//跳转
    24.                 startActivityForResult(woc, 1);/* 请求有回调的跳转 */
    25.             }
    26.         });
    27.     }
    28.     @Override//对回调的数据做对应的处理很关键!!
    29.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
    30.     {
    31.         super.onActivityResult(requestCode, resultCode, data);
    32.         if(requestCode==1 && resultCode==2){  //判断是否为待处理的结果   请求码和回调码一致才行
    33.             Bundle bundle=data.getExtras();          //获取传递的数据包
    34.             String woc =bundle.getString("woc");
    35.             ed.setText(woc);
    36.         }
    37.     }
    38. }

    xz.java

    1. package com.jyt.woc2;
    2. import android.content.Intent;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.RadioButton;
    7. import android.widget.RadioGroup;
    8. import androidx.appcompat.app.AppCompatActivity;
    9. public class xz extends AppCompatActivity {
    10.     private RadioButton rb1,rb2,rb3;
    11.     private Button bt;
    12.     @Override
    13.     protected void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.xz);
    16.         rb1=findViewById(R.id.rb1);
    17.         rb2=findViewById(R.id.rb2);
    18.         rb3=findViewById(R.id.rb3);
    19.         bt=findViewById(R.id.bt1);
    20.         bt.setOnClickListener(new View.OnClickListener() {
    21.             @Override
    22.             public void onClick(View view) {
    23.                 String x;
    24.                 if(rb1.isChecked())
    25.                 {
    26.                     x=rb1.getText().toString();
    27.                 }
    28.                 else if(rb2.isChecked())
    29.                 {
    30.                     x=rb2.getText().toString();
    31.                 }
    32.                 else
    33.                 {
    34.                     x=rb3.getText().toString();
    35.                 }
    36.                 Intent intent=new Intent();//创建并实例化一个intent对象
    37.                 Bundle bundle = new Bundle();    //实例化要传递的数据包
    38.                 bundle.putString("woc",x);// 放入数据
    39.                 intent.putExtras(bundle);    //将数据包保存到intent中
    40.                 setResult(2, intent);    //设置返回的结果码,并返回调用该Activity的Activity
    41.                 finish();        //关闭当前Activity
    42.             }
    43.         });
    44.     }
    45. }

    ​编辑

     ​编辑

    ​编辑


    三:Fragment 向Activity 传递数据

    ​编辑

    1:  activity_main.xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.     xmlns:tools="http://schemas.android.com/tools"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:orientation="vertical"
    6.     android:paddingBottom="@dimen/activity_vertical_margin"
    7.     android:paddingLeft="@dimen/activity_horizontal_margin"
    8.     android:paddingRight="@dimen/activity_horizontal_margin"
    9.     android:paddingTop="@dimen/activity_vertical_margin"
    10.     tools:context=".MainActivity" >
    11.   <FrameLayout
    12.         android:id="@+id/fragment_container1"
    13.         android:layout_width="match_parent"
    14.         android:layout_height="500dp"/>
    15. </LinearLayout>

    2:   MainActivity.java

    1. package com.example.a5_4_1;
    2. import android.app.Activity;
    3. import android.app.Fragment;
    4. import android.app.FragmentManager;
    5. import android.app.FragmentTransaction;
    6. import android.os.Bundle;
    7. public class MainActivity extends Activity {
    8.         FragmentManager fm;
    9.         FragmentTransaction ft,ft1;
    10.         Fragment f1=null;
    11.         protected void onCreate(Bundle savedInstanceState) {
    12.                super.onCreate(savedInstanceState);
    13.                setContentView(R.layout.activity_main);
    14.                fm = getFragmentManager();   // 获取Fragment
    15.                ft = fm.beginTransaction(); // 开启一个事务
    16.                f1 = new MainFragment1();
    17.                ft.add(R.id.fragment_container1, f1);
    18.                ft.commit();
    19.             }
    20.     
    21.  }

    xml中放一个FrameLayout,加载fragment界面

    3:mainfragment1.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent"
    6.     >
    7.     <LinearLayout
    8.       android:orientation="horizontal"
    9.       android:layout_width="wrap_content"
    10.       android:layout_height="wrap_content" >
    11.          <TextView
    12.         android:id="@+id/txt1"
    13.         android:text="请输入您的姓名"
    14.         android:textSize="30dp"
    15.         android:layout_width="wrap_content"
    16.         android:layout_height="wrap_content"
    17.         android:background="#FF0000"
    18.         />
    19.        <EditText
    20.         android:id="@+id/edt1"
    21.         android:text=""
    22.         android:textSize="30dp"
    23.         android:layout_width="180dp"
    24.         android:layout_height="wrap_content"
    25.         />
    26.     </LinearLayout>
    27.   
    28.     <Button
    29.         android:id="@+id/button"
    30.         android:text="点击"
    31.         android:gravity="center"
    32.         android:layout_marginTop="50sp"
    33.         android:layout_marginLeft="120sp"
    34.         android:textSize="30dp"
    35.         android:layout_width="wrap_content"
    36.         android:layout_height="wrap_content"
    37.         android:background="#00FF00"/>
    38. </LinearLayout>

    4:mainfragment.java

    1. package com.example.a5_4_1;
    2. import android.app.Fragment;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.view.LayoutInflater;
    6. import android.view.View;
    7. import android.view.ViewGroup;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10. public class MainFragment1 extends Fragment {
    11.     
    12.             @Override
    13.             public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    14.                 View contentView = inflater.inflate(R.layout.mainfragment1, container, false);
    15.                 return contentView;
    16.             }
    17.             public void onActivityCreated(Bundle savedInstanceState)
    18.                 super.onActivityCreated(savedInstanceState); 
    19.                 final EditText edt=(EditText)getActivity().findViewById(R.id.edt1);
    20.                 Button button = (Button) getActivity().findViewById(R.id.button); 
    21.                 button.setOnClickListener(new View.OnClickListener() { 
    22.             @Override 
    23.             public void onClick(View v)
    24.                
    25.                 Intent intent = new Intent();
    26.                 intent = new Intent(getActivity(), SecondActivity.class);
    27.                        intent.putExtra("data", edt.getText().toString());
    28.                        startActivity(intent);
    29.                         //Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_LONG).show(); 
    30.                     } 
    31.                 }); 
    32.            
    33.         }      
    34.         }

    5:

    SecondActivity.java

    1. package com.example.a5_4_1;
    2. import android.app.Activity;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6. public class SecondActivity extends Activity {
    7.         @Override
    8.         protected void onCreate(Bundle savedInstanceState) {
    9.                super.onCreate(savedInstanceState);
    10.                setContentView(R.layout.activity_second);
    11.                TextView text=(TextView)findViewById(R.id.txt1);
    12.    
    13.                Intent intent = getIntent();
    14.                String data1 = intent.getStringExtra("data");
    15.                text.setText("你的姓名是"+data1);
    16.         }
    17. }

    6:activity_second.xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.     xmlns:tools="http://schemas.android.com/tools"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:paddingBottom="@dimen/activity_vertical_margin"
    6.     android:paddingLeft="@dimen/activity_horizontal_margin"
    7.     android:paddingRight="@dimen/activity_horizontal_margin"
    8.     android:paddingTop="@dimen/activity_vertical_margin"
    9.     tools:context=".SecondActivity" >
    10.     <TextView
    11.         android:id="@+id/txt1"
    12.         android:layout_width="wrap_content"
    13.         android:layout_height="wrap_content"
    14.         android:text="" />
    15.   
    16. </LinearLayout>

    这里不难发现,基本原理就和Activity之间的直传差不多,只是在Main下只加载界面而工作全部在加载的界面下完成


  • 相关阅读:
    一个字符串模式匹配开源库
    第十二章 文件的上传和下载
    xhEditor实现WORD粘贴图片自动上传
    docker部署mysql无法远程连接2003解决
    k8s--基础--23.2--认证-授权-准入控制--认证
    CTF-Misc——图片分析
    Swagger简单使用
    针对遗留系统采取的不同演化策略
    Mysql加锁流程详解
    应用ceph文件系统存储(ceph-13.2.10)
  • 原文地址:https://blog.csdn.net/weixin_55439495/article/details/128125801