Android sharedPreferences实现记住密码功能
程序员文章站
2023-11-17 18:33:40
实现记住密码功能,供大家参考,具体内容如下
编写界面交互代码:
package com.example.bz0209.login;
impor...
实现记住密码功能,供大家参考,具体内容如下
编写界面交互代码:
package com.example.bz0209.login; import android.content.dialoginterface; import android.content.intent; import android.content.sharedpreferences; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.toast; public class mainactivity extends appcompatactivity { private edittext username; private edittext password; private checkbox ck; private sharedpreferences sharedpreferences; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); sharedpreferences=getsharedpreferences("remeberword",mode_private); boolean isremeber=sharedpreferences.getboolean("remeberword",false); if(isremeber) { string name=sharedpreferences.getstring("name",""); string pass=sharedpreferences.getstring("pass",""); username.settext(name); password.settext(pass); ck.setchecked(true); } } private void initview(){ username=(edittext)findviewbyid(r.id.username); password=(edittext)findviewbyid(r.id.password); ck= (checkbox) findviewbyid(r.id.ck); } private void login(view view){//在layout中设置onclick事件 string name=username.gettext().tostring(); string pass=password.gettext().tostring(); if("admin".equals(name)&&"123".equals(pass)){ sharedpreferences.editor editor=sharedpreferences.edit(); if(ck.ischecked()){ editor.putboolean("remeberword",true); editor.putstring("name",name); editor.putstring("pass",pass); }else{ editor.clear(); } editor.commit(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。