ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Activity之间参数传递

2020-03-12 11:57:32  阅读:235  来源: 互联网

标签:editText Activity list bundle 参数传递 intent 之间 new id


1、第一种,简单传递。

发送:

switch (v.getId()) {

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

intent.putExtra("data", editText.getText().toString());

startActivity(intent);

break;

接收:

Intent intent=getIntent();

String str1 = intent.getStringExtra("data");

TextView textView=findViewById(R.id.textView3);

textView.setText(str1);

2、第二种:使用bundle.外汇MT4教程

发送:

Bundle bundle=new Bundle();

bundle.putString("data1",editText.getText().toString()+editText.getText().toString());

intent.putExtras(bundle);

startActivity(intent);

接收:

Bundle bundle=getIntent().getExtras();

textView1.setText(bundle.getString("data1").toString());

3、相对复杂:

发送端:

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

//定义。

Mapmap=new HashMap<>();

map.put("key1","Value1");

map.put("key2","Value1");

List> list=new ArrayList<>();

list.add(map);

//须定义一个list用于在budnle中传递需要传递的

ArrayList<Object>,这个是必须要的!
Bundle bundle=new Bundle();
ArrayList bundlelist=new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
break;

接收:

//接收参数
Bundle bundle=getIntent().getExtras();
ArrayList list =bundle.getIntegerArrayList("list");
//把list中的参数,转换会
List<Map<String,Object>> lists=( List<Map<String,Object>>)list.get(0);

String sResult = "";
for (Map<String, Object> m : lists) {
for (String k : m.keySet()) {
sResult += "\r\n" + k + " : " + m.get(k);
}
}

TextView textView=findViewById(R.id.textView3);
TextView textView1=findViewById(R.id.textView4);
textView1.setText(sResult);

4、定义全局变量:
定义:

public class my_data{
public static String m1;
public static Integer d1;
}
使用:


textView.setText(my_data.m1);

标签:editText,Activity,list,bundle,参数传递,intent,之间,new,id
来源: https://www.cnblogs.com/benming/p/12468090.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有