客服热线:18391752892

Android 2个activity 之间的数据传递顺水鱼财经

核心摘要:【IT168 技术】初学android,小小的学习总结1.通过intent来传递:A.传字符等:activity1中设置:Java代码String text = "hello";Intent intent1 = new Intent(ActivityMain.this, Activity2
外汇期货股票比特币交易【IT168 技术】初学android,小小的学习总结

1.通过intent来传递:

A.传字符等:activity1中设置:

Java代码

String text = "hello";

Intent intent1 = new Intent(ActivityMain.this, Activity2.class);

intent1.putExtra("activity1", text);

startActivity(intent1 );

String text = "hello";

Intent intent1 = new Intent(ActivityMain.this, Activity2.class);

intent1.putExtra("activity1", text);

startActivity(intent1 );

B.传对象,对象要实例化,继承Serializable

Java代码

Bundle mbundle=new Bundle(); mbundle.putSerializable("user",userList.get(position));

Intent in =new Intent (getApplicationContext(), activity2.class);

in.putExtras(mbundle);

startActivity(in);

Bundle mbundle=new Bundle(); mbundle.putSerializable("user",userList.get(position));

Intent in =new Intent (getApplicationContext(), activity2.class);

in.putExtras(mbundle);

startActivity(in);

activity2中接收:

A:接收

Java代码

Bundle extras = getIntent().getExtras();

if (extras != null) {

textview.setText(extras.getString("activity1"));

}

Bundle extras = getIntent().getExtras();

if (extras != null) {

textview.setText(extras.getString("activity1"));

}

B.接收

Java代码

Bundle bundel = getIntent().getExtras();

user= (User) bundel.get("user");

Bundle bundel = getIntent().getExtras();

user= (User) bundel.get("user");

2.SharedPreferences

我在activity1中设置的如下:

Java代码

SharedPreferences sp =getSharedPreferences("textinfo",0);

Editor editor=sp.edit();

String text = "hello";

editor.putString("text", text);

editor.commit();Intent i = new Intent(getApplicationContext(),activity2.class);

startActivity(i);

SharedPreferences sp =getSharedPreferences("textinfo",0);

Editor editor=sp.edit();

String text = "hello";

editor.putString("text", text);

editor.commit();

Intent i = new Intent(getApplicationContext(),activity2.class);

startActivity(i);

跳转到Message的activity,获取内容如下

Java代码

SharedPreferences share=getSharedPreferences("textinfo",0);

String text =share.getString("text", null);

msgtextview.setText(text);
(责任编辑:admin)
下一篇:

4.25黄金多空争夺可双向斩利,周获利4.3万不过尔尔!

上一篇:

银行理财产品月均超4600款顺水鱼财经

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们
 
0相关评论