![](/img/trans.png)
[英]Send data from one Activity to another Activity using Intents
[英]Transfer data from one Activity to Another Activity Using Intents
我希望能夠將數據從一項活動傳輸到另一項活動。 如何做到這一點?
通過下面的代碼,我們可以在活動之間發送值
在父活動中使用以下代碼
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
在子活動中使用以下代碼
String s= getIntent().getStringExtra(<StringName>);
您可以通過多種方式訪問其他類或活動中的變量或對象。
A. 數據庫
B. 共享偏好。
C. 對象序列化。
D. 可以保存公共數據的類可以命名為 Common Utilities,這取決於您。
E. 通過 Intents 和 Parcelable 接口傳遞數據。
這取決於您的項目需求。
A.數據庫
SQLite 是一個嵌入到 Android 中的開源數據庫。 SQLite 支持標准的關系數據庫功能,如 SQL 語法、事務和准備好的語句。
教程 -- http://www.vogella.com/articles/AndroidSQLite/article.html
B.共享偏好
假設您要存儲用戶名。 所以現在會有兩個東西,一個關鍵用戶名,一個值值。
如何儲存
// Create object of SharedPreferences.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putString("userName", "stackoverlow");
//commits your edits
editor.commit();
使用 putString(),putBoolean(),putInt(),putFloat(),putLong() 你可以保存你想要的數據類型。
如何獲取
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");
http://developer.android.com/reference/android/content/SharedPreferences.html
C.對象序列化
如果我們想保存對象狀態以通過網絡發送它,或者您也可以將其用於您的目的,則使用對象序列化。
使用 java bean 並將其作為他的字段之一存儲在其中,並為此使用 getter 和 setter
JavaBean 是具有屬性的 Java 類。 將屬性視為私有實例變量。 由於它們是私有的,因此可以從類外部訪問它們的唯一方法是通過類中的方法。 更改屬性值的方法稱為 setter 方法,檢索屬性值的方法稱為 getter 方法。
public class VariableStorage implements Serializable {
private String inString ;
public String getInString() {
return inString;
}
public void setInString(String inString) {
this.inString = inString;
}
}
通過使用在您的郵件方法中設置變量
VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);
然后使用對象序列化來序列化這個對象,並在你的其他類中反序列化這個對象。
在序列化中,對象可以表示為字節序列,其中包括對象的數據以及有關對象類型和存儲在對象中的數據類型的信息。
序列化對象寫入文件后,可以從文件中讀取並反序列化,即表示對象及其數據的類型信息和字節可用於在內存中重新創建對象。
如果您需要此教程,請參閱此鏈接
http://javawithswaranga.blogspot.in/2011/08/serialization-in-java.html
D.公用事業
您可以自己創建一個類,其中包含您在項目中經常需要的常用數據。
樣品
public class CommonUtilities {
public static String className = "CommonUtilities";
}
E.通過 Intent 傳遞數據
有關傳遞數據的選項,請參閱本教程。
當您將數據從一個活動傳遞到另一個活動時,請執行如下操作
在父活動中:
startActivity(new Intent(presentActivity.this, NextActivity.class).putExtra("KEY_StringName",ValueData));
或如下所示的父活動
Intent intent = new Intent(presentActivity.this,NextActivity.class);
intent.putExtra("KEY_StringName", name);
intent.putExtra("KEY_StringName1", name1);
startActivity(intent);
在子活動中執行如下所示
TextView tv = ((TextView)findViewById(R.id.textViewID))
tv.setText(getIntent().getStringExtra("KEY_StringName"));
或者像下面顯示的子活動
TextView tv = ((TextView)findViewById(R.id.textViewID));
TextView tv1 = ((TextView)findViewById(R.id.textViewID1))
/* Get values from Intent */
Intent intent = getIntent();
String name = intent.getStringExtra("KEY_StringName");
String name1 = intent.getStringExtra("KEY_StringName1");
tv.setText(name);
tv.setText(name1);
在android中將數據從一個活動傳遞到另一個活動
Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);
從 android 活動中檢索包數據
Intent intent = getIntent();
if (intent!=null) {
String stringData= intent.getStringExtra(KEY);
int numberData = intent.getIntExtra(KEY, defaultValue);
boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
char charData = intent.getCharExtra(KEY, defaultValue); }
希望你能從這里找到答案發送數據到另一個活動 - 簡單的 Android 登錄
你只需要在調用你的意圖時發送額外內容
像這樣:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class); intent.putExtra("變量名","要傳遞的值"); 開始活動(意圖);
現在在你的 SecondActivity 的 OnCreate 方法上,你可以像這樣獲取額外的東西
如果您發送的值是“long”
long value = getIntent().getLongExtra("你額外發送的變量名", defaultValue(你可以給它任何東西));
如果您發送的值是“字符串”
String value = getIntent().getStringExtra("你額外發送的變量名");
如果您發送的值是“布爾值”
Boolean value = getIntent().getStringExtra("你額外發送的變量名",defaultValue);
假設您想從Activity A 轉到 Activity B 。
所以我們使用一個 Intent 來切換 Activity
典型的代碼看起來像這樣 -
//// Create a New Intent object
Intent i = new Intent(getApplicationContext(), B.class);
/// add what you want to pass from activity A to Activity B
i.putExtra("key", "value");
/// start the intent
startActivity(i);
並從子活動中獲取數據
Intent i = getIntent();
if (i!=null) {
String stringData= i.getStringExtra("key");
}
這效果最好:
通過下面的代碼,我們可以在活動之間發送值
在父活動中使用以下代碼(父類/值發送類)
Intent myintent=new Intent(<PARENTCLASSNAMEHERE>.this,<TARGETCLASSNAMEHERE>.class).putExtra("<StringName>", value);
startActivity(myintent);
在子活動(TARGET CLASS/ACTIVITY)中使用以下代碼
String s= getIntent().getStringExtra(<StringName>);
請在此處看到“StringName”是目標/子活動捕獲的名稱,而“值”是變量名稱,與父/目標/發送類中的相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.