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

本文详细解答了如何在Android应用中使用getExtra()和putExtra()方法传递字符串数据,包括EditText输入值的处理、Intent间的数据传递和接收,以及最佳实践和常见陷阱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有人可以告诉我如何使用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");

就这样 ;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值