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

Intent i = new Intent(FirstScreen.this, SecondScreen.class);

String keyIdentifer  = null;

i.putExtra(strName, keyIdentifer );

然后在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

}

}

我知道这是一个非常基本的问题但不幸的是我被困在这里。

请帮忙。

谢谢,

编辑:这里我试图从一个屏幕传递到另一个屏幕的字符串是动态的。

那就是我有一个editText,我得到的字符串无论用户类型如何。 然后在myEditText.getText().toString()的帮助下。 我将输入的值作为字符串,然后我必须传递此数据。

i.putExtra(strName,keyIdentifer); 此语句具有strName变量,而bundle.getString("strName")具有"strName"字符串。 它的intent.putExtra(key,value)和intent.getExtras()。getString(key); 确保你在put和get中使用相同的键。

用它来"放"文件......

Intent i = new Intent(FirstScreen.this, SecondScreen.class);

String strName = null;

i.putExtra("STRING_I_NEED", strName);

然后,要检索值,请尝试以下方法:

String newString;

if (savedInstanceState == null) {

Bundle extras = getIntent().getExtras();

if(extras == null) {

newString= null;

} else {

newString= extras.getString("STRING_I_NEED");

}

} else {

newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");

}

是用于处理方向变化的"savedInstanceState ..."和"... getSerialiable"代码? 如果不是,该代码用于什么?

我使用Android 3.0.1,我不得不使用this.getActivity().getIntent().getExtras()。

首先是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);

}

});

第二个Screen.java

public class newclass extends Activity

{

private TextView Textv;

@Override

protected 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);

}

}

}

最佳方法......

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

Bundle extras = intent.getExtras();

if(extras != null)

String data = extras.getString("keyName"); // retrieve the data using keyName

///接收数据的最短路径..

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

//这需要api 12。

//第二个参数是可选的。如果keyName为null,则使用defaultkey作为数据。

这就是我一直在使用的,它可以帮助某人......简单而有效。

发送数据

intent = new Intent(getActivity(), CheckinActivity.class);

intent.putExtra("mealID", meal.Meald);

startActivity(intent);

获取数据

int mealId;

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

if(bundle != null){

mealId = bundle.getInt("mealID");

}

干杯!

我还是要时刻提醒自己,这是怎么做得好的。哈哈!

在Android中实现intent非常容易。它会将您从一个活动转移到另一个活动,我们需要两个方法putExtra();和getExtra();现在我向您展示示例..

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 AnyKeyName

startActivity(intent);

现在我们必须从AnyKeyName参数中获取值,下面提到的代码将有助于这样做

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

textview.setText(data);

无论我们需要什么,我们都可以轻松地从intent设置接收值。

一个小附录:你不必为密钥创建自己的名字,android提供这些,f.ex。 Intent.EXTRA_TEXT。修改接受的答案:

Intent i = new Intent(FirstScreen.this, SecondScreen.class);

String strName = null;

i.putExtra(Intent.EXTRA_TEXT, strName);

Then, to retrieve the value try something like:

String newString;

Bundle extras = getIntent().getExtras();

if(extras == null) {

newString= null;

} else {

newString= extras.getString(Intent.EXTRA_TEXT);

}

Intent intent = new Intent(view.getContext(), ApplicationActivity.class);

intent.putExtra("int", intValue);

intent.putExtra("Serializable", object);

intent.putExtra("String", stringValue);

intent.putExtra("parcelable", parObject);

startActivity(intent);

ApplicationActivity

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

if(bundle != null){

int mealId = bundle.getInt("int");

Object object = bundle.getSerializable("Serializable");

String string = bundle.getString("String");

T string = < T >bundle.getString("parcelable");

}

在Intent类中更新。

使用hasExtra()检查intent是否包含密钥数据。

您可以直接使用getStringExtra()。

传递数据

intent.putExtra(PutExtraConstants.USER_NAME,"user");

获取数据

String userName;

if (getIntent().hasExtra(PutExtraConstants.USER_NAME)) {

userName = getIntent().getStringExtra(PutExtraConstants.USER_NAME);

}

始终将键放在常量中作为最佳实践。

public interface PutExtraConstants {

String USER_NAME ="USER_NAME";

}

为什么PutExtraConstants是一个接口?

@Big_Chair因为PutExtraConstants类只包含常量(public,static,final)。 所以最好使用Constants接口。

更简单

发件人方

Intent i = new Intent(SourceActiviti.this,TargetActivity.class);

i.putExtra("id","string data");

startActivity(i)

接收方

Intent i = new Intent(SourceActiviti.this,TargetActivity.class);

String strData = i.getStringExtra("id");

简单,

在第一个活动中 -

EditText name= (EditText) findViewById(R.id.editTextName);

Button button= (Button) findViewById(R.id.buttonGo);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent i = new Intent(MainActivity.this,Main2Activity.class);

i.putExtra("name",name.getText().toString());

startActivity(i);

}

});

在第二个活动中 -

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

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

Bundle bundle=getIntent().getExtras();

String s=bundle.getString("name");

t.setText(s);

}

You can add if/else conditions if you want.

推送数据

import android.content.Intent;

...

Intent intent =

new Intent(

this,

MyActivity.class );

intent.putExtra("paramName","paramValue" );

startActivity( intent );

上面的代码可能在主activity中。"MyActivity.class"是我们要发布的第二个activity;它必须明确包含在AndroidManifest.xml文件中。

拉数据

import android.os.Bundle;

...

Bundle extras = getIntent().getExtras();

if (extras != null)

{

String myParam = extras.getString("paramName");

}

else

{

//..oops!

}

在此示例中,上面的代码将位于MyActivity.java文件中。

陷阱

此方法只能传递strings。所以,假设您需要将ArrayList传递给ListActivity;一种可能的解决方法是传递逗号分隔字符串,然后在另一端分割它。

替代方案

使用SharedPreferences

如果我想从string.xml传递一个字符串怎么办?

把功能

etname=(EditText)findViewById(R.id.Name);

tvname=(TextView)findViewById(R.id.tvName);

b1= (ImageButton) findViewById(R.id.Submit);

b1.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

String s=etname.getText().toString();

Intent ii=new Intent(getApplicationContext(), MainActivity2.class);

ii.putExtra("name", s);

Toast.makeText(getApplicationContext(),"Page 222", Toast.LENGTH_LONG).show();

startActivity(ii);

}

});

getfunction

public class MainActivity2 extends Activity {

TextView tvname;

EditText etname;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_activity2);

tvname = (TextView)findViewById(R.id.tvName);

etname=(EditText)findViewById(R.id.Name);

Intent iin= getIntent();

Bundle b = iin.getExtras();

if(b!=null)

{

String j2 =(String) b.get("name");

etname.setText(j2);

Toast.makeText(getApplicationContext(),"ok",Toast.LENGTH_LONG).show();

}

}

将字符串放入Intent对象中

Intent intent = new Intent(FirstActivity.this,NextAcitivity.class);

intent.putExtra("key",your_String);

StartActivity(intent);

onCreate方法中的NextAcitvity获取String

String my_string=getIntent().getStringExtra("key");

这是简单而简短的方法

在FirstScreen.java

Intent intent = new Intent(FirstScreen.this, SecondScreen.class);

String keyIdentifier = null;

intent.putExtra(strName, keyIdentifier);

在SecondScreen.java

String keyIdentifier;

if (savedInstanceState != null)

keyIdentifier= (String) savedInstanceState.getSerializable(strName);

else

keyIdentifier = getIntent().getExtras().getString(strName);

欢迎来到SO! 请编辑您的答案并详细说明解决问题的原因和方法。 有关更多指导,请参阅stackoverflow.com/help/how-to-answer

发送

startActivity(new Intent(First.this, Secend.class).putExtra("key",edit.getText.tostring));

得到

String myData = getIntent.getStringExtra("key");

您可以使用静态变量来存储edittext的字符串,然后在另一个类中使用该变量。希望这能解决你的问题

首先放字符串

Intent secondIntent = new Intent(this, typeof(SecondActivity));

secondIntent.PutExtra("message","Greetings from MainActivity");

之后检索它

var message = this.Intent.GetStringExtra("message");

就这样 ;)

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

  1. android intent铃声选择,Android 设置系统铃声和系统音量

    android系统铃声设置 public class Main extends Activity { /** Called when the activity is first created. */ ...

  2. android intent 源码,Android 基础之 IntentService 源码

    Android 基础之 IntentService 源码 Android,IntentService,源码 IntentService 位于 android.app 包下面,是 Service 的一个 ...

  3. android intent短信,android – 通过Intent发送短信,并知道短信是否已被发送

    在以下示例中,我们使用ContentObserver来监视SMS提供程序的更新.此Observer在SMS Intent被触发之前创建并启动,并根据目标地址检查Provider更改.创建Observe ...

  4. android intent { (has extras) },android – 活动开始时缺少Intent extras

    在广播接收器中,我想启动我的应用程序(Activity)并传入一些数据. 我的问题是额外的东西似乎没有延续到活动中.我试图获取onNewIntent(Intent i)函数内的数据. 有任何想法吗? ...

  5. android intent服务器,使用android intent从服务器打开pdf文件

    首先创建一个下载器类 public class Downloader { public static void DownloadFile(String fileURL, File directory) ...

  6. android intent.action pick,android intent pick

    (一).调用本地联系人: Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contact ...

  7. Android Intent Action 大全

    1.Intent的用法: (1)Action跳转 1. 使用Action跳转,当程序AndroidManifest.xml中某一个 Activity的IntentFilter定义了包含Action,如 ...

  8. android intent 大全

    android intent和intent action大全 1.Intent的用法: (1)用Action跳转 1.使用Action跳转,如果有一个程序的AndroidManifest.xml中的某 ...

  9. Android——Intent动作汇总(转)

    String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. "android.intent.action.ADD_SHORTCUT"  String ...

最新文章

  1. html主动发起重新布局,重启连不上网
  2. markdown转html
  3. artdialog5 bug
  4. “相当一部分”HPE公司OpenStack工作人员被转移至SUSE
  5. Zabbix 配置钉钉脚本告警(4)
  6. PHP笔记-PHP中Web Service.
  7. [译]理解 Node.js 事件驱动架构
  8. 数字开头的字符可能有冒号可能没有,以分号分隔成数组
  9. 统计学常用的数据分析方法总结
  10. 【Shashlik.EventBus】.NET 事件总线,分布式事务最终一致性简介
  11. Python从入门到实战,我觉着拥有这三本书很有必要
  12. C++右值引用与函数返回值
  13. 1.12 将工作簿导出为PDF文件 [原创Excel教程]
  14. RXSwift进阶:RXSwift的底层核心逻辑解析
  15. 计算机社团活动教学计划,趣味科学社团教学计划记录、总结.doc
  16. 心若改变,态度就会改变;态度改变,习惯就改变;习惯改变,人生就会改变
  17. 英语不好,英语不好真的可以学编程吗?亲身经历告诉你
  18. 八数码 BFS+HASH
  19. 用omnetpp仿真dsr协议
  20. JAVA课程分为几部分_语文课程目标体系可以分为几个学段?

热门文章

  1. Python lambda表达式
  2. zookeeper安装包下载地址
  3. 大剑无锋之UNION 和 UNION ALL (SQL)【面试推荐】
  4. 一个略显复杂的transformation算子_distinct
  5. 【python】python第二周作业
  6. 一张PDF了解JDK10 GC调优秘籍-附PDF下载
  7. Hadoop2.2.0+HA+zookeeper3.4.5详细配置过程+错误处理(一)
  8. Flume 1.7 源码分析(四)从Source写数据到Channel
  9. 2016电大计算机网考,2016年电大-电大计算机网考题库[].doc
  10. tf调不到keras怎么 回事_拼多多刷单关键词搜不到是怎么回事?如何解决?