Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能
本文實(shí)例為大家分享了Android SharedPreferences保存登錄數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:

程序運(yùn)行顯示一個(gè)登陸框,用戶名輸入admin,密碼輸入123456會(huì)提示登錄成功,如果不是則提示不正確,如果勾選保存用戶名,在下一個(gè)程序打開時(shí),用戶名會(huì)自動(dòng)讀取并顯示。
1.activity_main.xml頁面存放所有的控件,我在每一行都使用了線性布局。
activity_main.xml頁面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" tools:context=".SecondActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" > <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" /> <EditText android:id="@+id/etInputName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:orientation="horizontal" > <TextView android:id="@+id/tvPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 碼:" /> <EditText android:id="@+id/etInputPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:orientation="horizontal" > <CheckBox android:id="@+id/cbSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="保存用戶名" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp" android:orientation="horizontal" > <Button android:id="@+id/btLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登錄" /> <Button android:id="@+id/btCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </RelativeLayout>
2.MainActivity.java頁面處理登錄和保存數(shù)據(jù)。
MainActivity.java頁面:
package com.example.sharedpreferences;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
SharedPreferences pref;
Editor editor;
private EditText etInputName,etInputPass;
private CheckBox ckSave;
private Button btLogin,btCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 獲取控件id
*/
getId();
/**
* 保存數(shù)據(jù)
*/
saveData();
/**
* 綁定點(diǎn)擊事件
*/
bindClick();
}
/**
* 獲取控件id
*/
private void getId() {
etInputName=(EditText) findViewById(R.id.etInputName);
etInputPass=(EditText) findViewById(R.id.etInputPass);
ckSave=(CheckBox) findViewById(R.id.cbSave);
btLogin=(Button) findViewById(R.id.btLogin);
btCancel=(Button) findViewById(R.id.btCancel);
}
/**
* 保存數(shù)據(jù)
*/
private void saveData() {
//獲得SharedPreferences對(duì)象
pref=getSharedPreferences("userInfo",MODE_PRIVATE);//將內(nèi)容存放到名為userInfo的文檔內(nèi)
//獲得SharedPreferences.Editor對(duì)象
editor=pref.edit();
String name=pref.getString("userName","");//獲取用戶名
if(name.equals("")){//如果name為空,代表未選擇保存用戶名
ckSave.setChecked(false);//不勾選
}else{
ckSave.setChecked(true);
etInputName.setText(name);//將讀取到的name值賦值到EditText中
}
}
/**
* 綁定點(diǎn)擊事件
*/
private void bindClick() {
btLogin.setOnClickListener(this);
btCancel.setOnClickListener(this);
}
/**
* 按鈕點(diǎn)擊事件
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btLogin:
String name=etInputName.getText().toString().trim();//獲取輸入的名字并去掉空格
String pass=etInputPass.getText().toString().trim();//獲取輸入的密碼并去掉空格
if("admin".equals(name)&&"123456".equals(pass)){
if(ckSave.isChecked()){//如果選擇保存用戶名
editor.putString("userName",name);
editor.commit();//提交數(shù)據(jù)
}else{//如果未選擇保存用戶名
editor.remove("userName");//刪除用戶名
editor.commit();//提交數(shù)據(jù)(每次更改都需要提交)
}
Toast.makeText(SecondActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SecondActivity.this,"用戶名或密碼不正確",Toast.LENGTH_SHORT).show();
}
break;
case R.id.btCancel:
break;
}
}
}
3.保存的文件目錄可以查看的到,點(diǎn)擊右上角進(jìn)入,找到data->data->當(dāng)前目錄的包名->shared-prefs->新建的文件名

4.另外,點(diǎn)擊右上角導(dǎo)出可以暫時(shí)保存到桌面,然后選擇打開方式可以查看里邊信息。
5.還有一點(diǎn)是,當(dāng)程序在真機(jī)上運(yùn)行時(shí),file explore打不開data文件夾,根據(jù)網(wǎng)上的經(jīng)驗(yàn),真機(jī)先root,然后在手機(jī)上裝上R.E 管理器(或類似軟件),將/data/data的權(quán)限修改為可讀可寫可執(zhí)行,然后,就可以在eclipse中展開了。

6.SharedPreferences多用于配置信息或者內(nèi)容較少的數(shù)據(jù)的保存,當(dāng)數(shù)據(jù)量復(fù)雜或者較大,還是需要使用數(shù)據(jù)庫。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Libgdx解決部分Android機(jī)型鎖屏崩潰的方法
今天小編就為大家分享一篇關(guān)于Libgdx解決部分Android機(jī)型鎖屏崩潰的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android編程仿Iphone拖動(dòng)相片特效Gallery的簡單應(yīng)用示例
這篇文章主要介紹了Android編程仿Iphone拖動(dòng)相片特效Gallery的簡單應(yīng)用,結(jié)合實(shí)例形式分析了Android圖形拖動(dòng)特效的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android 顯示刷新頻率的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 顯示刷新頻率的實(shí)現(xiàn)代碼,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實(shí)現(xiàn)方法
這篇文章主要介紹了Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Android Scroll滑動(dòng)效果實(shí)例
這篇文章主要為大家分享了Android Scroll滑動(dòng)效果實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Android 三種動(dòng)畫詳解及簡單實(shí)例
這篇文章主要介紹了Android 三種動(dòng)畫詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果
這篇文章主要介紹了Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果,本文通過實(shí)例代碼結(jié)合文字說明的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-03-03

