編輯:關於Android編程
SharedPreferences是一種輕型的Android數據存儲方式,它的本質是基於XML文件存儲key-value鍵值對數據,通常用來存儲一些簡單的配置信息。其存儲位置在/data/data/<包名>/shared_prefs目錄下。SharedPreferences對象本身只能獲取數據而不支持存儲和修改,存儲修改是通過Editor對象實現。比較經典的使用方式例如用戶輸入框對過往登錄賬戶的存儲。實現SharedPreferences存儲的步驟如下:
public abstract SharedPreferences getSharedPreferences (String name, int mode)
方法得到一個sharedpreferences對象,參數name是preference文件的名字,mode則是方式,默認為0。
Editor可用於SharedPreferences數據的添加,刪除,修改和查詢。
Public abstract SharedPreferences.Editor putString (String key,String value)
通過執行commit()或是apply()方法,將會應用更改。
// 存儲sharedpreferences
public void setSharedPreference() {
sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("username", text1.getText().toString());
editor.putInt("password", getpw());
editor.commit();// 提交修改
}
// 清除sharedpreferences的數據
public void removeSharedPreference() {
sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.remove("username");
editor.remove("password");
editor.commit();// 提交修改
}
// 獲得sharedpreferences的數據
public void getSahrePreference() {
String username = sharedPreferences.getString("username", "");
int password = sharedPreferences.getInt("password", 0);
String str = String.valueOf(password);
text1.setText(username);
text2.setText(str);
}
玩轉android之Action bar
玩轉android之Action bar 背景: 在Android3.0之後,Google對UI導航設計上進行了一系列的改革,其中有一個非常好用的新功能就是
Android實現語音識別代碼
蘋果的iphone 有語音識別用的是Google 的技術,做為Google 力推的Android 自然會將其核心技術往Android 系統裡面植入,並結合google 的
Android學習筆記二十九之SwipeRefreshLayout、RecyclerView和CardView
前面我們介紹了AlertDialog和幾個常用的Dialog,ProgressDialog進度條提示框、DatePickerDialog日期選擇對話框和TimePicke
微信個人公眾號怎麼改名字 微信個人公眾號改名字教程
近日,微信悄然上線了個人公眾號改名功能,據悉,微信在原賬號名稱下方會有一行“個人類賬號一年內可主動修改一次名稱”的標注,每年只有一次