当前位置: 移动技术网 > 移动技术>移动开发>Android > Android SharedPreferences实现保存登录数据功能

Android SharedPreferences实现保存登录数据功能

2019年07月23日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了android sharedpreferences保存登录数据的具体代码,供大家参考,具体内容如下

目标效果: 

程序运行显示一个登陆框,用户名输入admin,密码输入123456会提示登录成功,如果不是则提示不正确,如果勾选保存用户名,在下一个程序打开时,用户名会自动读取并显示。

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页面处理登录和保存数据。

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();
 /**
 * 保存数据
 */
 savedata();
 /**
 * 绑定点击事件
 */
 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);
 }
 /**
 * 保存数据
 */
 private void savedata() {
 
 //获得sharedpreferences对象
 pref=getsharedpreferences("userinfo",mode_private);//将内容存放到名为userinfo的文档内
 
 //获得sharedpreferences.editor对象
 editor=pref.edit();
 
 string name=pref.getstring("username","");//获取用户名
 if(name.equals("")){//如果name为空,代表未选择保存用户名
 cksave.setchecked(false);//不勾选
 }else{
 cksave.setchecked(true);
 etinputname.settext(name);//将读取到的name值赋值到edittext中
 }
 }
 
 /**
 * 绑定点击事件
 */
 private void bindclick() {
 btlogin.setonclicklistener(this);
 btcancel.setonclicklistener(this);
 }
 
 /**
 * 按钮点击事件
 */
 @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();//提交数据
 }else{//如果未选择保存用户名
  editor.remove("username");//删除用户名
  editor.commit();//提交数据(每次更改都需要提交)
 }
 toast.maketext(secondactivity.this,"登录成功",toast.length_short).show();
 }else{
 toast.maketext(secondactivity.this,"用户名或密码不正确",toast.length_short).show();
 }
 break;
 case r.id.btcancel:
 break;
 }
 }
}

3.保存的文件目录可以查看的到,点击右上角进入,找到data->data->当前目录的包名->shared-prefs->新建的文件名

4.另外,点击右上角导出可以暂时保存到桌面,然后选择打开方式可以查看里边信息。

5.还有一点是,当程序在真机上运行时,file explore打不开data文件夹,根据网上的经验,真机先root,然后在手机上装上r.e 管理器(或类似软件),将/data/data的权限修改为可读可写可执行,然后,就可以在eclipse中展开了。


6.sharedpreferences多用于配置信息或者内容较少的数据的保存,当数据量复杂或者较大,还是需要使用数据库。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网