付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么实现Activity间传递参数
这篇文章将为大家详细讲解有关Android中怎么实现Activity间传递参数,文章内容质量较高,因此在下分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1.传递简单数据在AActivity中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);
}
});在BActivity中接受数据
tv
=(TextView)findViewById(R.id.TheTextView);
Intent
i
=
getIntent();
tv.setText(i.getStringExtra("data"));这种传值就是传递基本的数据类型2.传递数据包Bundle在AActivity中
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);
}
});
}在BActivity中接受数据
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){
=
name;
}
public
User(String
name,int
age){
=
name;
this.age
=
age;
}在AActivity中
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);
}
});
}在BActivity中
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){
=
name;
}
public
User(String
name,int
age){
=
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];
}
};
}在AActivity中与上面的相同在BActivity中
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的返回参数在BActivity中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();
}
});
}
}在AActivity中startActivityForResult(i,0);
@Override
protected
void
onActivityResult(int
requestCode,
int
resultC
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 重庆市渝中区名校2026届初三十六模生物试题含解析
- 2026年广东省揭阳市榕城区一中学初三招生考试物理试题模拟测试附加题试题含解析
- 护理查房中的护理法律风险防范
- 2025年前台防疫接待礼仪能力提升
- 手术室输血护理与注意事项
- 感染控制与护理科研
- 护理人员死亡教育培训效果评估
- 2026二年级数学 人教版数学乐园计算擂台
- 忠诚履职落实责任制度
- 房地产安全主体责任制度
- 国开2026年春季《形势与政策》专题测验1-5答案
- 2026年山东圣翰财贸职业学院单招职业技能考试题库及答案解析
- 2026离婚协议书标准范文
- GB 14249-2026电子衡器安全要求
- 2026四川宜宾发展产城投资有限公司及子公司第一批员工招聘35人考试参考试题及答案解析
- 2026年邮政从业职业技能鉴定考试题库(附答案)
- 2026第二师铁门关市公安局招聘警务辅助人员(36人)笔试备考题库及答案解析
- 2026年临汾职业技术学院单招职业技能考试题库带答案详解(精练)
- 2026年春期人教版四年级下册数学全册教案(核心素养教案)
- 2026年法律专业基础知识考试试题及答案
- (完整版)护士结构化面试题目及答案
评论
0/150
提交评论