【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2116|回复: 1

Android 记住密(转)码和自动登录界面的实现(SharedPreferences 的用法)

[复制链接]
发表于 2013-5-19 22:47:08 | 显示全部楼层 |阅读模式

SharedPreferences介绍:

SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下。

SharedPreferences的用法:

由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的,用法见下面代码:

Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例


name:是指文件名称,不需要加后缀.xml,系统会自动为我们添加上。

mode:是指定读写方式,其值有三种,分别为:

Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写

Context.MODE_WORLD_READABLE指定该SharedPreferences数据能被其他应用程序读,但不能写

Context.MODE_WORLD_WRITEABLE指定该SharedPreferences数据能被其他应用程序读写。


结果截图:








工程目录:


  1. <span style="font-family:'Courier New';">package com.liu.activity;

  2. import android.app.Activity;
  3. import android.app.backup.SharedPreferencesBackupHelper;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.content.SharedPreferences.Editor;
  8. import android.os.Bundle;
  9. import android.text.Spannable;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.view.Window;
  13. import android.widget.Button;
  14. import android.widget.CheckBox;
  15. import android.widget.CompoundButton;
  16. import android.widget.CompoundButton.OnCheckedChangeListener;
  17. import android.widget.EditText;
  18. import android.widget.ImageButton;
  19. import android.widget.Toast;

  20. public class LoginActivity extends Activity {
  21.         
  22.         private EditText userName, password;
  23.         private CheckBox rem_pw, auto_login;
  24.         private Button btn_login;
  25.         private ImageButton btnQuit;
  26.     private String userNameValue,passwordValue;
  27.         private SharedPreferences sp;

  28.         public void onCreate(Bundle savedInstanceState) {
  29.                 super.onCreate(savedInstanceState);
  30.                
  31.                 //去除标题
  32.                 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  33.                 setContentView(R.layout.login);
  34.                
  35.         //获得实例对象
  36.                 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
  37.                 userName = (EditText) findViewById(R.id.et_zh);
  38.                 password = (EditText) findViewById(R.id.et_mima);
  39.         rem_pw = (CheckBox) findViewById(R.id.cb_mima);
  40.                 auto_login = (CheckBox) findViewById(R.id.cb_auto);
  41.         btn_login = (Button) findViewById(R.id.btn_login);
  42.         btnQuit = (ImageButton)findViewById(R.id.img_btn);
  43.                
  44.         
  45.                 //判断记住密码多选框的状态
  46.       if(sp.getBoolean("ISCHECK", false))
  47.         {
  48.               //设置默认是记录密码状态
  49.           rem_pw.setChecked(true);
  50.                  userName.setText(sp.getString("USER_NAME", ""));
  51.                  password.setText(sp.getString("PASSWORD", ""));
  52.                  //判断自动登陆多选框状态
  53.                  if(sp.getBoolean("AUTO_ISCHECK", false))
  54.                  {
  55.                             //设置默认是自动登录状态
  56.                             auto_login.setChecked(true);
  57.                            //跳转界面
  58.                                 Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
  59.                                 LoginActivity.this.startActivity(intent);
  60.                                 
  61.                  }
  62.         }
  63.                
  64.             // 登录监听事件  现在默认为用户名为:liu 密码:123
  65.                 btn_login.setOnClickListener(new OnClickListener() {

  66.                         public void onClick(View v) {
  67.                                 userNameValue = userName.getText().toString();
  68.                             passwordValue = password.getText().toString();
  69.                            
  70.                                 if(userNameValue.equals("liu")&&passwordValue.equals("123"))
  71.                                 {
  72.                                         Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
  73.                                         //登录成功和记住密码框为选中状态才保存用户信息
  74.                                         if(rem_pw.isChecked())
  75.                                         {
  76.                                          //记住用户名、密码、
  77.                                           Editor editor = sp.edit();
  78.                                           editor.putString("USER_NAME", userNameValue);
  79.                                           editor.putString("PASSWORD",passwordValue);
  80.                                           editor.commit();
  81.                                         }
  82.                                         //跳转界面
  83.                                         Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
  84.                                         LoginActivity.this.startActivity(intent);
  85.                                         //finish();
  86.                                        
  87.                                 }else{
  88.                                        
  89.                                         Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
  90.                                 }
  91.                                 
  92.                         }
  93.                 });

  94.             //监听记住密码多选框按钮事件
  95.                 rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  96.                         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  97.                                 if (rem_pw.isChecked()) {
  98.                     
  99.                                         System.out.println("记住密码已选中");
  100.                                         sp.edit().putBoolean("ISCHECK", true).commit();
  101.                                        
  102.                                 }else {
  103.                                        
  104.                                         System.out.println("记住密码没有选中");
  105.                                         sp.edit().putBoolean("ISCHECK", false).commit();
  106.                                        
  107.                                 }

  108.                         }
  109.                 });
  110.                
  111.                 //监听自动登录多选框事件
  112.                 auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  113.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  114.                                 if (auto_login.isChecked()) {
  115.                                         System.out.println("自动登录已选中");
  116.                                         sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

  117.                                 } else {
  118.                                         System.out.println("自动登录没有选中");
  119.                                         sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
  120.                                 }
  121.                         }
  122.                 });
  123.                
  124.                 btnQuit.setOnClickListener(new OnClickListener() {
  125.                         
  126.                         @Override
  127.                         public void onClick(View v) {
  128.                                 finish();
  129.                         }
  130.                 });

  131.         }
  132. }</span>
复制代码
LogoActivity.java
  1. <span style="font-family:'Courier New';">package com.liu.activity;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.content.SharedPreferences.Editor;
  6. import android.opengl.ETC1;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.Window;
  11. import android.view.animation.AlphaAnimation;
  12. import android.view.animation.Animation;
  13. import android.view.animation.Animation.AnimationListener;
  14. import android.widget.Button;
  15. import android.widget.ImageButton;
  16. import android.widget.ImageView;
  17. import android.widget.ProgressBar;

  18. public class LogoActivity extends Activity {
  19.         private ProgressBar progressBar;
  20.         private Button backButton;

  21.         protected void onCreate(Bundle savedInstanceState) {
  22.                 super.onCreate(savedInstanceState);
  23.                 // 去除标题
  24.                 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  25.                 setContentView(R.layout.logo);

  26.                 progressBar = (ProgressBar) findViewById(R.id.pgBar);
  27.                 backButton = (Button) findViewById(R.id.btn_back);

  28.                 Intent intent = new Intent(this, WelcomeAvtivity.class);
  29.                 LogoActivity.this.startActivity(intent);

  30.                 backButton.setOnClickListener(new OnClickListener() {

  31.                         @Override
  32.                         public void onClick(View v) {
  33.                                 finish();

  34.                         }
  35.                 });

  36.         }

  37. }
  38. </span>
复制代码
WelcomeActivity.java
  1. <span style="font-family:'Courier New';"><span style="BACKGROUND-COLOR: rgb(240,240,240); WHITE-SPACE: pre">package com.liu.activity;</span>
  2. import android.app.Activity;
  3. import android.os.Bundle;

  4. public class WelcomeAvtivity extends Activity {

  5.         @Override
  6.         protected void onCreate(Bundle savedInstanceState) {
  7.                 // TODO Auto-generated method stub
  8.                 super.onCreate(savedInstanceState);
  9.                 setContentView(R.layout.welcome);
  10.         }

  11. }</span>
复制代码

布局文件:

login.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.     <RelativeLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content" >
  10.         <ImageButton
  11.             android:id="@+id/img_btn"
  12.             android:layout_width="wrap_content"
  13.             android:layout_height="wrap_content"
  14.             android:layout_alignParentRight="true"
  15.             android:background="@drawable/quit"/>

  16.         <TextView
  17.             android:id="@+id/tv_zh"
  18.             android:layout_width="wrap_content"
  19.             android:layout_height="35dip"
  20.             android:layout_marginLeft="12dip"
  21.             android:layout_marginTop="10dip"
  22.             android:gravity="bottom"
  23.             android:text="帐号:"
  24.             android:textColor="#000000"
  25.             android:textSize="18sp" />

  26.         <EditText
  27.             android:id="@+id/et_zh"
  28.             android:layout_width="fill_parent"
  29.             android:layout_height="40dip"
  30.             android:layout_below="@id/tv_zh"
  31.             android:layout_marginLeft="12dip"
  32.             android:layout_marginRight="10dip" />

  33.         <TextView
  34.             android:id="@+id/tv_mima"
  35.             android:layout_width="wrap_content"
  36.             android:layout_height="35dip"
  37.             android:layout_below="@id/et_zh"
  38.             android:layout_marginLeft="12dip"
  39.             android:layout_marginTop="10dip"
  40.             android:gravity="bottom"
  41.             android:text="密码:"
  42.             android:textColor="#000000"
  43.             android:textSize="18sp" />

  44.         <EditText
  45.             android:id="@+id/et_mima"
  46.             android:layout_width="fill_parent"
  47.             android:layout_height="40dip"
  48.             android:layout_below="@id/tv_mima"
  49.             android:layout_marginLeft="12dip"
  50.             android:layout_marginRight="10dip"
  51.             android:maxLines="200"
  52.             android:password="true"
  53.             android:scrollHorizontally="true" />

  54.         <CheckBox
  55.             android:id="@+id/cb_mima"
  56.             android:layout_width="wrap_content"
  57.             android:layout_height="wrap_content"
  58.             android:layout_below="@id/et_mima"
  59.             android:layout_marginLeft="12dip"
  60.             android:text="记住密码"
  61.             android:textColor="#000000" />

  62.         <CheckBox
  63.             android:id="@+id/cb_auto"
  64.             android:layout_width="wrap_content"
  65.             android:layout_height="wrap_content"
  66.             android:layout_below="@id/cb_mima"
  67.             android:layout_marginLeft="12dip"
  68.             android:text="自动登录"
  69.             android:textColor="#000000" />
  70.         <Button
  71.             android:id="@+id/btn_login"
  72.             android:layout_width="80dip"
  73.             android:layout_height="40dip"
  74.             android:layout_below="@id/et_mima"
  75.             android:layout_alignParentRight="true"
  76.             android:layout_alignTop="@id/cb_auto"
  77.             android:layout_marginRight="10dip"
  78.             android:gravity="center"
  79.             android:text="登录"
  80.             android:textColor="#000000"
  81.             android:textSize="18sp"/>

  82.         
  83.     </RelativeLayout>
  84.    
  85.    

  86. </LinearLayout></span>
复制代码
logo.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.     <RelativeLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:layout_weight="3">

  11.         <ProgressBar
  12.             android:id="@+id/pgBar"
  13.             android:layout_width="wrap_content"
  14.             android:layout_height="wrap_content"
  15.             android:layout_centerInParent="true" />

  16.         <TextView
  17.             android:id="@+id/tv1"
  18.             android:layout_width="wrap_content"
  19.             android:layout_height="wrap_content"
  20.             android:layout_below="@id/pgBar"
  21.             android:layout_centerHorizontal="true"
  22.             android:text="正在登录..."
  23.             android:textColor="#000000"
  24.             android:textSize="18sp" />
  25.     </RelativeLayout>

  26.     <LinearLayout
  27.         android:layout_width="fill_parent"
  28.         android:layout_height="wrap_content"
  29.         android:layout_weight="1"
  30.         android:gravity="center"
  31.         android:orientation="vertical" >

  32.         <Button
  33.             android:id="@+id/btn_back"
  34.             android:layout_width="70dip"
  35.             android:layout_height="35dip"
  36.             android:text="取消"
  37.             android:textColor="#000000"
  38.             android:textSize="12sp" />
  39.     </LinearLayout>


  40. </LinearLayout></span>
复制代码
welcome.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:layout_gravity="center"
  6.     android:background="@drawable/login_bg"
  7.     android:orientation="vertical" >

  8.     <TextView
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:gravity="center"
  12.         android:text="登陆成功,进入用户界面"
  13.         android:textColor="#000000"
  14.         android:textSize="20sp" />

  15. </LinearLayout></span>
复制代码
工程下载连接:点击打开链接
发表于 2013-5-20 15:18:43 | 显示全部楼层
本帖最后由 夜行的猫仔 于 2013-5-20 15:19 编辑

关键的步骤写出来就行了:
读取:
  1. SharedPreferences userInfo = getSharedPreferences("iSOSOuserinfo", 0);
  2. String username = userInfo.getString("name", "");
  3. String pass = userInfo.getString("pass", "");
  4. ((EditText)findViewById(R.id.editText1)).setText(pass);
  5. ((EditText)findViewById(R.id.editText2)).setText(username);
复制代码
写入:
  1. SharedPreferences userInfo = getSharedPreferences("iSOSOuserinfo", 0);
  2. userInfo.edit().putString("name", Name.getText().toString()).commit();  
  3. userInfo.edit().putString("pass", Pwd.getText().toString()).commit();
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-6-17 02:09

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表