当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android 使用SharedPreferrences储存密码登录界面记住密码功能

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

android存储方式有很多种,在这里所用的存储方式是sharedpreferrences, 其采用了map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入。所以比较适合我们今天做的这个项目。我们来看一下运行图:

一.布局界面

1.login_top.xml

 <?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="@dimen/activity_horizontal_margin"
 android:background="@drawable/logintop_roundbg">
 <edittext
 android:id="@+id/etname"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:drawablepadding="10dp"
 android:background="@android:drawable/edit_text"
 android:drawableleft="@drawable/icon_user"
 android:hint="@string/etname">
 <requestfocus></requestfocus>
 </edittext>
 <edittext
 android:id="@+id/etpassword"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/etname"
 android:inputtype="textpassword"
 android:ems="10"
 android:drawablepadding="10dp"
 android:background="@android:drawable/edit_text"
 android:drawableleft="@drawable/icon_pass"
 android:hint="@string/etpassword">
 <requestfocus></requestfocus>
 </edittext>
 <checkbox
 android:id="@+id/cbremenber"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/etpassword"
 android:text="@string/cbpass"/>
 <linearlayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/cbremenber">
 <button
  android:id="@+id/btnlogin"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/btnselect"
  android:text="@string/btnlogin"
  android:onclick="login"/>
 <button
  android:id="@+id/btnregister"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/btnselect"
  android:text="@string/btnregister"
  android:layout_marginleft="10dp"/>
 </linearlayout>
</relativelayout>

2.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/loginbg"
 tools:context="cn.edu.bzu.logindemo.mainactivity">
 <include layout="@layout/login_top"></include>
 <imageview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/deer"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_alignparentend="true" />
</relativelayout>

3.activity_welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_welcome"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="cn.edu.bzu.logindemo.welcomeactivity">
 <textview
 android:id="@+id/tvwelcome"
 android:text="welcome you"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignparenttop="true"
 android:layout_centerhorizontal="true"
 android:layout_margintop="200dp"
 android:textsize="40sp"
  />
</relativelayout>

二.mainactivity

public class mainactivity extends appcompatactivity {
 private edittext etname;
 private edittext etpassword;
 private sharedpreferences sharedpreferences;
 private checkbox cbremenber;
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 initviews();
 sharedpreferences=getsharedpreferences("remenberpassword", context.mode_private);
 boolean isremember=sharedpreferences.getboolean("remenberpassword",false);
 if(isremember) {
  string name = sharedpreferences.getstring("name", "");
  string password = sharedpreferences.getstring("password", "");
  etname.settext(name);
  etpassword.settext(password);
  cbremenber.setchecked(true);
 }
 }
 private void initviews() {
 etname=(edittext) findviewbyid(r.id.etname);
 etpassword=(edittext) findviewbyid(r.id.etpassword);
 cbremenber=(checkbox)findviewbyid(r.id.cbremenber);
 }
 public void login(view view){
 string name=etname.gettext().tostring();
 string password=etpassword.gettext().tostring();
 if("admin".equals(name)&&"123456".equals(password)){
  sharedpreferences.editor editor= sharedpreferences.edit();
  if(cbremenber.ischecked()){
  editor.putboolean("remenberpassword",true);
  editor.putstring("name",name);
  editor.putstring("password",password);
  }else {
  editor.clear();
  }
  editor.commit();
  intent intent=new intent(this,welcomeactivity.class);
  startactivity(intent);
  finish();
 }else {
  toast.maketext(this,"账号或密码有误",toast.length_long).show();
 }
 }
}

三.welcomeactivity

 public class welcomeactivity extends appcompatactivity {
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_welcome);
 }
}

以上所述是小编给大家介绍的android 使用sharedpreferrences储存密码登录界面记住密码,希望对大家有所帮助

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

相关文章:

验证码:
移动技术网