Android存储--SharedPreferences

本文介绍 SharedPreferences 在 Android 应用中存储轻量级数据的方法,并通过一个用户登录记住密码的功能实现来展示具体步骤。该功能包括读取已保存的用户名和密码、在登录成功后保存这些信息等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

共享参数存储  Shared Preferences
1, 特征
1.1   存放轻量级数据的存储方式
1.2   本质上是以xml的格式, 通过键值队的方式对数据进行读取
1.3   通常是用于存储简单的数据信息
1.4   应用程序卸载后, 文件也会被删
2, 存储的数据类型
boolean  int  string  long  float
3, 存放数据的路径
data/data/应用程序包名/shared_prefs/***.xml
4, 存储数据
4.1, 得到共享参数对象
SharedPreferences sp = getSharedPreferences(文件名称没有扩展名, 文件的操作模式);
     4.2, 得到共享参数的编辑对象
Editor editor = sp.edit();
     4.3, 向共享参数中写入数据
editor.putInt(key,value);    
     4.4, 提交数据
editor.commit();
5, 读取数据
5.1  得到共享参数的对象
    SharedPreferences sp = getSharedPreferences(文件名称没有扩展名, 文件的操作模式);
5.2  读取数据
    int color = sp.getInt(key,默认的内容);
6, Editor 的常用方法
editor.commit();  提交数据
editor.clear();   清除所有数据

editor.remove(key); 移除指定Key的数据

实现用户登录记住密码:

[java]  view plain  copy
 print ?
  1. public class MainActivity extends Activity {  
  2.     private EditText namee, pwde;  
  3.     private CheckBox check;  
  4.     private SharedPreferences sp;  
  5.     private Editor editor;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         namee = (EditText) findViewById(R.id.namee);  
  12.         pwde = (EditText) findViewById(R.id.pwde);  
  13.         check = (CheckBox) findViewById(R.id.check);  
  14.   
  15.         sp = getSharedPreferences("loginxml", Context.MODE_PRIVATE);  
  16.   
  17.         String name = sp.getString("name""");  
  18.         String pwd = sp.getString("pwd""");  
  19.         if (!("").equals(name) || !("").equals(pwd)) {  
  20.             namee.setText(name);  
  21.             pwde.setText(pwd);  
  22.             check.setChecked(true);  
  23.         }  
  24.     }  
  25.   
  26.     public void login(View v) {  
  27.         String name = namee.getText().toString();  
  28.         String pwd = pwde.getText().toString();  
  29.         if (check.isChecked()) {  
  30.   
  31.             editor = sp.edit();  
  32.             editor.putString("name", name);  
  33.             editor.putString("pwd", pwd);  
  34.             editor.commit();  
  35.             Toast.makeText(this"登录成功"2).show();  
  36.   
  37.         } else {  
  38.             editor = sp.edit();  
  39.             //editor.clear();  
  40.              editor.remove("pwd");  
  41.             editor.commit();  
  42.             Toast.makeText(this"清除成功"2).show();  
  43.         }  
  44.     }  
  45. }  
[html]  view plain  copy
 print ?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/tv"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_centerHorizontal="true"  
  16.         android:text="用户登录"  
  17.         android:textSize="25sp" />  
  18.     <TextView  
  19.         android:id="@+id/name"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="用户名 : "  
  23.         android:layout_below="@id/tv"  
  24.         android:layout_marginTop="20dp"  
  25.         android:textSize="20sp" />  
  26.   <EditText   
  27.       android:id="@+id/namee"  
  28.       android:layout_width="wrap_content"  
  29.       android:layout_height="wrap_content"  
  30.       android:hint="请输入用户名"  
  31.       android:layout_toRightOf="@id/name"  
  32.       android:background="@android:drawable/edit_text"  
  33.       android:layout_marginLeft="20dp"  
  34.       android:layout_alignBottom="@id/name"/>  
  35.     <TextView  
  36.         android:id="@+id/pwd"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:text="密码 : "  
  40.         android:layout_below="@id/name"  
  41.         android:layout_marginTop="30dp"  
  42.         android:textSize="20sp" />  
  43.   <EditText   
  44.       android:id="@+id/pwde"  
  45.       android:layout_width="wrap_content"  
  46.       android:layout_height="wrap_content"  
  47.      android:layout_alignLeft="@id/namee"  
  48.       android:hint="请输入密码"  
  49.       android:layout_toRightOf="@id/pwd"  
  50.       android:background="@android:drawable/edit_text"  
  51.       android:layout_alignBottom="@id/pwd"/>  
  52.   <CheckBox   
  53.       android:id="@+id/check"  
  54.       android:layout_width="wrap_content"  
  55.       android:layout_height="wrap_content"  
  56.       android:layout_below="@id/pwd"  
  57.       android:layout_marginTop="20dp"  
  58.       android:text="记住密码"/>  
  59.    <Button   
  60.        android:id="@+id/btn"  
  61.        android:layout_width="wrap_content"  
  62.        android:layout_height="wrap_content"  
  63.        android:layout_margin="30dp"  
  64.        android:onClick="login"  
  65.        android:text="登录"  
  66.        android:layout_below="@id/check"/>  
  67.    <Button   
  68.        android:id="@+id/btn01"  
  69.        android:layout_width="wrap_content"  
  70.        android:layout_height="wrap_content"  
  71.        android:layout_margin="30dp"  
  72.        android:text="重置"  
  73.        android:layout_toRightOf="@id/btn"  
  74.        android:layout_below="@id/check"/>  
  75.      
  76. </RelativeLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值