android開發(fā)中,可能需要持久化少量的數(shù)據(jù),譬如說一些用戶設置的數(shù)據(jù),用數(shù)據(jù)庫就有點殺雞用牛刀了,這時候可以嘗試使用SharedPreferences。SharedPreferences是一種輕量級的存儲數(shù)據(jù)的方式,使用xml文件來進行鍵值對方式的存儲,而且使用起來非常輕量和快捷。 得到SharedPreferences: SharedPreferences sharedPreferences = getSharedPreferences( "usable_sharedPreferences", Context.MODE_PRIVATE); SharedPreferences使用一個內(nèi)部類SharedPreferences.Editor來對數(shù)據(jù)進行編輯 得到edit: SharedPreferences.Editor editor = sharedPreferences.edit(); 提交數(shù)據(jù): editor.putString(key, value); editor.commit(); 查詢數(shù)據(jù): String value = sharedPreferences.getString(key, "沒有找到鍵:" + key + " 對應的值,請檢查輸入的鍵!"); 清空數(shù)據(jù): editor.clear().commit(); 本文出自 “清源教育-android技術” 博客,請務必保留此出處http://blog.sina.com.cn/s/blog_b3801c8f0101lh7h.html,更多安卓技術問題歡迎加群探討:278744577,驗證碼:eec,不寫驗證不予通過喲~ |