你的位置:首页 > 操作系统

[操作系统]Android SharedPreference的使用


在《Android 在内部存储读写文件》一文中,谈到了登录用户名和密码的方法,通过读取内存文件来实现,但是会出现问题,因为登录名和密码的形式通过username##password的形式,读取的时候以##为分割线,但是一旦用户名中存在##就会出现问题,本文使用SharedPreference来解决这个问题

1. 定义布局
使用之前的布局
2. 把组件的状态用SharedPreference保存起来

* 往SharedPreference里写数据

//拿到一个SharedPreference对象
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
//拿到编辑器
Editor ed = sp.edit();
ed.putBoolean("checkBox", isChecked);
ed.commit();

* 从SharedPreference里取数据

SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
//从SharedPreference里取数据
boolean b = sp.getBoolean("checkBox", false);

简化后的代码如下:

package com.wuyudong.sharedpreference;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // 获取到SharedPreference对象    SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);    //获取sp中的内容    String name = sp.getString("name", "");    String pass = sp.getString("pass", "");    EditText et_name = (EditText) findViewById(R.id.et_name);    EditText et_pass = (EditText) findViewById(R.id.et_pass);    //数据回显至输入框    et_name.setText(name);    et_pass.setText(pass);  }  public void click(View v) {    EditText et_name = (EditText) findViewById(R.id.et_name);    EditText et_pass = (EditText) findViewById(R.id.et_pass);    String name = et_name.getText().toString();    String pass = et_pass.getText().toString();    // 获取到SharedPreference对象    SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);    // 得到编辑器    Editor ed = sp.edit();    ed.putString("name", name);    ed.putString("pass", pass);    ed.commit();  }}

其实android中的很多设置都用到了SharedPreference

例如

查看com.android.settings/shared_prefs下的com.android.settings_preferences.

<??><map>  <boolean name="sound_effects" value="true" />  <boolean name="com.android.inputmethod.latin/.LatinIME" value="true" />  <boolean name="force_hw_ui" value="false" />  <boolean name="jp.co.omronsoft.openwnn/.OpenWnnJAJP" value="false" />  <boolean name="auto_time" value="false" />  <boolean name="show_hw_screen_udpates" value="false" />  <boolean name="24 hour" value="true" />  <boolean name="dock_sounds" value="false" />  <string name="font_size">1.30</string>  <boolean name="allow_mock_location" value="false" />  <boolean name="keep_screen_on" value="false" />  <boolean name="show_hw_layers_udpates" value="false" />  <string name="date_format"></string>  <boolean name="enable_adb" value="false" />  <boolean name="enforce_read_external" value="false" />  <boolean name="show_touches" value="false" />  <boolean name="dtmf_tone" value="true" />  <boolean name="strict_mode" value="false" />  <boolean name="com.example.android.softkeyboard/.SoftKeyboard" value="false" />  <boolean name="show_hw_overdraw" value="false" />  <boolean name="auto_zone" value="false" />  <string name="hdcp_checking">drm-only</string>  <boolean name="show_cpu_usage" value="false" />  <boolean name="debug_layout" value="false" />  <boolean name="com.android.inputmethod.pinyin/.PinyinIME" value="false" />  <boolean name="pointer_location" value="false" />  <boolean name="disable_overlays" value="false" />  <boolean name="bugreport_in_power" value="false" />  <boolean name="haptic_feedback" value="true" />  <boolean name="immediately_destroy_activities" value="false" />  <boolean name="show_all_anrs" value="false" />  <boolean name="verify_apps_over_usb" value="false" />  <boolean name="lock_sounds" value="true" />  <boolean name="show_screen_updates" value="false" />  <boolean name="force_msaa" value="false" />  <boolean name="wait_for_debugger" value="false" /></map>