当前位置: 移动技术网 > IT编程>移动开发>Android > Android中使用SharedPreferences完成记住账号密码的功能

Android中使用SharedPreferences完成记住账号密码的功能

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

兴安证券大智慧,印度大吉岭40天骚乱持续,北京水云居

效果图:

记住密码后,再次登录就会出现账号密码,否则没有。

分析:

sharedpreferences可将数据存储到本地的配置文件中

sharedpreferences会记录checkbox的状态,如果checkbox被选,则将配置文件中记录的账号密码信息回馈给账号密码控件,否则清空。

sharedpreferences使用方法:

1、创建名为config的配置文件,并且私有

private sharedpreferences config;
config=getsharedpreferences("config", mode_private);

2、添加编辑器

editor edit=config.edit();

3、向内存中写入数据

string username=et_username.gettext().tostring();
string password=et_password.gettext().tostring();
edit.putstring("username", username).putstring("password", password);

4、提交到本地

edit.commit(); 

代码:

fry.activity01

package fry;
import com.example.rememberuserandpassword.r;
import android.app.activity;
import android.content.sharedpreferences;
import android.content.sharedpreferences.editor;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.checkbox;
import android.widget.textview;
import android.widget.toast;
public class activity01 extends activity{
  private button btn_login;
  private textview et_username;
  private textview et_password;
  private checkbox cb_choose;
  private sharedpreferences config;
  @override
  protected void oncreate(bundle savedinstancestate) {
    // todo auto-generated method stub
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity01);
    config=getsharedpreferences("config", mode_private);
    btn_login=(button) findviewbyid(r.id.btn_login);
    et_username=(textview) findviewbyid(r.id.et_username);
    et_password=(textview) findviewbyid(r.id.et_password);
    cb_choose=(checkbox) findviewbyid(r.id.cb_choose);
    //是否记住了密码,初始化为false
    boolean ischeck=config.getboolean("ischeck", false);
    //toast.maketext(this, ischeck+" ", toast.length_short).show();
    if(ischeck){
      et_username.settext(config.getstring("username", ""));
      et_password.settext(config.getstring("password", ""));
      cb_choose.setchecked(ischeck);
    }
  }
  //权限要是public,要不然访问不到
  //因为在button控件中设置了android:onclick="onclick"
  public void onclick(view view){
    toast.maketext(this, "登录成功", toast.length_short).show();
    editor edit=config.edit();
    string username=et_username.gettext().tostring();
    string password=et_password.gettext().tostring();
    boolean ischeck=cb_choose.ischecked();
    //toast.maketext(this, ischeck+" ", toast.length_short).show();
    //存储checkbox的状态
    edit.putboolean("ischeck", ischeck);
    if(ischeck){
      edit.putstring("username", username).putstring("password", password);
    }else{
      edit.remove("username").remove("password");
    }
    //提交到本地
    edit.commit();
  }
}

/记住账号和密码/res/layout/activity01.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <edittext 
    android:id="@+id/et_username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
  <edittext
    android:id="@+id/et_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >
    <requestfocus />
  </edittext>
  <linearlayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <checkbox 
        android:id="@+id/cb_choose"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
    <textview 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="记住密码"
      />
  </linearlayout>
  <!-- android:onclick="onclick" 点击时去class中调用onclick方法,权限要为public -->
  <button
    android:id="@+id/btn_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="登录"
    android:layout_gravity="center_horizontal"
    android:onclick="onclick"
    />
</linearlayout>

总结

以上所述是小编给大家介绍的android中使用sharedpreferences完成记住账号密码的功能,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网