android intent.putextras,关于android:如何使用putExtra()和getExtra()来表示字符串数据
有人可以告訴我如何使用getExtra()和putExtra()進(jìn)行意圖? 實(shí)際上我有一個(gè)字符串變量,比如str,它存儲(chǔ)一些字符串?dāng)?shù)據(jù)。 現(xiàn)在,我想將這些數(shù)據(jù)從一個(gè)活動(dòng)發(fā)送到另一個(gè)活動(dòng)。
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
}
}
我知道這是一個(gè)非常基本的問(wèn)題但不幸的是我被困在這里。
請(qǐng)幫忙。
謝謝,
編輯:這里我試圖從一個(gè)屏幕傳遞到另一個(gè)屏幕的字符串是動(dòng)態(tài)的。
那就是我有一個(gè)editText,我得到的字符串無(wú)論用戶類型如何。 然后在myEditText.getText().toString()的幫助下。 我將輸入的值作為字符串,然后我必須傳遞此數(shù)據(jù)。
i.putExtra(strName,keyIdentifer); 此語(yǔ)句具有strName變量,而bundle.getString("strName")具有"strName"字符串。 它的intent.putExtra(key,value)和intent.getExtras()。getString(key); 確保你在put和get中使用相同的鍵。
用它來(lái)"放"文件......
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String strName = null;
i.putExtra("STRING_I_NEED", strName);
然后,要檢索值,請(qǐng)嘗試以下方法:
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);
}
});
第二個(gè)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
///接收數(shù)據(jù)的最短路徑..
String data = getIntent().getExtras().getString("keyName","defaultKey");
//這需要api 12。
//第二個(gè)參數(shù)是可選的。如果keyName為null,則使用defaultkey作為數(shù)據(jù)。
這就是我一直在使用的,它可以幫助某人......簡(jiǎn)單而有效。
發(fā)送數(shù)據(jù)
intent = new Intent(getActivity(), CheckinActivity.class);
intent.putExtra("mealID", meal.Meald);
startActivity(intent);
獲取數(shù)據(jù)
int mealId;
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
mealId = bundle.getInt("mealID");
}
干杯!
我還是要時(shí)刻提醒自己,這是怎么做得好的。哈哈!
在Android中實(shí)現(xiàn)intent非常容易。它會(huì)將您從一個(gè)活動(dòng)轉(zhuǎn)移到另一個(gè)活動(dòng),我們需要兩個(gè)方法putExtra();和getExtra();現(xiàn)在我向您展示示例..
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);
現(xiàn)在我們必須從AnyKeyName參數(shù)中獲取值,下面提到的代碼將有助于這樣做
String data = getIntent().getExtras().getString("AnyKeyName");
textview.setText(data);
無(wú)論我們需要什么,我們都可以輕松地從intent設(shè)置接收值。
一個(gè)小附錄:你不必為密鑰創(chuàng)建自己的名字,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是否包含密鑰數(shù)據(jù)。
您可以直接使用getStringExtra()。
傳遞數(shù)據(jù)
intent.putExtra(PutExtraConstants.USER_NAME,"user");
獲取數(shù)據(jù)
String userName;
if (getIntent().hasExtra(PutExtraConstants.USER_NAME)) {
userName = getIntent().getStringExtra(PutExtraConstants.USER_NAME);
}
始終將鍵放在常量中作為最佳實(shí)踐。
public interface PutExtraConstants {
String USER_NAME ="USER_NAME";
}
為什么PutExtraConstants是一個(gè)接口?
@Big_Chair因?yàn)镻utExtraConstants類只包含常量(public,static,final)。 所以最好使用Constants接口。
更簡(jiǎn)單
發(fā)件人方
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");
簡(jiǎn)單,
在第一個(gè)活動(dòng)中 -
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);
}
});
在第二個(gè)活動(dòng)中 -
@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.
推送數(shù)據(jù)
import android.content.Intent;
...
Intent intent =
new Intent(
this,
MyActivity.class );
intent.putExtra("paramName","paramValue" );
startActivity( intent );
上面的代碼可能在主activity中。"MyActivity.class"是我們要發(fā)布的第二個(gè)activity;它必須明確包含在AndroidManifest.xml文件中。
拉數(shù)據(jù)
import android.os.Bundle;
...
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String myParam = extras.getString("paramName");
}
else
{
//..oops!
}
在此示例中,上面的代碼將位于MyActivity.java文件中。
陷阱
此方法只能傳遞strings。所以,假設(shè)您需要將ArrayList傳遞給ListActivity;一種可能的解決方法是傳遞逗號(hào)分隔字符串,然后在另一端分割它。
替代方案
使用SharedPreferences
如果我想從string.xml傳遞一個(gè)字符串怎么辦?
把功能
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對(duì)象中
Intent intent = new Intent(FirstActivity.this,NextAcitivity.class);
intent.putExtra("key",your_String);
StartActivity(intent);
onCreate方法中的NextAcitvity獲取String
String my_string=getIntent().getStringExtra("key");
這是簡(jiǎn)單而簡(jiǎn)短的方法
在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);
歡迎來(lái)到SO! 請(qǐng)編輯您的答案并詳細(xì)說(shuō)明解決問(wèn)題的原因和方法。 有關(guān)更多指導(dǎo),請(qǐng)參閱stackoverflow.com/help/how-to-answer
發(fā)送
startActivity(new Intent(First.this, Secend.class).putExtra("key",edit.getText.tostring));
得到
String myData = getIntent.getStringExtra("key");
您可以使用靜態(tài)變量來(lái)存儲(chǔ)edittext的字符串,然后在另一個(gè)類中使用該變量。希望這能解決你的問(wèn)題
首先放字符串
Intent secondIntent = new Intent(this, typeof(SecondActivity));
secondIntent.PutExtra("message","Greetings from MainActivity");
之后檢索它
var message = this.Intent.GetStringExtra("message");
就這樣 ;)
總結(jié)
以上是生活随笔為你收集整理的android intent.putextras,关于android:如何使用putExtra()和getExtra()来表示字符串数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android volley 上传图片
- 下一篇: android+tv+无线键盘,手机键盘