共享首选项-SharedPreferences

一、共享首选项-SharedPreferences

(一)概述
有些应用需要保存配置信息,如是否打开音效开关、登录时是否保存上次输入的用户名、密码等。Android 对这些类应用提供了 SharedPreferences(共享首选项),共享首选项适合数据量少,数据以键/值对的方式保存的应用。

(二)常用类/接口
1、SharedPreferences 接口
作用:该接口中定义了共享首选项操作的方法和一个重要的内部接口:Editor。

2、Editor 接口
作用:该接口定义了存储、删除数据的方法。

(三)常用方法
1、context.getSharedPreferences(String fileName,int mode);
作用:创建 SharedPreferences 接口的实现类的对象

第一个参数——fileName:指定共享首选项数据保存、读取的文件名。该文件存放在data/data/packageName/shared_prefs文件夹下。通过eclipse中的DDMS视图中的file-explorer视图可以找到当前项目中以上文件夹。默认文件名:类名.xml。

第二个参数——mode:共享首选项文件的存取权限,通过以下常量设置:

MODE_WORLD_PRIVATE:私有属性,只有本项目才能存取第一个参数指定的.xml文件。

MODE_WORLD_READABLE:允许其它项目读取本项目中的xml文件内容。

MODE_WORLD_WRITEABLE:允许其它项目向本项目中的.xml文件写数据。

——————————存数据————————————
2、共享首选项文件存储的相关方法:

1)Editor.putInt(String key,int value)
作用:存放键名为key的int类型的数据

2)Editor.putFloat(String key,float value)
作用:存放键名为key的float类型的数据

3)Editor.putString(String key,String value)
作用:存放键名为key的String类型的数据

4)Editor.putBoolean(String key,Boolean value)
作用:存放键名为key的Boolean类型的数据

5)Editor.putLong(String key,long value)
作用:存放键名为key的long类型的数据

6)Editor.remove(String key)
作用:移除键名为key的键/值对

7)Eidtor.commit()
作用:提交修改、保存结果
提示:只有执行了本方法,以上的put方法存储的数据才能真正有效。

 


——————————取数据————————————
3、共享首选等文件读取的相关方法(以下方法均从属于SharedPreferences接口的实现类)

1)int getInt(String key,int defValue)
作用:获取键名是key的int类型的数据,若没有key,则可以设置一个默认值:defValue

2)boolean getBoolean(String key,Boolean defValue)
作用:获取键名是key的boolean类型的数据,若没有key,则可以设置一个默认值:defValue

3)float getFloat(String key,float defValue)
作用:获取键名是key的float类型的数据,若没有key,则可以设置一个默认值:defValue

4)long getLong(String key,long defValue)
作用:获取键名是key的long类型的数据,若没有key,则可以设置一个默认值:defValue

5)String getString(String key,String defValue)
作用:获取键名是key的String类型的数据,若没有key,则可以设置一个默认值:defValue


案例:

package com.jxust.day06_05_sharedpreferencesdemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	EditText metId, metPwd;
	SharedPreferences mSp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setListener();
		readData();
	}

	private void readData() {
		mSp = getSharedPreferences("login", MODE_PRIVATE);
		String id = mSp.getString("id", "");
		String password = mSp.getString("password", "");
		metId.setText(id);
		metPwd.setText(password);
	}

	private void setListener() {
		findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String id = metId.getText().toString();
				if(TextUtils.isEmpty(id)){
					metId.setError("编号不能为空!");
					return;
				}
				String password = metPwd.getText().toString();
				if(TextUtils.isEmpty(password)){
					metPwd.setError("密码不能为空!");
					return;
				}
				mSp  = getSharedPreferences("login", MODE_PRIVATE);
				Editor editor = mSp.edit();	//会生成一个Editor类型的引用变量
				editor.putString("id", id);
				editor.putString("password", password);
				editor.commit();
				Toast.makeText(MainActivity.this, "登陆完成!", 3000).show();
			}
		});
		findViewById(R.id.btnExit).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView() {
		metId = (EditText) findViewById(R.id.etId);
		metPwd = (EditText) findViewById(R.id.etPwd);
	}

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆编号" />

        <EditText
            android:id="@+id/etId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="2-10个字符" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆密码" />

        <EditText
            android:id="@+id/etPwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="2-10个字符"
            android:password="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:text="登陆" />

        <Button
            android:id="@+id/btnExit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:text="退出" />
    </LinearLayout>

</LinearLayout>

猜你喜欢

转载自1124117571.iteye.com/blog/2300663