如何对字符串数据使用putExtra()和getExtra()有谁能告诉我如何准确地使用getExtra()和putExtra()为了目的?实际上,我有一个字符串变量,比如说str,它存储一些字符串数据。现在,我想将这些数据从一个活动发送到另一个活动。 Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra(strName, keyIdentifer );然后在SecdScreen.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
}
}我知道这是一个非常基本的问题,但不幸的是,我被困在这里了。请帮帮忙。谢谢,编辑:在这里,我试图从一个屏幕传递到另一个屏幕的字符串是动态的。也就是说,我有一个edtext,在其中我可以得到任何用户类型的字符串。然后在.的帮助下myEditText.getText().toString()..我将输入的值作为字符串,然后必须传递这些数据。
3 回答
![?](http://img1.sycdn.imooc.com/545845b40001de9902200220-100-100.jpg)
慕莱坞森
TA贡献1810条经验 获得超4个赞
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");}
![?](http://img1.sycdn.imooc.com/545867280001ed6402200220-100-100.jpg)
呼唤远方
TA贡献1856条经验 获得超11个赞
Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);intent.putExtra("keyName", value); // pass your values and retrieve them in the other Activity using keyNamestartActivity(intent);
Bundle extras = intent.getExtras(); if(extras != null) String data = extras.getString("keyName"); // retrieve the data using keyName
String data = getIntent().getExtras().getString("keyName","defaultKey");
defaultkey
- 3 回答
- 0 关注
- 2559 浏览
添加回答
举报
0/150
提交
取消