SharedPreferences是ANDROID系统中轻量级别的键值存储机制,只是存储基本数据类型。如下是它的基本使用方法:
AndroidManifest.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.mySharedPreferences"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".mySharedPreferences"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<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:text="请输入用户名:"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:text="@+id/EditText01"
android:id="@+id/EditText01"
android:maxLines = "1"
android:width = "100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<TextView android:text="请输入密码"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:text="@+id/EditText02"
android:id="@+id/EditText02"
android:maxLines = "1"
android:width = "100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
mySharedPreferences.java文件内容如下:
package pack.mySharedPreferences;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class mySharedPreferences extends Activity {
//定义各个元素对象
EditText username;
EditText password;
//定义要保存SharedPreferences的标示
String PREFS_NAME = "pack.mySharedPreferences.username";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//元素绑定
username = (EditText)findViewById(R.id.EditText01);
password = (EditText)findViewById(R.id.EditText02);
//取得保存的用户名称,如果没有,就设为空
username.setText(getUserName());
password.setText("");
username.setOnEditorActionListener(new OnEditorActionListener(){
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
//保存现在的用户名,下次使用时可读取
saveUserName(username.getText().toString());
username.setText(username.getText().toString());
return false;
}
});
}
private String getUserName()
{
//定义一个只允许自有程序访问的SharedPreferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
//获得一个键值为username的值,若Preference中不存在,则就用后面的值作为返回值
String username = settings.getString("username","");
return username;
}
private void saveUserName(String name)
{
//定义一个只允许自有程序访问的SharedPreferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
//生成一个保存编辑对象
SharedPreferences.Editor editor = settings.edit();
//添加要保存的键值和其它对应的值
editor.putString("username",name);
//提交保存
editor.commit();
}
}
结果界面如下:
在DDMS里面的DATA目录下有如下的保存文件,其值是以XML文件格式保存,如目录格式如下图所示: