生活随笔
收集整理的這篇文章主要介紹了
android数据存储_SharedPreferences的简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SharedPreferences的寫數據簡單使用步驟如下:
第一步:獲取SharedPreferences對象
第二步:獲取SharedPreferences.Editor對象
第三步:通過此Editor對象來寫入數據
第四步:通過此Editor對象來提交數據寫入文件。
SharedPreferences的讀數據簡單使用步驟如下:
第一步:獲取SharedPreferences對象
第二步:通過此對象來讀數據。
注:
1、通過SharedPreferences可以將String類型數據和基本類型數據以XML格式存入到本地系統路徑。
2、這種方式寫入文件和讀文件,不需要在manifest.xml配置中添加讀寫權限,因為它沒有涉及到外部存儲器。
[java]?view plaincopy
public?class?MainActivity?extends?Activity?{?? ????TextView?t;?? ????EditText?e;?? ????SharedPreferences?sp?;?? ????SharedPreferences.Editor?editor;?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ????????sp?=?getSharedPreferences("test",?Activity.MODE_PRIVATE);?? ????????editor?=?sp.edit();?? ??????????? ????????t?=?(TextView)?findViewById(R.id.textView);?? ????????e?=?(EditText)?findViewById(R.id.editText);?? ?? ?????????? ????????Button?w?=?(Button)?findViewById(R.id.write);?? ????????w.setOnClickListener(new?OnClickListener()?{?? ????????????public?void?onClick(View?v)?{?? ????????????????editor.putString("content",?e.getText().toString());?? ????????????????editor.commit();?? ????????????}?? ????????});?? ?????????? ????????Button?r?=?(Button)?findViewById(R.id.read);?? ????????r.setOnClickListener(new?OnClickListener()?{?? ?? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ????????????????String?content?=?sp.getString("content",?"");?? ????????????????t.setText(content);?? ????????????}?? ????????});?? ????}?? } ?
總結
以上是生活随笔為你收集整理的android数据存储_SharedPreferences的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。