簡體   English   中英

通過按鈕可以調用其他活動的共享首選項

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM