SharedPreferences解析和实现记住用户名

移动开发 Android
SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,它提供了Android平台常规的Long长整形、Int整形、String字符串型的保存。

SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,它提供了Android平台常规的Long长整形、Int整形、String字符串型的保存。

SharedPreferences不支持多线程。例如,可以通过它保存上一次用户所做的修改或者自定义参数设定,当再次启动程序后依然保持原有的设置。

另外的数据存储方式还有SQLite、Content Provider、File...

用法:

SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现的。

存放:

1.获得SharedPreferences 的实例对象,通过getSharedPreferences()传递存储时的名称和模式

2.获得Editor 的实例对象,通过SharedPreferences 的实例对象的edit()

3.存入数据用Editor的putXXX()  [存放什么数据就put那个数据的类型,支持Long、Int、String、Boolean]

4.提交修改的数据用Editor的commit()

读取:

1.获得SharedPreferences 的实例对象,通过getSharedPreferences()传递存储时的名称和模式

2.读取数据,通过SharedPreferences 的实例对象的getXXX()       [读取什么类型的数据就get那个类型]

getSharedPreferences方法扩展

getSharedPreferences("存储时的名称","模式")

模式(可组合使用):        

私有:       Context.MODE_PRIVATE                  值0

公开可读:Context.MODE_WORLD_READABLE    值1

公开可写:Context.MODE_WORLD_WRITEABLE  值2

  1. SharedPreferences sp = getSharedPreferences("mydata", Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_WRITEABLE); 

1、数据共享

2个activity 之间可以使用SharedPreferences来共享数据的方式

A类

  1. Editor sharedata = getSharedPreferences("data"0).edit(); 
  2.  sharedata.putString("item","hello getSharedPreferences"); 
  3.  sharedata.commit(); 

B类

  1. SharedPreferences sharedata = getSharedPreferences("data"0); 
  2.  String data = sharedata.getString("item"null); 
  3.  Log.v("cola","data="+data); 

2、保存修改

这里用记住用户名为例子解说

main.xml 布局文件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.     <LinearLayout  
  7.         android:layout_width="fill_parent" 
  8.         android:layout_height="wrap_content" 
  9.         android:orientation="horizontal"> 
  10.         <TextView 
  11.             android:layout_width="wrap_content" 
  12.             android:layout_height="wrap_content" 
  13.             android:text="用户名:" /> 
  14.         <EditText  
  15.             android:id="@+id/login_user_et" 
  16.             android:layout_width="150dip" 
  17.             android:layout_height="wrap_content" 
  18.             android:digits="abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"/> 
  19.     </LinearLayout> 
  20.     <LinearLayout  
  21.         android:layout_width="fill_parent" 
  22.         android:layout_height="wrap_content" 
  23.         android:orientation="horizontal"> 
  24.         <TextView 
  25.             android:layout_width="wrap_content" 
  26.             android:layout_height="wrap_content" 
  27.             android:text="密     码:" /> 
  28.         <EditText  
  29.             android:id="@+id/login_pswd_et" 
  30.             android:layout_width="150dip" 
  31.             android:layout_height="wrap_content" 
  32.             android:password="true"/> 
  33.     </LinearLayout> 
  34.     <LinearLayout  
  35.         android:layout_width="fill_parent" 
  36.         android:layout_height="wrap_content" 
  37.         android:orientation="horizontal"> 
  38.         <TextView 
  39.             android:layout_width="wrap_content" 
  40.             android:layout_height="wrap_content" 
  41.             android:text="记住密码:" /> 
  42.         <CheckBox 
  43.             android:id="@+id/login_checkbox" 
  44.             android:layout_width="wrap_content" 
  45.             android:layout_height="wrap_content"/> 
  46.     </LinearLayout> 
  47.     <Button  
  48.         android:id="@+id/login_btn" 
  49.         android:layout_width="200dip" 
  50.         android:layout_height="wrap_content" 
  51.         android:text="登陆"/> 
  52. </LinearLayout> 

 

  1. public class DemoActivity extends Activity { 
  2.      
  3.     public static final String PREFS_NAME = "prefsname"//偏好设置名称 
  4.     public static final String REMEMBER_USERID_KEY = "remember"//记住用户名 
  5.     public static final String USERID_KEY = "userid"//用户名标记 
  6.     private static final String DEFAULT_USERNAME = "Hades"//默认用户名 
  7.      
  8.     //组件 
  9.     private EditText userName = null
  10.     private EditText passWord = null
  11.     private CheckBox cb = null
  12.     private SharedPreferences mSettings = null
  13.     private Button submitBtn = null
  14.      
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) { 
  17.         super.onCreate(savedInstanceState); 
  18.         setContentView(R.layout.main); 
  19.         userName = (EditText) findViewById(R.id.login_user_et); 
  20.         passWord = (EditText) findViewById(R.id.login_pswd_et); 
  21.         cb = (CheckBox) findViewById(R.id.login_checkbox); 
  22.         submitBtn = (Button) findViewById(R.id.login_btn); 
  23.         mSettings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //模式为私有 
  24.         cb.setChecked(getRemember()); //勾选记住用户名 
  25.         userName.setText(getUserName()); //设置用户名 
  26.          
  27.         //保存用户名 
  28.         submitBtn.setOnClickListener(new OnClickListener() { 
  29.             @Override 
  30.             public void onClick(View v) { 
  31.                 //是否保存用户名 
  32.                 if (cb.isChecked()) { 
  33.                     saveRemember(true); 
  34.                     saveUserName(userName.getText().toString()); 
  35.                 } else { 
  36.                     saveRemember(false); 
  37.                     saveUserName(""); 
  38.                 } 
  39.             } 
  40.         }); 
  41.     } 
  42.  
  43.     // 保存用户名 
  44.     private void saveUserName(String userid) { 
  45.         Editor editor = mSettings.edit();// 获取编辑器 
  46.         editor.putString(USERID_KEY, userid); 
  47.         editor.commit(); //保存数据 
  48.         //editor.clear();//清除数据 
  49.     } 
  50.  
  51.     // 设置是否保存的用户名 
  52.     private void saveRemember(boolean remember) { 
  53.         Editor editor = mSettings.edit();// 获取编辑器 
  54.         editor.putBoolean(REMEMBER_USERID_KEY, remember); 
  55.         editor.commit(); 
  56.     } 
  57.     // 获取保存的用户名 
  58.     private String getUserName() { 
  59.         return mSettings.getString(USERID_KEY, DEFAULT_USERNAME); 
  60.     } 
  61.  
  62.     // 获取是否保存的用户名 
  63.     private boolean getRemember() { 
  64.         return mSettings.getBoolean(REMEMBER_USERID_KEY, true); 
  65.     } 

页面:

 

责任编辑:徐川 来源: OSChina
相关推荐

2019-08-26 19:24:55

Podman容器Linux

2022-06-24 08:48:47

用户名密码登录

2010-09-27 14:48:12

SQL用户名

2020-07-11 09:26:16

数据泄露黑客网络攻击

2009-08-18 13:52:57

Ubuntu用户名密码

2011-07-22 15:01:28

MongoDB权限管理

2009-10-21 16:34:03

Oracle用户名重建索引

2010-10-29 11:51:30

oracle用户名

2014-09-11 09:25:19

2010-05-31 09:10:20

Myeclipse S

2011-09-06 10:36:44

2009-08-05 13:32:07

Oracle按用户名重

2013-05-29 09:47:45

2010-05-24 14:00:43

Flex Svn

2009-10-21 17:13:32

Oracle用户名

2010-02-25 16:09:15

Fedora驱动程序

2009-10-26 16:08:40

Oracle默认用户名

2011-05-26 10:11:24

Oracle数据库索引

2018-01-02 10:06:49

Linux修改用户名修改家目录

2009-06-18 15:05:11

点赞
收藏

51CTO技术栈公众号