[英]Shared preferences from other activity to be call with button
我是一個初學者,試圖了解sharedpreferences
。 當我的共享首選項程序運行時,一切進展順利。
我的輸入在活動1中,使用共享的首選項,我在活動2中將其回叫。
但是, 如何僅通過使用活動2中的按鈕來使用共享的首選項將活動1中的輸入稱為活動3 ?
將sharedpreference
存儲到Constant類,並使用靜態變量而不是set並在需要時從該類獲取值。
在首選項中設置值:
MY_PREFS_NAME - a static String variable like:
public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Amit");
editor.putInt("idName", 888);
editor.commit();
從首選項中檢索數據:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined"); //"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
檢查此答案以獲取更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.