本文翻译自:How to use putExtra() and getExtra() for string data

Can someone please tell me how exactly to use getExtra() and putExtra() for intents? 有人可以告诉我如何使用getExtra()putExtra()进行意图? Actually I have a string variable, say str, which stores some string data. 实际上我有一个字符串变量,比如str,它存储一些字符串数据。 Now, I want to send this data from one activity to another activity. 现在,我想将这些数据从一个活动发送到另一个活动。

  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   String keyIdentifer  = null;i.putExtra(strName, keyIdentifer );

and then in the SecondScreen.java 然后在SecondScreen.java中

 public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.table);TextView userName = (TextView)findViewById(R.id.userName);Bundle bundle = getIntent().getExtras();if(bundle.getString("strName")!= null){//TODO here get the string stored in the string variable and do // setText() on userName }}

I know it is very basic question but unfortunately I am stuck here. 我知道这是一个非常基本的问题但不幸的是我被困在这里。 Please help. 请帮忙。

Thanks, 谢谢,

Edit: Here the string which I am trying to pass from one screen to the other is dynamic. 编辑:这里我试图从一个屏幕传递到另一个屏幕的字符串是动态的。 That is I have an editText where I am getting string whatever user types. 那就是我有一个editText,我得到的字符串无论用户类型如何。 Then with the help of myEditText.getText().toString() . 然后在myEditText.getText().toString()的帮助下。 I am getting the entered value as a string then I have to pass this data. 我将输入的值作为字符串,然后我必须传递此数据。


#1楼

参考:https://stackoom.com/question/M5u5/如何使用putExtra-和getExtra-来表示字符串数据


#2楼

first Screen.java 首先是Screen.java

text=(TextView)findViewById(R.id.tv1);
edit=(EditText)findViewById(R.id.edit);
button=(Button)findViewById(R.id.bt1);button.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {String s=edit.getText().toString();Intent ii=new Intent(MainActivity.this, newclass.class);ii.putExtra("name", s);startActivity(ii);}
});

Second Screen.java 第二个Screen.java

public class newclass extends Activity
{private TextView Textv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.intent);Textv = (TextView)findViewById(R.id.tv2);Intent iin= getIntent();Bundle b = iin.getExtras();if(b!=null){String j =(String) b.get("name");Textv.setText(j);}}
}

#3楼

Best Method... 最佳方法......

SendingActivity SendingActivity

Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
intent.putExtra("keyName", value);  // pass your values and retrieve them in the other Activity using keyName
startActivity(intent);

RecievingActivity RecievingActivity

 Bundle extras = intent.getExtras();if(extras != null)String data = extras.getString("keyName"); // retrieve the data using keyName

/// shortest way to recieve data.. ///接收数据的最短路径..

String data = getIntent().getExtras().getString("keyName","defaultKey");

//This requires api 12. //the second parameter is optional . //这需要api 12. //第二个参数是可选的。 If keyName is null then use the defaultkey as data. 如果keyName为null,则使用defaultkey作为数据。


#4楼

This is what i have been using, hopfully it helps someone.. simple and affective. 这就是我一直在使用的,它可以帮助某人......简单而有效。

send data 发送数据

    intent = new Intent(getActivity(), CheckinActivity.class);intent.putExtra("mealID", meal.Meald);startActivity(intent);

get data 获取数据

    int mealId;Intent intent = getIntent();Bundle bundle = intent.getExtras();if(bundle != null){mealId = bundle.getInt("mealID");}

cheers! 干杯!


#5楼

It is very easy to implement intent in Android.. It takes you to move from one activity to another activity,we have to two method putExtra(); 在Android中实现intent非常容易。它需要你从一个活动转移到另一个活动,我们必须有两个方法putExtra(); and getExtra(); getExtra(); Now I am showing you the example.. 现在我告诉你这个例子..

    Intent intent = new Intent(activity_registration.this, activity_Login.class);intent.putExtra("AnyKeyName", Email.getText().toString());  // pass your values and retrieve them in the other Activity using AnyKeyNamestartActivity(intent);

Now we have to get the value from AnyKeyName parameter,the below mentioned code will help in doing this 现在我们必须从AnyKeyName参数中获取值, AnyKeyName代码将有助于这样做

       String data = getIntent().getExtras().getString("AnyKeyName");textview.setText(data);

We can easily set the receiving value from Intent ,wherever we required. 无论我们需要什么,我们都可以轻松地从Intent设置接收值。


#6楼

More simple 更简单

sender side 发件人方

Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
i.putExtra("id","string data");
startActivity(i)

receiver side 接收方

Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
String strData = i.getStringExtra("id");

如何使用putExtra()和getExtra()来表示字符串数据相关推荐

  1. android intent.putextras,关于android:如何使用putExtra()和getExtra()来表示字符串数据

    有人可以告诉我如何使用getExtra()和putExtra()进行意图? 实际上我有一个字符串变量,比如str,它存储一些字符串数据. 现在,我想将这些数据从一个活动发送到另一个活动. Intent ...

  2. php字符串定义为arraylist,如何把arraylist集合中的字符串数据保存的文本文件中

    分析: (推荐教程:java课程) 通过题目的意思我们可以知道如下内容:ArrayList集合里存储的是字符串 遍历ArrayList集合,把数据获取 然后存储到文本文件中 文本文文件说明使用文本文件 ...

  3. oracle 数据有引号,oracle插入字符串数据时字符串中有'单引号问题

    使用insert into(field1,field2...) values('val1','val2'...)时,若值中有单引号时会报错. 处理方法:判断一下val1,val2中是否含有单引号,若含 ...

  4. pandas基于dataframe字符串数据列不包含特定字符串来筛选dataframe中的数据行(rows where values do not contain substring)

    pandas基于dataframe字符串数据列不包含(not contains)特定字符串来筛选dataframe中的数据行(rows where values do not contain subs ...

  5. pandas使用replace函数和正则表达式移除dataframe字符串数据列中头部指定模式字符串(Removing leading substring in dataframe)

    pandas使用replace函数和正则表达式移除dataframe字符串数据列中头部指定模式字符串(Removing leading substring in dataframe) 目录

  6. pandas基于dataframe字符串数据列包含(contains)特定字符串来筛选dataframe中的数据行(rows where values contain substring)

    pandas基于dataframe字符串数据列包含(contains)特定字符串来筛选dataframe中的数据行(rows where values contain substring) 目录

  7. pandas使用query函数基于dataframe字符串数据列中字符串的长度筛选dataframe的数据行(specific column string length)

    pandas使用query函数基于dataframe字符串数据列中字符串的长度筛选dataframe的数据行(select dataframe rows based on specific colum ...

  8. pandas移除dataframe字符串数据列中的前N个字符(remove the first n characters from values from column of dataframe)

    pandas移除dataframe字符串数据列中的前N个字符(remove the first n characters from values from str column of datafram ...

  9. pandas移除dataframe字符串数据列中的后N个字符(remove the last n characters from values from column of dataframe)

    pandas移除dataframe字符串数据列中的后N个字符(remove the last n characters from values from column of dataframe) 目录 ...

最新文章

  1. 重磅:2019年全国普通高校学科竞赛排行榜出炉!
  2. UA MATH636 信息论7 高斯信道简介
  3. windows中如何设置开机自启tomcat,nginx,jdk等应用服务的解决办法
  4. HNOI2013 游走
  5. java如何开发bpm系统_java工作流bpm开发ERP实例
  6. Some Essential JavaScript Questions And Answers(2)
  7. AcWing 204. 表达整数的奇怪方式 / Strange Way To Express Integers
  8. Centos 安装最新版git
  9. 【干货】Vray渲染器的使用方法
  10. 算法第四版练习题答案
  11. python软件怎么打开画图_Python实现画图软件功能
  12. 阿里百度腾讯facebookMicrosoftGoogle开源项目汇总
  13. 各国网络标识码表(MCC MNC表)
  14. Python爬取中国票房网所有电影片名和演员名字,爬取齐鲁网大陆所有电视剧名称...
  15. 三哥新发现了比金星还厉害的飞行物
  16. Android百度地图自定义添加Marker点
  17. 关于校园流浪猫狗的调查报告 新生研讨课校内调查
  18. 顾客细分(Customer Segmentation)(转载)
  19. 用汇编程序实现电子时钟
  20. Android开机启动检测和连接wifi检测

热门文章

  1. 在leangoo里怎么添加泳道?
  2. EGOTableViewPullRefresh实现下拉刷新
  3. 从网管做到CIO---看如何提升IT人员职业价值
  4. 20145335郝昊《网络攻防》Exp5 MS08_067漏洞测试
  5. [MongoDB]安装MongoDB遇到问题
  6. [译]git fetch
  7. ListView的Item点击事件(消息传递)
  8. 在Pocket PC/Smartphone智能设备上编写压缩程序(特别简单,任何人都能简单使用)...
  9. Centos定时备份 MySQL数据库
  10. HP服务器集成 iLO 端口的配置