Android編程之SharedPreferences文件存儲(chǔ)操作實(shí)例分析
本文實(shí)例講述了Android編程之SharedPreferences文件存儲(chǔ)操作的方法。分享給大家供大家參考。具體分析如下:
SharedPreferences類提供了一種簡(jiǎn)單的文件存儲(chǔ)功能,像程序的配置文件可以通過(guò)它來(lái)實(shí)現(xiàn)。
源代碼:
package com.test.sharedpreferences;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class test_sharedpreferences extends Activity
{
SharedPreferences file;
SharedPreferences.Editor editor;
TextView text;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲得配置文件
file = getSharedPreferences("test_config", 0);
editor = file.edit();
//寫入配置:Id:10000 Name:Jdh
editor.putInt("Id", 10000);
editor.putString("Name", "Jdh");
editor.commit();
//讀取配置
int id = 0;
id = file.getInt("Id", 0);
String str;
str = file.getString("Name", null);
text = (TextView)findViewById(R.id.textView1);
text.setText("Id:" + id + " Name:" + str);
}
}
效果圖如下:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- 詳解Android數(shù)據(jù)存儲(chǔ)之Android 6.0運(yùn)行時(shí)權(quán)限下文件存儲(chǔ)的思考
- Android實(shí)現(xiàn)文件存儲(chǔ)并讀取的示例代碼
- android數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)方法
- android開(kāi)發(fā)基礎(chǔ)教程—文件存儲(chǔ)功能實(shí)現(xiàn)
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- 實(shí)例詳解Android文件存儲(chǔ)數(shù)據(jù)方式
- 詳解Android開(kāi)發(fā)數(shù)據(jù)持久化之文件存儲(chǔ)(附源碼)
- Android學(xué)習(xí)之文件存儲(chǔ)讀取
- 詳解Android文件存儲(chǔ)
- Android開(kāi)發(fā)文件存儲(chǔ)實(shí)例
相關(guān)文章
Android 安全加密:數(shù)字簽名和數(shù)字證書詳解
本文主要介紹Android 安全加密數(shù)字簽名和數(shù)字證書的資料,這里整理詳細(xì)的資料及數(shù)字簽名和數(shù)字證書應(yīng)用詳解,有需要的小伙伴可以參考下2016-09-09
Android開(kāi)發(fā)實(shí)現(xiàn)圖片圓角的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片圓角的方法,涉及Android針對(duì)圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
使用newInstance()來(lái)實(shí)例化fragment并傳遞數(shù)據(jù)操作
這篇文章主要介紹了使用newInstance()來(lái)實(shí)例化fragment并傳遞數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Material Design系列之Behavior實(shí)現(xiàn)Android知乎首頁(yè)
這篇文章主要為大家詳細(xì)介紹了Material Design系列之Behavior實(shí)現(xiàn)Android知乎首頁(yè)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android SQLite數(shù)據(jù)庫(kù)基本操作方法
本篇文章主要介紹了Android SQLite數(shù)據(jù)庫(kù)基本操作方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
android中強(qiáng)制更新app實(shí)例代碼
本篇文章主要介紹了android中強(qiáng)制更新app實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Android中Handler實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
本篇文章主要介紹了Android中Handler實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

