Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
SharedPreferences介紹:
SharedPreferences是Android平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,主要是保存一些常用的配置參數(shù),它是采用xml文件存放數(shù)據(jù)的,文件存放在"/data/data<package name>/shared_prefs"目錄下。
SharedPreferences的用法:
由于SharedPreferences是一個(gè)接口,而且在這個(gè)接口里沒(méi)有提供寫(xiě)入數(shù)據(jù)和讀取數(shù)據(jù)的能力。但它是通過(guò)其Editor接口中的一些方法來(lái)操作SharedPreference的,用法見(jiàn)下面代碼:
Context.getSharedPreferences(String name,int mode)來(lái)得到一個(gè)SharedPreferences實(shí)例
name:是指文件名稱,不需要加后綴.xml,系統(tǒng)會(huì)自動(dòng)為我們添加上。
mode:是指定讀寫(xiě)方式,其值有三種,分別為:
Context.MODE_PRIVATE:指定該SharedPreferences數(shù)據(jù)只能被本應(yīng)用程序讀、寫(xiě)
Context.MODE_WORLD_READABLE:指定該SharedPreferences數(shù)據(jù)能被其他應(yīng)用程序讀,但不能寫(xiě)
Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences數(shù)據(jù)能被其他應(yīng)用程序讀寫(xiě)。
結(jié)果截圖:



工程目錄:

Java代碼
LoginActivity.java
package com.liu.activity;
import android.app.Activity;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText userName, password;
private CheckBox rem_pw, auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除標(biāo)題
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
//獲得實(shí)例對(duì)象
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.et_zh);
password = (EditText) findViewById(R.id.et_mima);
rem_pw = (CheckBox) findViewById(R.id.cb_mima);
auto_login = (CheckBox) findViewById(R.id.cb_auto);
btn_login = (Button) findViewById(R.id.btn_login);
btnQuit = (ImageButton)findViewById(R.id.img_btn);
//判斷記住密碼多選框的狀態(tài)
if(sp.getBoolean("ISCHECK", false))
{
//設(shè)置默認(rèn)是記錄密碼狀態(tài)
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
//判斷自動(dòng)登陸多選框狀態(tài)
if(sp.getBoolean("AUTO_ISCHECK", false))
{
//設(shè)置默認(rèn)是自動(dòng)登錄狀態(tài)
auto_login.setChecked(true);
//跳轉(zhuǎn)界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
}
// 登錄監(jiān)聽(tīng)事件 現(xiàn)在默認(rèn)為用戶名為:liu 密碼:123
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString();
if(userNameValue.equals("liu")&&passwordValue.equals("123"))
{
Toast.makeText(LoginActivity.this,"登錄成功", Toast.LENGTH_SHORT).show();
//登錄成功和記住密碼框?yàn)檫x中狀態(tài)才保存用戶信息
if(rem_pw.isChecked())
{
//記住用戶名、密碼、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
//跳轉(zhuǎn)界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish();
}else{
Toast.makeText(LoginActivity.this,"用戶名或密碼錯(cuò)誤,請(qǐng)重新登錄", Toast.LENGTH_LONG).show();
}
}
});
//監(jiān)聽(tīng)記住密碼多選框按鈕事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {
System.out.println("記住密碼已選中");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("記住密碼沒(méi)有選中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
//監(jiān)聽(tīng)自動(dòng)登錄多選框事件
auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (auto_login.isChecked()) {
System.out.println("自動(dòng)登錄已選中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
} else {
System.out.println("自動(dòng)登錄沒(méi)有選中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
});
btnQuit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
LogoActivity.java
package com.liu.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.opengl.ETC1;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class LogoActivity extends Activity {
private ProgressBar progressBar;
private Button backButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去除標(biāo)題
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.logo);
progressBar = (ProgressBar) findViewById(R.id.pgBar);
backButton = (Button) findViewById(R.id.btn_back);
Intent intent = new Intent(this, WelcomeAvtivity.class);
LogoActivity.this.startActivity(intent);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
WelcomeActivity.java
package com.liu.activity;
import android.app.Activity;
import android.os.Bundle;
public class WelcomeAvtivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
}
}
布局文件:
login.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/logo_bg" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageButton android:id="@+id/img_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/quit"/> <TextView android:id="@+id/tv_zh" android:layout_width="wrap_content" android:layout_height="35dip" android:layout_marginLeft="12dip" android:layout_marginTop="10dip" android:gravity="bottom" android:text="帳號(hào):" android:textColor="#000000" android:textSize="18sp" /> <EditText android:id="@+id/et_zh" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_below="@id/tv_zh" android:layout_marginLeft="12dip" android:layout_marginRight="10dip" /> <TextView android:id="@+id/tv_mima" android:layout_width="wrap_content" android:layout_height="35dip" android:layout_below="@id/et_zh" android:layout_marginLeft="12dip" android:layout_marginTop="10dip" android:gravity="bottom" android:text="密碼:" android:textColor="#000000" android:textSize="18sp" /> <EditText android:id="@+id/et_mima" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_below="@id/tv_mima" android:layout_marginLeft="12dip" android:layout_marginRight="10dip" android:maxLines="200" android:password="true" android:scrollHorizontally="true" /> <CheckBox android:id="@+id/cb_mima" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et_mima" android:layout_marginLeft="12dip" android:text="記住密碼" android:textColor="#000000" /> <CheckBox android:id="@+id/cb_auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb_mima" android:layout_marginLeft="12dip" android:text="自動(dòng)登錄" android:textColor="#000000" /> <Button android:id="@+id/btn_login" android:layout_width="80dip" android:layout_height="40dip" android:layout_below="@id/et_mima" android:layout_alignParentRight="true" android:layout_alignTop="@id/cb_auto" android:layout_marginRight="10dip" android:gravity="center" android:text="登錄" android:textColor="#000000" android:textSize="18sp"/> </RelativeLayout> </LinearLayout>
logo.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/logo_bg" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3"> <ProgressBar android:id="@+id/pgBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/pgBar" android:layout_centerHorizontal="true" android:text="正在登錄..." android:textColor="#000000" android:textSize="18sp" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/btn_back" android:layout_width="70dip" android:layout_height="35dip" android:text="取消" android:textColor="#000000" android:textSize="12sp" /> </LinearLayout> </LinearLayout>
welcome.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:background="@drawable/login_bg" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="登陸成功,進(jìn)入用戶界面" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout>
工程下載連接:android-SharedPreferences_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android個(gè)人手機(jī)通訊錄開(kāi)發(fā)詳解
在本篇文章里小編給大家分享了關(guān)于Android個(gè)人手機(jī)通訊錄開(kāi)發(fā)的步驟和相關(guān)源碼,有需要的朋友們學(xué)習(xí)下。2019-02-02
android實(shí)現(xiàn)圖片驗(yàn)證碼方法解析(自繪控件)
本文主要介紹了android自繪控件的應(yīng)用--實(shí)現(xiàn)圖片驗(yàn)證碼方法案例,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
Android開(kāi)發(fā)之ListView實(shí)現(xiàn)Item局部刷新
對(duì)于ListView數(shù)據(jù)的刷新大家都知道,改變Adapter的數(shù)據(jù)源,然后調(diào)用Adapter的notifyDateSetChanged()方法即可。通過(guò)本篇文章給大家詳細(xì)介紹Android開(kāi)發(fā)之ListView實(shí)現(xiàn)Item局部刷新,感興趣的朋友一起學(xué)習(xí)吧2015-10-10
Android PreferenceActivity與PreferenceFragment詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android PreferenceActivity與PreferenceFragment詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android開(kāi)發(fā)之針對(duì)聯(lián)系人的封裝
本文給大家分享的是如何在Android開(kāi)發(fā)中封裝聯(lián)系人模塊以及封裝后的使用及總結(jié),最后奉上代碼,有需要的小伙伴可以參考下。2016-02-02
Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Android圓形控件實(shí)現(xiàn)畫(huà)圓效果
這篇文章主要為大家詳細(xì)介紹了Android圓形控件實(shí)現(xiàn)畫(huà)圓效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android中外接鍵盤(pán)的檢測(cè)的實(shí)現(xiàn)
這篇文章主要介紹了Android中外接鍵盤(pán)的檢測(cè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android 顯示和隱藏軟鍵盤(pán)的方法(手動(dòng))
在Android開(kāi)發(fā)中,經(jīng)常會(huì)有一個(gè)需求,做完某項(xiàng)操作后,隱藏鍵盤(pán),也即讓Android中的軟鍵盤(pán)不顯示。今天,和大家分享如何利用代碼來(lái)實(shí)現(xiàn)對(duì)Android的軟件盤(pán)的隱藏、顯示的操作2016-01-01

