Android下保存數(shù)據(jù)到SharePreference和從SharePreference讀取數(shù)據(jù)
2018-07-20 來源:open-open

Android提供了SharePreference方便程序存儲自己的key-value數(shù)據(jù),使用非常方便,不用自己再卻寫文件了。通過SharePreference保存的文件為一個標(biāo)準(zhǔn)的xml文件。
public void saveToSharedPreference(View v){ sp = this.getSharedPreferences("config.txt", Context.MODE_PRIVATE); //得到sharedpreference的編輯器 SharedPreferences.Editor editor = sp.edit(); editor.putString("title","www.sharejs.com"); editor.putBoolean("success",true); //提交數(shù)據(jù) editor.commit(); Toast.makeText(this,"保存到sharedpreference成功",Toast.LENGTH_SHORT).show(); //讀取SharePreference的內(nèi)容 String tel = sp.getString("title","沒有獲取到值"); Toast.makeText(this,tel,Toast.LENGTH_SHORT).show(); }
標(biāo)簽:
版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。
最新資訊
熱門推薦