android小功能實(shí)現(xiàn)之簡(jiǎn)單數(shù)據(jù)持久化保存(SharedPreferences)
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-04-08 08:40:20 閱讀次數(shù):3068次
為了保存1些簡(jiǎn)單的配置,類(lèi)似iOS的NSUserDefault和cocos2dx的CCUserDefault,Android提供了SharedPreferences。
1 布局
先看效果圖:
打開(kāi)main.xml修改內(nèi)容以下:
<?xml version="1.0" encoding="utf⑻"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_name" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/input_name" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_age" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:id="@+id/input_age" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_save"
android:onClick="save"
android:id="@+id/button_save"/>
</LinearLayout>
2 定義字符串
打開(kāi)strings.xml添加內(nèi)容以下:
<string name="input_name">姓名</string>
<string name="input_age">年齡</string>
<string name="button_save">保存參數(shù)</string>
<string name="success">保存成功</string>
<string name="fail">保存失敗</string>
3 功能實(shí)現(xiàn)
修改MainActivity.java代碼以下:
private EditText nameText;
private EditText ageText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText)this.findViewById(R.id.input_name);
ageText = (EditText)this.findViewById(R.id.input_age);
// 讀取保存的值
//SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);// 默許使用類(lèi)名作為文件名稱
//SharedPreferences preferences = this.getSharedPreferences("preference", Context.MODE_PRIVATE);
SharedPreferences preferences = getApplicationContext().getSharedPreferences("preference", Context.MODE_PRIVATE);
String name = preferences.getString("name", "空");
String age = String.valueOf(preferences.getInt("age", 0));
nameText.setText(name);
ageText.setText(age);
}
public void save(View v){
String name = nameText.getText().toString();
Integer age = Integer.valueOf( ageText.getText().toString() );
try{
// 第1個(gè)參數(shù)為文件名稱,不能指定后綴名,第2個(gè)參數(shù)為文件操作模式
SharedPreferences preferences = getApplicationContext().getSharedPreferences("preference", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", age);
editor.commit();
Toast.makeText(getApplicationContext(),R.string.success, Toast.LENGTH_LONG).show();
}
catch (Exception e){
Toast.makeText(getApplicationContext(),R.string.fail, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
4 運(yùn)行結(jié)果
第1次運(yùn)行結(jié)果如圖:

輸入內(nèi)容,點(diǎn)擊保存參數(shù)按鈕,退出程序,再次打開(kāi),如圖:

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)