android學習十三 喜好設定

來源:互聯網
上載者:User

標籤:

1,喜好設定可用用來持久儲存使用者佈建,遊戲最高分等

2,喜好設定有,列表喜好設定,複選框喜好設定,對話方塊喜好設定..3,通過xml檔案和代碼建立喜好設定     addPreferencesFromResource(R.xml.main)4.從喜好設定中擷取值   Resources resources = this.getResources();              SharedPreferences spf= PreferenceManager.getDefaultSharedPreferences( this);              String option = spf.getString(resources.getString(R.string.FLIGHT_SORT_KEY),                            resources.getString(R.string.FLIGHT_SORT_DEFAULT_VALUE ));                            String[] optionArray = resources.getStringArray(R.array.filght_sort_option );5.判斷是否存在某個喜好設定的值 boolean hasPreferences = myPrefs.getBoolean(INITIALIZED, false);
if(hasPreferences) {
Log.v("Preferences", "We‘ve been called before");
// Read other values as desired from preferences file...
someString = myPrefs.getString("someString", "");
}
else {
Log.v("Preferences", "First time ever being called");
// Set up initial values for what will end up
// in the preferences file
someString = "some default value";
}
// Later when ready to write out values 6.在代碼中設定喜好設定的值Editor editor = myPrefs.edit();
CHAPTER 13: Working with Preferences and Saving State  359
editor.putBoolean(INITIALIZED, true);
editor.putString("someString", someString);
// Write other values as desired
editor.commit();  7.喜好設定可用嵌套,可以存在父子關係。    8一般步驟   a.在xml檔案中布局喜好設定  b.建立活動繼承自PreferenceActivity,然後通過   addPreferencesFromResource() 建立視圖  c.通過相應的方法擷取和設定值   

android學習十三 喜好設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.