Android 中Activity 之间传递参数
作者:一人一城_Qian 发布时间:2022-09-06 04:29:20
标签: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<User> CREATOR = new Creator<User>() {
@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"));
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://www.jianshu.com/p/a3c2be98894f


猜你喜欢
- 假设你已经装了texlive打开cmd输入latex --version应该能输出打开vscode,安装这几个插件设置->Settin
- 第一步:项目中资源配置文件夹(resources文件夹)下先新增测试环境application-dev.yml和application-pr
- 这篇文章主要介绍了spring cloud gateway网关路由分配代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有
- 复制文件并命名的超简洁写法没错又是我,这次为大家带来Java中 复制文件并命名的超简洁写法(请确保你的jre在1.8+),这次用到了File
- 不论是float 还是double都是浮点数,而计算机是二进制的,浮点数会失去一定的精确度。Java在java.math包中提供的API类B
- 首先引入pom <!--SpringBoot 2.1.0--> <parent>  
- 我们都知道,当RecyclerView数据源更新后,还需要通过adapter调用对应的方法,从而让RecyclerView重新绘制页面本次也
- 1 什么是cookie浏览器与WEB服务器之间是使用HTTP协议进行通信的,当某个用户发出页面请求时,WEB服务器只是简单的进行响应,然后就
- 本文为大家分享了Android Studio debug功能的具体使用方法,供大家参考,具体内容如下运行debug模式 1. 进入
- 前言XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场
- https://www.jb51.net/article/114838.htm这篇文章很详细的介绍了JS的跨域,给出的解决方案是spring
- 解决Android调用系统分享图片给微信,出现分享失败,分享多文件必须为图片格式近期应公司需求,分享多图片到微信的功能,之前一直是用微信自己
- 用一道选择题作为本文的开始吧! ArrayList list = new ArrayList(20);中的list扩充几次 A.0 B.1
- 在项目中常常常使用到DataTable,假设DataTable使用得当,不仅能使程序简洁有用,并且可以提高性能,达到事半功倍的效果,List
- 本文实例为大家分享了android通过usb读取U盘的具体代码,供大家参考,具体内容如下1.关联 compile ‘com.github.m
- 本文实例讲述了C#创建、读取和修改Excel的方法。分享给大家供大家参考。具体如下:windows下我们可以通过 Jet OLE DB访问E
- 本章,会对synchronized关键字进行介绍。涉及到的内容包括:1. synchronized原理2. synchronized基本规则
- 我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用。而ViewPage
- 前言Java作为一种平台无关性的语言,其主要依靠于Java虚拟机——JVM,我们写好的代码会被编译成class文件,再由JVM进行加载、解析
- 本文实例为大家分享Winform版计算器的具体实现方法,供大家参考,具体内容如下前台页面设计后台代码实现using System;using