Android : 页面之间的数据传递 intent+bundle
一:Activity之间的传递——从当前页面直接传到跳转 的页面
1:方法:利用Intent和Bundle实现
2:目录:

编辑
一个activity对应一个xml对应一个界面;
3:MainAcitivity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
-
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et1"
- android:hint="数据填写行1"
- />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/et2"
- android:hint="数据填写行2"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="传递"
- android:layout_gravity="center"
- android:id="@+id/bt"/>
-
- </LinearLayout>
-
![]()
传递页
js.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/tv1"
-
- android:layout_gravity="center"
- />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/tv2"
-
- android:layout_gravity="center"
- />
-
- </LinearLayout>
![]()
接收页
4:java
MainAcitivity.java
- package com.jyt.woc1;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
-
- public class MainActivity extends AppCompatActivity {
- EditText et1,et2;
- Button bt;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- et1=findViewById(R.id.et1);
- et2=findViewById(R.id.et2);
- bt=findViewById(R.id.bt);
-
- bt.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String x,y;
- x=et1.getText().toString();
- y=et2.getText().toString();
-
- Bundle bundle=new Bundle();//存各种数据
- bundle.putString("1:",x);
- bundle.putString("2:",y);
-
- Intent woc =new Intent(MainActivity.this,js.class);
- woc.putExtras(bundle);
- startActivity(woc);
-
- }
- });
-
- }
- }
![]()
js.java
- package com.jyt.woc1;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.EditText;
- import android.widget.TextView;
-
- public class js extends AppCompatActivity {
- TextView tv1,tv2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.js);
-
- tv1=findViewById(R.id.tv1);
- tv2=findViewById(R.id.tv2);
-
- Intent woc=getIntent();//获取
- Bundle nb=woc.getExtras();//取出数据
-
- tv1.setText(nb.getString("1:"));//通过key取出
- tv2.setText(nb.getString("2:"));
-
-
-
- }
- }
二:Activity之间的传递——回调到上一页
1:结构两个xml对应两个Acitivity

编辑
2:xml文件
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Who am i?"
- android:layout_gravity="center"
- android:textSize="50sp"
- android:textColor="@color/black"
-
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint="你自己决定"
- android:layout_gravity="center"
- android:textSize="30sp"
- android:id="@+id/et"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="让我康康!"
-
- android:textSize="20sp"
- android:layout_gravity="center"
- android:id="@+id/bt"/>
-
- </LinearLayout>
![]()
xz.xml
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- android:layout_width="match_parent"
-
- android:layout_height="match_parent"
-
- android:orientation="vertical">
-
- <RadioGroup
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:layout_gravity="center"
-
- android:id="@+id/rg">
-
- <RadioButton
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="我是阿伟!"
-
- android:textSize="20sp"
-
- android:id="@+id/rb1"
-
- />
-
-
-
- <RadioButton
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="我是杰哥啊!"
-
- android:textSize="20sp"
-
- android:id="@+id/rb2"
-
- />
-
-
-
- <RadioButton
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="在下邢道荣!"
-
- android:textSize="20sp"
-
- android:id="@+id/rb3"
-
- />
-
- </RadioGroup>
-
- <Button
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:id="@+id/bt1"
-
- android:layout_gravity="center"
-
- android:text="我觉醒了"
-
- />
-
-
-
- </LinearLayout>
![]()
3:java
MainAcitivity.java
-
- package com.jyt.woc2;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
-
- import android.os.Bundle;
-
- import android.view.View;
-
- import android.widget.Button;
-
- import android.widget.EditText;
-
- import android.widget.ImageView;
-
- import android.widget.TextView;
-
- import org.w3c.dom.Text;
-
- public class MainActivity extends AppCompatActivity {
-
- private TextView ed;
-
- private Button bt;
-
- @Override
-
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_main);
-
- ed=findViewById(R.id.et);
-
- bt=findViewById(R.id.bt);
-
- bt.setOnClickListener(new View.OnClickListener() {
-
- @Override
-
- public void onClick(View view) {
-
- Intent woc=new Intent(MainActivity.this,xz.class);//跳转
-
- startActivityForResult(woc, 1);/* 请求有回调的跳转 */
-
- }
-
- });
-
- }
-
- @Override//对回调的数据做对应的处理很关键!!
-
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
-
- {
-
- super.onActivityResult(requestCode, resultCode, data);
-
- if(requestCode==1 && resultCode==2){ //判断是否为待处理的结果 请求码和回调码一致才行
-
- Bundle bundle=data.getExtras(); //获取传递的数据包
-
- String woc =bundle.getString("woc");
-
- ed.setText(woc);
-
- }
-
- }
-
- }
-
-
-
-
xz.java
- package com.jyt.woc2;
-
-
-
- import android.content.Intent;
-
- import android.os.Bundle;
-
- import android.view.View;
-
- import android.widget.Button;
-
- import android.widget.RadioButton;
-
- import android.widget.RadioGroup;
-
-
-
- import androidx.appcompat.app.AppCompatActivity;
-
-
-
- public class xz extends AppCompatActivity {
-
-
-
- private RadioButton rb1,rb2,rb3;
-
- private Button bt;
-
- @Override
-
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.xz);
-
-
-
- rb1=findViewById(R.id.rb1);
-
- rb2=findViewById(R.id.rb2);
-
- rb3=findViewById(R.id.rb3);
-
-
-
- bt=findViewById(R.id.bt1);
-
-
-
- bt.setOnClickListener(new View.OnClickListener() {
-
- @Override
-
- public void onClick(View view) {
-
- String x;
-
- if(rb1.isChecked())
-
- {
-
- x=rb1.getText().toString();
-
- }
-
- else if(rb2.isChecked())
-
- {
-
- x=rb2.getText().toString();
-
- }
-
- else
-
- {
-
- x=rb3.getText().toString();
-
- }
-
- Intent intent=new Intent();//创建并实例化一个intent对象
-
- Bundle bundle = new Bundle(); //实例化要传递的数据包
-
- bundle.putString("woc",x);// 放入数据
-
- intent.putExtras(bundle); //将数据包保存到intent中
-
- setResult(2, intent); //设置返回的结果码,并返回调用该Activity的Activity
-
- finish(); //关闭当前Activity
-
- }
-
- });
-
- }
-
- }
![]()

编辑

编辑

编辑
三:Fragment 向Activity 传递数据

编辑
1: activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- xmlns:tools="http://schemas.android.com/tools"
-
- android:layout_width="match_parent"
-
- android:layout_height="match_parent"
-
- android:orientation="vertical"
-
- android:paddingBottom="@dimen/activity_vertical_margin"
-
- android:paddingLeft="@dimen/activity_horizontal_margin"
-
- android:paddingRight="@dimen/activity_horizontal_margin"
-
- android:paddingTop="@dimen/activity_vertical_margin"
-
- tools:context=".MainActivity" >
-
- <FrameLayout
-
- android:id="@+id/fragment_container1"
-
- android:layout_width="match_parent"
-
- android:layout_height="500dp"/>
-
- </LinearLayout>
![]()
2: MainActivity.java
- package com.example.a5_4_1;
-
-
-
-
-
- import android.app.Activity;
-
- import android.app.Fragment;
-
- import android.app.FragmentManager;
-
- import android.app.FragmentTransaction;
-
- import android.os.Bundle;
-
- public class MainActivity extends Activity {
-
- FragmentManager fm;
-
- FragmentTransaction ft,ft1;
-
- Fragment f1=null;
-
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_main);
-
-
-
- fm = getFragmentManager(); // 获取Fragment
-
- ft = fm.beginTransaction(); // 开启一个事务
-
- f1 = new MainFragment1();
-
- ft.add(R.id.fragment_container1, f1);
-
- ft.commit();
-
-
-
- }
-
-
-
- }
![]()
xml中放一个FrameLayout,加载fragment界面
3:mainfragment1.xml
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- android:orientation="vertical"
-
- android:layout_width="match_parent"
-
- android:layout_height="match_parent"
-
- >
-
- <LinearLayout
-
- android:orientation="horizontal"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content" >
-
- <TextView
-
- android:id="@+id/txt1"
-
- android:text="请输入您的姓名"
-
- android:textSize="30dp"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:background="#FF0000"
-
- />
-
- <EditText
-
- android:id="@+id/edt1"
-
- android:text=""
-
- android:textSize="30dp"
-
- android:layout_width="180dp"
-
- android:layout_height="wrap_content"
-
- />
-
- </LinearLayout>
-
-
-
- <Button
-
- android:id="@+id/button"
-
- android:text="点击"
-
- android:gravity="center"
-
- android:layout_marginTop="50sp"
-
- android:layout_marginLeft="120sp"
-
- android:textSize="30dp"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:background="#00FF00"/>
-
- </LinearLayout>
![]()
4:mainfragment.java
- package com.example.a5_4_1;
-
-
-
- import android.app.Fragment;
-
- import android.content.Intent;
-
- import android.os.Bundle;
-
- import android.view.LayoutInflater;
-
- import android.view.View;
-
- import android.view.ViewGroup;
-
- import android.widget.Button;
-
- import android.widget.EditText;
-
- public class MainFragment1 extends Fragment {
-
-
-
- @Override
-
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-
- View contentView = inflater.inflate(R.layout.mainfragment1, container, false);
-
-
-
- return contentView;
-
- }
-
- public void onActivityCreated(Bundle savedInstanceState) {
-
- super.onActivityCreated(savedInstanceState);
-
- final EditText edt=(EditText)getActivity().findViewById(R.id.edt1);
-
- Button button = (Button) getActivity().findViewById(R.id.button);
-
- button.setOnClickListener(new View.OnClickListener() {
-
- @Override
-
- public void onClick(View v) {
-
-
-
- Intent intent = new Intent();
-
- intent = new Intent(getActivity(), SecondActivity.class);
-
- intent.putExtra("data", edt.getText().toString());
-
- startActivity(intent);
-
- //Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_LONG).show();
-
- }
-
- });
-
-
-
- }
-
- }
![]()
5:
SecondActivity.java
-
- package com.example.a5_4_1;
-
- import android.app.Activity;
-
- import android.content.Intent;
-
- import android.os.Bundle;
-
- import android.widget.TextView;
-
- public class SecondActivity extends Activity {
-
- @Override
-
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_second);
-
- TextView text=(TextView)findViewById(R.id.txt1);
-
-
-
- Intent intent = getIntent();
-
- String data1 = intent.getStringExtra("data");
-
- text.setText("你的姓名是"+data1);
-
- }
-
- }
-
-
-
-
6:activity_second.xml
-
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- xmlns:tools="http://schemas.android.com/tools"
-
- android:layout_width="match_parent"
-
- android:layout_height="match_parent"
-
- android:paddingBottom="@dimen/activity_vertical_margin"
-
- android:paddingLeft="@dimen/activity_horizontal_margin"
-
- android:paddingRight="@dimen/activity_horizontal_margin"
-
- android:paddingTop="@dimen/activity_vertical_margin"
-
- tools:context=".SecondActivity" >
-
- <TextView
-
- android:id="@+id/txt1"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="" />
-
-
-
- </LinearLayout>
-
-
-
-
这里不难发现,基本原理就和Activity之间的直传差不多,只是在Main下只加载界面而工作全部在加载的界面下完成