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

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

概述效果图:记住密码后,再次登录就会出现账号密码,否则没有。分析:SharedPreferences可将数据存储到本地的配置文件中

效果图:

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

分析:

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,Toast.LENGTH_SHORT).show();    //存储CheckBox的状态    edit.putBoolean("isCheck",isCheck);    if(isCheck){      edit.putString("username",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完成记住账号密码的功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android中使用SharedPreferences完成记住账号密码的功能全部内容,希望文章能够帮你解决Android中使用SharedPreferences完成记住账号密码的功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144666.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存