如何使用putExtra()和getExtra()字符串数据

有人能告诉我到底如何使用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
}


}
我知道这是一个非常基本的问题,但不幸的是我被困在这里了。 请帮助。< / p >

谢谢,

编辑:这里的字符串,我试图从一个屏幕传递到另一个是动态的。 也就是我有一个editText,在那里我获取字符串,不管用户类型是什么。然后在myEditText.getText().toString()的帮助下。我得到输入的值作为字符串,然后我必须传递这个数据

912168 次浏览

使用这个来“放置”文件…

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");
}

第一个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为空,则使用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();和__abc2现在我向你展示的例子。

    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设置接收值,无论我们需要的是什么。

更简单

发送方

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

把数据

import android.content.Intent;


...


Intent intent =
new Intent(
this,
MyActivity.class );
intent.putExtra( "paramName", "paramValue" );
startActivity( intent );

上面的代码可能在主activity中。"MyActivity.class"是我们想要启动的第二个Activity;它必须显式包含在你的AndroidManifest.xml文件中。

<activity android:name=".MyActivity" />

提取数据

import android.os.Bundle;


...


Bundle extras = getIntent().getExtras();
if (extras != null)
{
String myParam = extras.getString("paramName");
}
else
{
//..oops!
}

在这个例子中,上面的代码将在你的MyActivity.java文件中。

陷阱

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

替代方案

使用SharedPreferences

把函数

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();
}
}
< p >简单, 在第一个活动中-

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

如果需要,可以添加if/else条件。

先放绳子

Intent secondIntent = new Intent(this, typeof(SecondActivity));
secondIntent.PutExtra("message", "Greetings from MainActivity");

在此之后检索它

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

就这些;)

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");
}

一个小补充:你不需要为键创建自己的名字,android提供了这些,f.ex。Intent.EXTRA_TEXT。修改接受的答案:

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

然后,要检索值,请尝试如下操作:

String newString;
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString(Intent.EXTRA_TEXT);
}

意图类中更新。

  • 使用hasExtra()检查intent是否在key上有数据。
  • 你现在可以直接使用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";
}

在意图对象中放入字符串

  Intent intent = new Intent(FirstActivity.this,NextAcitivity.class);
intent.putExtra("key",your_String);
StartActivity(intent);

onCreate方法中的NextAcitvity获取字符串

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

那是一种简单易行的方法

发送

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

得到

String myData = 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);
对于这个任务,一个内联代码就足够了。这对我来说毫不费力。 面向android开发者。 String strValue=Objects.requireNonNull(getIntent().getExtras()).getString("string_Key"); < / p >