安卓系统中使用SharedPreferences完成记住账号密码的功能

  

效果图:

  

 Android中使用SharedPreferences完成记住账号密码的功能“> <img src=

  

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

  


  

  

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

  

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

  

  

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

        私人SharedPreferences配置;   配置=getSharedPreferences(“配置”,MODE_PRIVATE);      

2,添加编辑器

        编辑器编辑=config.edit ();      

3,向内存中写入数据

        字符串的用户名=et_username.getText () .toString ();   .toString字符串密码=et_password.getText () ();   编辑。putString(“用户名”,用户名)。putString(“密码”,密码);      

4,提交到本地

        edit.commit ();之前      

代码:   

fry.Activity01         包煎;   进口com.example.rememberUserAndPassword.R;   进口android.app.Activity;   进口android.content.SharedPreferences;   进口android.content.SharedPreferences.Editor;   进口android.os.Bundle;   进口android.view.View;   进口android.widget.Button;   进口android.widget.CheckBox;   进口android.widget.TextView;   进口android.widget.Toast;   公开课Activity01延伸活动{   私人按钮btn_login;   私人TextView et_username;   私人TextView et_password;   私人复选框cb_choose;   私人SharedPreferences配置;   @Override   受保护的空白   公共空间>   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   面向android:=按怪薄北?   EditText & lt;   android: id=癅 + id/et_username”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "/比;   EditText & lt;   android: id=癅 + id/et_password”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: ems=" 10 "比;   & lt; requestFocus/比;   & lt;/EditText>   LinearLayout & lt;   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   比;   & lt;复选框   android: id=癅 + id/cb_choose”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "/比;   & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="记住密码”/比;   & lt;/LinearLayout>   & lt; !——android: onClick=" onClick "点击时去类中调用onClick方法,权限要为公众——比;   & lt;按钮   android: id=癅 + id/btn_login”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="登录”   android: layout_gravity=" center_horizontal "   android: onClick=" onClick "/比;   & lt;/LinearLayout>      

  

以上所述是小编给大家介绍的Android中使用SharedPreferences完成记住账号密码的功能,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

安卓系统中使用SharedPreferences完成记住账号密码的功能