Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transfer parameters between Activity in Android

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"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间传递参数"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report