SharedPreferences的介绍

来源:互联网 发布:windows udp端口测试 编辑:程序博客网 时间:2024/06/04 18:58

SharedPreferences的用法简介:

1.SharedPreferences是Android中用于实现存储方式的技术。SharedPreferences的使用非常简单,能够轻松的存放数据和读取数据。SharedPreferences只能保存简单类型的数据,例如,String、int等。一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在XML文件中,再用SharedPreferences保存。 

   使用SharedPreferences保存key-value对的步骤如下:

  (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。

  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。

  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。

  (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。

2.实例(用SharedPreferences实现登陆页面记住密码和自动登录)

MainActivity.java 代码:

  1. "font-family:'Courier New';">package com.liu.activity;  
  2.   
  3. public class LoginActivity extends Activity 
  4.       
  5.     private EditText userName, password;  
  6.     private CheckBox rem_pw, auto_login;  
  7.     private Button btn_login;  
  8.     private ImageButton btnQuit;  
  9.     private String userNameValue,passwordValue;  
  10.     private SharedPreferences sp;  
  11.   
  12.     public void onCreate(Bundle savedInstanceState)  
  13.         super.onCreate(savedInstanceState);  
  14.           
  15.         //去除标题  
  16.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  17.         setContentView(R.layout.login);  
  18.           
  19.         //获得实例对象  
  20.         sp this.getSharedPreferences("userInfo"Context.MODE_WORLD_READABLE);  
  21.         userName (EditText) findViewById(R.id.et_zh);  
  22.         password (EditText) findViewById(R.id.et_mima);  
  23.         rem_pw (CheckBox) findViewById(R.id.cb_mima);  
  24.         auto_login (CheckBox) findViewById(R.id.cb_auto);  
  25.         btn_login (Button) findViewById(R.id.btn_login);  
  26.         btnQuit (ImageButton)findViewById(R.id.img_btn);  
  27.           
  28.           
  29.         //判断记住密码多选框的状态  
  30.       if(sp.getBoolean("ISCHECK"false))  
  31.          
  32.           //设置默认是记录密码状态  
  33.           rem_pw.setChecked(true);  
  34.           userName.setText(sp.getString("USER_NAME"""));  
  35.           password.setText(sp.getString("PASSWORD"""));  
  36.           //判断自动登陆多选框状态  
  37.           if(sp.getBoolean("AUTO_ISCHECK"false))  
  38.            
  39.                  //设置默认是自动登录状态  
  40.                  auto_login.setChecked(true);  
  41.                 //跳转界面  
  42.                 Intent intent new Intent(LoginActivity.this,LogoActivity.class);  
  43.                 LoginActivity.this.startActivity(intent);  
  44.                   
  45.            
  46.          
  47.           
  48.         // 登录监听事件  现在默认为用户名为:liu 密码:123  
  49.         btn_login.setOnClickListener(new OnClickListener()  
  50.   
  51.             public void onClick(View v)  
  52.                 userNameValue userName.getText().toString();  
  53.                 passwordValue password.getText().toString();  
  54.                   
  55.                 if(userNameValue.equals("liu")&&passwordValue.equals("123"))  
  56.                  
  57.                     Toast.makeText(LoginActivity.this,"登录成功"Toast.LENGTH_SHORT).show();  
  58.                     //登录成功和记住密码框为选中状态才保存用户信息  
  59.                     if(rem_pw.isChecked())  
  60.                      
  61.                      //记住用户名、密码、  
  62.                       Editor editor sp.edit();  
  63.                       editor.putString("USER_NAME"userNameValue);  
  64.                       editor.putString("PASSWORD",passwordValue);  
  65.                       editor.commit();  
  66.                      
  67.                     //跳转界面  
  68.                     Intent intent new Intent(LoginActivity.this,LogoActivity.class);  
  69.                     LoginActivity.this.startActivity(intent);  
  70.                     //finish();  
  71.                       
  72.                 }else 
  73.                       
  74.                     Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录"Toast.LENGTH_LONG).show();  
  75.                  
  76.                   
  77.              
  78.         });  
  79.   
  80.         //监听记住密码多选框按钮事件  
  81.         rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener()  
  82.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)  
  83.                 if (rem_pw.isChecked())  
  84.                       
  85.                     System.out.println("记住密码已选中");  
  86.                     sp.edit().putBoolean("ISCHECK"true).commit();  
  87.                       
  88.                 }else  
  89.                       
  90.                     System.out.println("记住密码没有选中");  
  91.                     sp.edit().putBoolean("ISCHECK"false).commit();  
  92.                       
  93.                  
  94.   
  95.              
  96.         });  
  97.           
  98.         //监听自动登录多选框事件  
  99.         auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener()  
  100.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)  
  101.                 if (auto_login.isChecked())  
  102.                     System.out.println("自动登录已选中");  
  103.                     sp.edit().putBoolean("AUTO_ISCHECK"true).commit();  
  104.   
  105.                 else  
  106.                     System.out.println("自动登录没有选中");  
  107.                     sp.edit().putBoolean("AUTO_ISCHECK"false).commit();  
  108.                  
  109.              
  110.         });  
  111.           
  112.         btnQuit.setOnClickListener(new OnClickListener()  
  113.               
  114.             @Override  
  115.             public void onClick(View v)  
  116.                 finish();  
  117.              
  118.         });  
  119.   
  120.      
  121. }  

activity.xml 文件:

  1. <span style="font-family:'Courier New';"><?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:background="@drawable/logo_bg"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" >  
  11.         <ImageButton   
  12.             android:id="@+id/img_btn"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_alignParentRight="true"  
  16.             android:background="@drawable/quit"/>  
  17.   
  18.         <TextView  
  19.             android:id="@+id/tv_zh"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="35dip"  
  22.             android:layout_marginLeft="12dip"  
  23.             android:layout_marginTop="10dip"  
  24.             android:gravity="bottom"  
  25.             android:text="帐号:"  
  26.             android:textColor="#000000"  
  27.             android:textSize="18sp" />  
  28.   
  29.         <EditText  
  30.             android:id="@+id/et_zh"  
  31.             android:layout_width="fill_parent"  
  32.             android:layout_height="40dip"  
  33.             android:layout_below="@id/tv_zh"  
  34.             android:layout_marginLeft="12dip"  
  35.             android:layout_marginRight="10dip" />  
  36.   
  37.         <TextView  
  38.             android:id="@+id/tv_mima"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="35dip"  
  41.             android:layout_below="@id/et_zh"  
  42.             android:layout_marginLeft="12dip"  
  43.             android:layout_marginTop="10dip"  
  44.             android:gravity="bottom"  
  45.             android:text="密码:"  
  46.             android:textColor="#000000"  
  47.             android:textSize="18sp" />  
  48.   
  49.         <EditText  
  50.             android:id="@+id/et_mima"  
  51.             android:layout_width="fill_parent"  
  52.             android:layout_height="40dip"  
  53.             android:layout_below="@id/tv_mima"  
  54.             android:layout_marginLeft="12dip"  
  55.             android:layout_marginRight="10dip"  
  56.             android:maxLines="200"  
  57.             android:password="true"  
  58.             android:scrollHorizontally="true" />  
  59.   
  60.         <CheckBox  
  61.             android:id="@+id/cb_mima"  
  62.             android:layout_width="wrap_content"  
  63.             android:layout_height="wrap_content"  
  64.             android:layout_below="@id/et_mima"  
  65.             android:layout_marginLeft="12dip"  
  66.             android:text="记住密码"  
  67.             android:textColor="#000000" />  
  68.   
  69.         <CheckBox  
  70.             android:id="@+id/cb_auto"  
  71.             android:layout_width="wrap_content"  
  72.             android:layout_height="wrap_content"  
  73.             android:layout_below="@id/cb_mima"  
  74.             android:layout_marginLeft="12dip"  
  75.             android:text="自动登录"  
  76.             android:textColor="#000000" />  
  77.         <Button  
  78.             android:id="@+id/btn_login"  
  79.             android:layout_width="80dip"  
  80.             android:layout_height="40dip"  
  81.             android:layout_below="@id/et_mima"  
  82.             android:layout_alignParentRight="true"  
  83.             android:layout_alignTop="@id/cb_auto"  
  84.             android:layout_marginRight="10dip"  
  85.             android:gravity="center"  
  86.             android:text="登录"  
  87.             android:textColor="#000000"  
  88.             android:textSize="18sp"/>  
  89.   
  90.           
  91.     </RelativeLayout>  
  92.       
  93.       
  94.   
  95. </LinearLayout></span>  

0 0
原创粉丝点击