利用SharedPreferences完成记住账号密码的功能
利用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完成记住账号密码的功能的更多相关文章
- css 修改placeholder字体颜色字体大小 修改input记住账号密码后的默认背景色
壹 ❀ 引 本来这个阶段的项目页面都是给实习生妹子做的,我只用写写功能接接数据,但这两天妹子要忙翻译,这个工作阶段也快结束了导致有点慌,只能自己把剩余的几个小页面给写了. 那么做页面的过程中,UI也 ...
- struts2的记住账号密码的登录设计
一个简单的基于struts2的登录功能,实现的额外功能有记住账号密码,登录错误提示.这里写上我在设计时的思路流程,希望大家能给点建设性的意见,帮助我改善设计. 登录功能的制作,首先将jsp界面搭建出来 ...
- 基于struts2的记住账号密码的登录设计
一个简单的基于struts2的登录功能,实现的额外功能有记住账号密码,登录错误提示.这里写上我在设计时的思路流程,希望大家能给点建设性的意见,帮助我改善设计. 登录功能的制作,首先将jsp界面搭建出来 ...
- C# ASP.NET MVC:使用Cookie记住账号密码
MVC记住账号密码 使用cookie操作 前端: <div> 用户名:<input type="text" id="UserName" val ...
- Linux让git记住账号密码
Linux让git记住账号密码 ——IT唐伯虎 摘要: Linux让git记住账号密码. 1.进入根目录,指令:cd / 2.创建记录账号密码的文件,指令:touch .git-credentials ...
- Git设置记住账号密码
Git设置记住账号密码 添加如下配置 [credential] helper = store
- cocos2d JS 本地缓存存储登陆记住账号密码->相当于C++中的UserDefault
在cocos-js 3.0以上的版本中,当我们用到本地存储的时候,发现以前用到的UserDefault在JS中并没有导出,而是换成了LocalStorage. 在LocalStorage.h文件中我们 ...
- OpenVPN记住账号密码自动连接
说明:在增加了证书+账号密码之后,安全性确实提高了,但是面临的问题也有,每次重启时必须输入账号密码才能连接,这也造成了无人值守的问题. 解决: 1.在Client的client.ovpn末尾添加一行a ...
- 利用python破解sqlserver账号密码
一.编写密码测试函数 在用python连接mssql数据库的时候,通常会使用pymssql模板中的connect函数,格式如下: connect(server,user,password,databa ...
随机推荐
- javascript跨域的几种方法
以下的例子包含的文件均为为 http://www.a.com/a.html .http://www.a.com/c.html 与 http://www.b.com/b.html,要做的都是从a.htm ...
- [原创]Linux(CentOS)下安装nodejs+express
网上找了很多步骤,各种问题,自己总结下吧 1.下载 wget --no-check-certificate https://nodejs.org/dist/v6.10.1/node-v6.10.1-l ...
- java Web(4)
Web 应用程序状态管理 通过隐藏表单域 hidden,cookie,session,重写URL来实现: cookie存在于客户端,浏览器关闭时失效 cookie原理:服务器在响应请求时将一些数据以“ ...
- Join 语句
select * from books as A join (select * from Orders) as B on A.BookId = B.BookId select A.BookId,Aut ...
- django分页功能,templatetags的应用
django 将不会将得到的html代码自动转化 from django.utils.html import format_html html =''' <a href='http://www. ...
- Java常量池详细说明
java常量池技术 java中的常量池技术,是为了方便快捷地创建某些对象而出现的,当需要一个对象时,就可以从池中取一个出来(如果池中没有则创建一个),则在需要重复创建相等变量时节省了很多时间.常量池 ...
- Linux之部署虚拟环境、安装系统
本章涵盖了Linux的优势和哲学思想,零基础详细记录了部署虚拟环境安装Linux系统,完整演示了VM虚拟机的安装与配置过程,以及Centos 7系统的安装.配置过程和初始化方法. Linux优势分析: ...
- Codeforces 805D/804B - Minimum number of steps
传送门:http://codeforces.com/contest/805/problem/D 对于一个由‘a’.‘b’组成的字符串,有如下操作:将字符串中的一个子串“ab”替换成“bba”.当字符串 ...
- Spring MVC学习总结(2)——Spring MVC常用注解说明
使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02 之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...
- POJ 3762 The Bonus Salary!
The Bonus Salary! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Origi ...