利用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, 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>

界面设计部分

利用SharedPreferences完成记住账号密码的功能的更多相关文章

  1. css 修改placeholder字体颜色字体大小 修改input记住账号密码后的默认背景色

     壹 ❀ 引 本来这个阶段的项目页面都是给实习生妹子做的,我只用写写功能接接数据,但这两天妹子要忙翻译,这个工作阶段也快结束了导致有点慌,只能自己把剩余的几个小页面给写了. 那么做页面的过程中,UI也 ...

  2. struts2的记住账号密码的登录设计

    一个简单的基于struts2的登录功能,实现的额外功能有记住账号密码,登录错误提示.这里写上我在设计时的思路流程,希望大家能给点建设性的意见,帮助我改善设计. 登录功能的制作,首先将jsp界面搭建出来 ...

  3. 基于struts2的记住账号密码的登录设计

    一个简单的基于struts2的登录功能,实现的额外功能有记住账号密码,登录错误提示.这里写上我在设计时的思路流程,希望大家能给点建设性的意见,帮助我改善设计. 登录功能的制作,首先将jsp界面搭建出来 ...

  4. C# ASP.NET MVC:使用Cookie记住账号密码

    MVC记住账号密码 使用cookie操作 前端: <div> 用户名:<input type="text" id="UserName" val ...

  5. Linux让git记住账号密码

    Linux让git记住账号密码 ——IT唐伯虎 摘要: Linux让git记住账号密码. 1.进入根目录,指令:cd / 2.创建记录账号密码的文件,指令:touch .git-credentials ...

  6. Git设置记住账号密码

    Git设置记住账号密码 添加如下配置 [credential] helper = store

  7. cocos2d JS 本地缓存存储登陆记住账号密码->相当于C++中的UserDefault

    在cocos-js 3.0以上的版本中,当我们用到本地存储的时候,发现以前用到的UserDefault在JS中并没有导出,而是换成了LocalStorage. 在LocalStorage.h文件中我们 ...

  8. OpenVPN记住账号密码自动连接

    说明:在增加了证书+账号密码之后,安全性确实提高了,但是面临的问题也有,每次重启时必须输入账号密码才能连接,这也造成了无人值守的问题. 解决: 1.在Client的client.ovpn末尾添加一行a ...

  9. 利用python破解sqlserver账号密码

    一.编写密码测试函数 在用python连接mssql数据库的时候,通常会使用pymssql模板中的connect函数,格式如下: connect(server,user,password,databa ...

随机推荐

  1. Spring Cloud (2) 服务消费者-基础

    LoadBalancerClient 使用Spring Cloud提供的负载均衡器客户端来实现服务的消费. 首先创建一个服务消费者工程,命名为com.david.consumer,并在pom.xml中 ...

  2. selenium获取页面通过样式隐藏获取不到元素解决方案

    如图更换图像这个按钮通过bottom:-30px隐藏了,通过如下代码获取不到页面元素,后台会报错 driver.findElement(By.className("js-avator-lin ...

  3. 极客学院免费VIP

    [手快福利]用我的链接注册极客学院,你我都能免费得30天VIP!6500+编程开发视频教程随便学,还能下载资料和源码 http://e.jikexueyuan.com/invite/index.htm ...

  4. c#同步锁Monitor.Enter(T)

    protected static object MObjLock = new object();//同步锁 public string GetData(int mId) { Monitor.Enter ...

  5. iOS 加密算法汇总

    CCCryptorStatus CCCryptorCreate( CCOperation op,             /* kCCEncrypt, etc. */ CCAlgorithm alg, ...

  6. SSM项目中表单分页操作(PageHepler使用)

    Maven pom.xml添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifa ...

  7. 【转载】解决方案:git@github.com出现Permission denied (publickey)

    遇到的问题 今天心血来潮,想将intellij上的项目代码放到GitHub上管理. 在进行添加远程库的时候,出现了:git@github.com出现Permission denied (publick ...

  8. [luogu1627 CQOI2009] 中位数 (乱搞)

    传送门 Solution 好水的题(ーー;) Code //By Menteur_Hxy #include <map> #include <queue> #include &l ...

  9. Python语言数据结构和语言结构(2)

    目录 1. Python预备基础 2. Python数据类型 3. Python条件语句 4. while循环和for循环 1. Python预备基础 1.1 变量的命名   变量命名规则主要有以下几 ...

  10. MyBatis学习总结(9)——使用MyBatis Generator自动创建代码

    一.构建一个环境 1. 首先创建一个表: [sql] view plaincopy CREATE TABLE t_user ( USER_ID INT NOT NULL AUTO_INCREMENT, ...