Android中如何实现Activity间传递参数
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"Android中如何实现Activity间传递参数",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android中如何实现Activity
千家信息网最后更新 2026年11月01日Android中如何实现Activity间传递参数
本篇内容主要讲解"Android中如何实现Activity间传递参数",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android中如何实现Activity间传递参数"吧!
1.传递简单数据
在A Activity中
findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,TheActivity.class);// 对基础的数据类型进行传递 i.putExtra("data","我是国人"); startActivity(i); } });在B Activity中接受数据
tv =(TextView)findViewById(R.id.TheTextView); Intent i = getIntent(); tv.setText(i.getStringExtra("data"));这种传值就是传递基本的数据类型
2.传递数据 包Bundle
在A Activity中
findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,TheActivity.class); Bundle bundle = new Bundle(); bundle.putString("name","qll"); bundle.putInt("age",3);// i.putExtras(bundle); // 另种传递方式 i.putExtra("data",bundle); startActivity(i); } }); }在B Activity中接受数据
tv =(TextView)findViewById(R.id.TheTextView); editText = (EditText) findViewById(R.id.editText3); Intent i = getIntent();// Bundle date = i.getExtras();// 接受方式不同 Bundle date = i.getBundleExtra("data"); tv.setText(String.format("name=%s,age=%d",date.getString("name"),date.getInt("age")));这总传递方式类似iOS中传递字典数据类型过来 。
3.传递值对象
自定义一个User类,传递自定义类需要对类进行序列化
用Serializable进行序列化
这种方法只需要类实现Serializable接口就可以了
User 类
import java.io.Serializable;public class User implements Serializable{ private String name; private int age; public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public User(String name,int age){ this.name = name; this.age = age; }在A Activity中
findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,TheActivity.class); i.putExtra("user",new User("qll",24)); startActivity(i); } }); }在B Activity中
tv =(TextView)findViewById(R.id.TheTextView); User user = (User)i.getSerializableExtra("user"); tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));用Parcelable实现
同样的需要实现Parcelable接口
User 类
package com.example.wyhaiapple.transferdata1;import android.os.Parcel;import android.os.Parcelable;import android.text.ParcelableSpan;public class User implements Parcelable{ private String name; private int age; public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public User(String name,int age){ this.name = name; this.age = age; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(getName()); dest.writeInt(getAge()); } public static final Creator CREATOR = new Creator() { @Override public User createFromParcel(Parcel source) { return new User(source.readString(),source.readInt()); } @Override public User[] newArray(int size) { return new User[size]; } };} 在A Activity中 与上面的相同
在B Activity中
tv =(TextView)findViewById(R.id.TheTextView); User user = (User)i.getParcelableExtra("user"); tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));4.获取 Activity 的返回参数
在B Activity中
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_the); tv =(TextView)findViewById(R.id.TheTextView); editText = (EditText) findViewById(R.id.editText3); findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.putExtra("data",editText.getText().toString()); setResult(1,i); finish(); } }); }}在A Activity中
startActivityForResult(i,0); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); textView.setText("返回的值:"+data.getStringExtra("data")); }到此,相信大家对"Android中如何实现Activity间传递参数"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
数据
参数
方式
类型
内容
序列
接口
方法
学习
不同
实用
更深
兴趣
国人
基础
字典
实用性
实际
对象
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网黑科技
互联网科技股
互联网科技网
互联网科技工作委员会副主任毛军
互联网科技官网
中国互联网科技股
金融科技 互联网金融
万易互联网科技
华中科技大学互联网
上海万想互联网科技
港股科技和互联网
宜昌元轩互联网科技有限公司
南博望互联网科技有限公司
四川互联网展示科技馆设计
富国互联网科技002692
2021世界互联网领先科技成果发布
广州梦谷互联网科技有限公司领导
米泉互联网科技专业怎么样
科技和互联网行业的特点
互联网科技大佬云集
百单网互联网科技有限公司怎么样
广州互联网络科技有限公司
互联网科技有限公司 翻译
济南和联互联网科技有限公司
现代互联网科技股
互联网保险科技创业
滨湖区互联网智能科技产品有哪些
众安科技工业互联网合作
互联网高科技行业有哪些企业
银川新华互联网科技学校原画