如何使用putExtra()和getExtra()作为string数据
有人可以告诉我如何使用getExtra()和putExtra()的意图。 其实我有一个stringvariables说str,它存储了一些string数据。 现在我想把这个数据从一个活动发送到另一个活动。 
  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 } } 
我知道这是非常基本的问题,但不幸的是我被困在这里。 请帮忙。
谢谢,
 编辑:这里我试图从一个屏幕传递到另一个string是dynamic的。 这是我有一个editText我得到string的用户types。 然后在myEditText.getText().toString()的帮助下。 我得到input的值作为一个string,然后我必须通过这些数据。 
用这个“放”文件…
 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 
///收到数据的最短path
 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参数中获取值, 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"); 
 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"); } 
放function
 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(); } } 
推送数据
 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 ; 一个可能的解决方法是传递一个逗号分隔的string,然后在另一边分割它。 
替代scheme
 使用SharedPreferences 
简单,在第一个活动 –
  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); } }); 
在第二个Activity-
  @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条件。
先把string放
 Intent secondIntent = new Intent(this, typeof(SecondActivity)); secondIntent.PutExtra("message", "Greetings from MainActivity"); 
之后检索它
 var message = this.Intent.GetStringExtra("message"); 
就这样 ;)