Android 記住密碼和自動登錄界面的實現(SharedPreferences 的用法)1

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數據能被其他應用程序讀寫


結果截圖:







工程目錄:



Java代碼

LoginActivity.java

[java] view plaincopy

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

  2.   

  3. import android.app.Activity;  

  4. import android.app.backup.SharedPreferencesBackupHelper;  

  5. import android.content.Context;  

  6. import android.content.Intent;  

  7. import android.content.SharedPreferences;  

  8. import android.content.SharedPreferences.Editor;  

  9. import android.os.Bundle;  

  10. import android.text.Spannable;  

  11. import android.view.View;  

  12. import android.view.View.OnClickListener;  

  13. import android.view.Window;  

  14. import android.widget.Button;  

  15. import android.widget.CheckBox;  

  16. import android.widget.CompoundButton;  

  17. import android.widget.CompoundButton.OnCheckedChangeListener;  

  18. import android.widget.EditText;  

  19. import android.widget.ImageButton;  

  20. import android.widget.Toast;  

  21.   

  22. public class LoginActivity extends Activity {  

  23.       

  24.     private EditText userName, password;  

  25.     private CheckBox rem_pw, auto_login;  

  26.     private Button btn_login;  

  27.     private ImageButton btnQuit;  

  28.     private String userNameValue,passwordValue;  

  29.     private SharedPreferences sp;  

  30.   

  31.     public void onCreate(Bundle savedInstanceState) {  

  32.         super.onCreate(savedInstanceState);  

  33.           

  34.         //去除標題  

  35.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  

  36.         setContentView(R.layout.login);  

  37.           

  38.         //獲得實例對象  

  39.         sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);  

  40.         userName = (EditText) findViewById(R.id.et_zh);  

  41.         password = (EditText) findViewById(R.id.et_mima);  

  42.         rem_pw = (CheckBox) findViewById(R.id.cb_mima);  

  43.         auto_login = (CheckBox) findViewById(R.id.cb_auto);  

  44.         btn_login = (Button) findViewById(R.id.btn_login);  

  45.         btnQuit = (ImageButton)findViewById(R.id.img_btn);  

  46.           

  47.           

  48.         //判斷記住密碼多選框的狀態  

  49.       if(sp.getBoolean("ISCHECK"false))  

  50.         {  

  51.           //設置默認是記錄密碼狀態  

  52.           rem_pw.setChecked(true);  

  53.           userName.setText(sp.getString("USER_NAME"""));  

  54.           password.setText(sp.getString("PASSWORD"""));  

  55.           //判斷自動登陸多選框狀態  

  56.           if(sp.getBoolean("AUTO_ISCHECK"false))  

  57.           {  

  58.                  //設置默認是自動登錄狀態  

  59.                  auto_login.setChecked(true);  

  60.                 //跳轉界面  

  61.                 Intent intent = new Intent(LoginActivity.this,LogoActivity.class);  

  62.                 LoginActivity.this.startActivity(intent);  

  63.                   

  64.           }  

  65.         }  

  66.           

  67.         // 登錄監聽事件  現在默認爲用戶名爲:liu 密碼:123  

  68.         btn_login.setOnClickListener(new OnClickListener() {  

  69.   

  70.             public void onClick(View v) {  

  71.                 userNameValue = userName.getText().toString();  

  72.                 passwordValue = password.getText().toString();  

  73.                   

  74.                 if(userNameValue.equals("liu")&&passwordValue.equals("123"))  

  75.                 {  

  76.                     Toast.makeText(LoginActivity.this,"登錄成功", Toast.LENGTH_SHORT).show();  

  77.                     //登錄成功和記住密碼框爲選中狀態才保存用戶信息  

  78.                     if(rem_pw.isChecked())  

  79.                     {  

  80.                      //記住用戶名、密碼、  

  81.                       Editor editor = sp.edit();  

  82.                       editor.putString("USER_NAME", userNameValue);  

  83.                       editor.putString("PASSWORD",passwordValue);  

  84.                       editor.commit();  

  85.                     }  

  86.                     //跳轉界面  

  87.                     Intent intent = new Intent(LoginActivity.this,LogoActivity.class);  

  88.                     LoginActivity.this.startActivity(intent);  

  89.                     //finish();  

  90.                       

  91.                 }else{  

  92.                       

  93.                     Toast.makeText(LoginActivity.this,"用戶名或密碼錯誤,請重新登錄", Toast.LENGTH_LONG).show();  

  94.                 }  

  95.                   

  96.             }  

  97.         });  

  98.   

  99.         //監聽記住密碼多選框按鈕事件  

  100.         rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {  

  101.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  

  102.                 if (rem_pw.isChecked()) {  

  103.                       

  104.                     System.out.println("記住密碼已選中");  

  105.                     sp.edit().putBoolean("ISCHECK"true).commit();  

  106.                       

  107.                 }else {  

  108.                       

  109.                     System.out.println("記住密碼沒有選中");  

  110.                     sp.edit().putBoolean("ISCHECK"false).commit();  

  111.                       

  112.                 }  

  113.   

  114.             }  

  115.         });  

  116.           

  117.         //監聽自動登錄多選框事件  

  118.         auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {  

  119.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  

  120.                 if (auto_login.isChecked()) {  

  121.                     System.out.println("自動登錄已選中");  

  122.                     sp.edit().putBoolean("AUTO_ISCHECK"true).commit();  

  123.   

  124.                 } else {  

  125.                     System.out.println("自動登錄沒有選中");  

  126.                     sp.edit().putBoolean("AUTO_ISCHECK"false).commit();  

  127.                 }  

  128.             }  

  129.         });  

  130.           

  131.         btnQuit.setOnClickListener(new OnClickListener() {  

  132.               

  133.             @Override  

  134.             public void onClick(View v) {  

  135.                 finish();  

  136.             }  

  137.         });  

  138.   

  139.     }  

  140. }</span>  


LogoActivity.java

[java] view plaincopy

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

  2.   

  3. import android.app.Activity;  

  4. import android.content.Intent;  

  5. import android.content.SharedPreferences;  

  6. import android.content.SharedPreferences.Editor;  

  7. import android.opengl.ETC1;  

  8. import android.os.Bundle;  

  9. import android.view.View;  

  10. import android.view.View.OnClickListener;  

  11. import android.view.Window;  

  12. import android.view.animation.AlphaAnimation;  

  13. import android.view.animation.Animation;  

  14. import android.view.animation.Animation.AnimationListener;  

  15. import android.widget.Button;  

  16. import android.widget.ImageButton;  

  17. import android.widget.ImageView;  

  18. import android.widget.ProgressBar;  

  19.   

  20. public class LogoActivity extends Activity {  

  21.     private ProgressBar progressBar;  

  22.     private Button backButton;  

  23.   

  24.     protected void onCreate(Bundle savedInstanceState) {  

  25.         super.onCreate(savedInstanceState);  

  26.         // 去除標題  

  27.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  

  28.         setContentView(R.layout.logo);  

  29.   

  30.         progressBar = (ProgressBar) findViewById(R.id.pgBar);  

  31.         backButton = (Button) findViewById(R.id.btn_back);  

  32.   

  33.         Intent intent = new Intent(this, WelcomeAvtivity.class);  

  34.         LogoActivity.this.startActivity(intent);  

  35.   

  36.         backButton.setOnClickListener(new OnClickListener() {  

  37.   

  38.             @Override  

  39.             public void onClick(View v) {  

  40.                 finish();  

  41.   

  42.             }  

  43.         });  

  44.   

  45.     }  

  46.   

  47. }  

  48. </span>  


WelcomeActivity.java

[java] view plaincopy

  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.   

  5. public class WelcomeAvtivity extends Activity {  

  6.   

  7.     @Override  

  8.     protected void onCreate(Bundle savedInstanceState) {  

  9.         // TODO Auto-generated method stub  

  10.         super.onCreate(savedInstanceState);  

  11.         setContentView(R.layout.welcome);  

  12.     }  

  13.   

  14. }</span>  



【轉】http://blog.csdn.net/liuyiming_/article/details/7704923

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章