利用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. IBMWebsphere 使用jar包删除文件

    1. 先使用ant打包一个jar包,删除其他不要的目录和文件,仅保留一个空的xxx.war文件夹("xxx"对应was上的工程安装根目录) 2. 在文件夹下新建一个META-INF ...

  2. 精确获取对象的类型:Object.prototype.toString()

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

  3. inline-block默认间距解决方法

    方法一: 父元素设置font-size: 0;  行内块元素有文字时再在该元素上设置font-size 方法二: 父元素设置word-spacing为负 方法三: Inline-block   元素浮 ...

  4. 子线程更新UI

    https://www.cnblogs.com/joy99/p/6121280.html

  5. 分布式文件管理系统MooseFS在centOS 7中的安装

    首先,MooseFS是做什么的在这边不做具体详述,这边主要记录一下我在自己部署MooseFS中遇到的问题和步骤(大部分参考的其他博客或者资料) 首先是准备资源,MooseFS的最新安装包可以去官网下载 ...

  6. Python之global

    1 Global The global statement and its nonlocal cousin are the only things that are remotely like dec ...

  7. 调试程序时找不到DLL的解决办法

    最近调试程序的经常弹出找不到DLL.只好一个个把DLL拷贝到程序目录下(我是拷贝到源文件目录,也有人说是Debug目录). 其实可以这么设置: 项目属性->配置属性->调试->工作目 ...

  8. 我所理解的monad(4):函子(functor)是什么--可把范畴简单的看成高阶类型

    大致介绍了幺半群(monoid)后,我们重新回顾最初引用wadler(haskell委员会成员,把monad引入haskell的家伙)的那句话: 现在我们来解读这句话中包含的另一个概念:自函子(End ...

  9. snv报错

    ERROR svn: E160028: Commit failed svn出现上面错误,先更新再提交即可解决.

  10. 在Unity中json文件的解析方式

    using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using ...