用户登录保存数据实例(慕课笔记 使用SharedPreferences保存用户名)
学习视频之后自己操作时的笔记。
0.视频地址:http://www.imooc.com/video/3265
1.功能预览:

说明:1)输入错误用户名和密码,点击登录,弹出提示框“禁止登录”;
2)输入正确用户名和密码,点击登录,弹出提示框“登录成功”;
3)输入正确用户名和密码,并且勾选保存用户名,点击登录,弹出框显示“登录成功”,退出APP,再次打开,用户名已有。
2.具体布局:

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="用户名:" /> <EditText
android:id="@+id/etuserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10" /> <TextView
android:id="@+id/aa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etuserName"
android:text="密 码" /> <EditText
android:id="@+id/etuserPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etuserName"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/aa"
android:ems="10" > <requestFocus />
</EditText> <Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etuserPass"
android:layout_marginTop="62dp"
android:onClick="doClick"
android:text="登陆" /> <Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnLogin"
android:layout_alignBottom="@+id/btnLogin"
android:layout_toRightOf="@+id/btnLogin"
android:onClick="doClick"
android:text="取消" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnCancel"
android:layout_below="@+id/etuserPass"
android:layout_marginTop="15dp"
android:checked="false"
android:text="保存用户名" /> </RelativeLayout>
3.MainActivity.java:
public class MainActivity extends Activity {
EditText etUserName,etUserPass;
CheckBox chk;
SharedPreferences pref;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定对应布局控件
etUserName=(EditText)findViewById(R.id.etuserName);
etUserPass=(EditText)findViewById(R.id.etuserPass);
chk=(CheckBox)findViewById(R.id.checkBox1);
pref=getSharedPreferences("UserInfo",MODE_PRIVATE);
editor=pref.edit();
String name=pref.getString("userName", "");
if(name==null){
chk.setChecked(false);
}else{
chk.setChecked(true);
etUserName.setText(name);
}
}
//为按钮添加响应
public void doClick(View v){
switch(v.getId()){
case R.id.btnLogin:
//转成字符串进行判断
String name=etUserName.getText().toString().trim();
String pass=etUserPass.getText().toString().trim();
if("admin".equals(name)&&"123456".equals(pass)){
if(chk.isChecked()){
//用户名与密码均正确且保存用户名确认框处于选,
//则保存数据并提交到数据库
editor.putString("userName", name);
editor.commit();
}else{
editor.remove("userName");
editor.commit();
}
//加信息提示框
Toast.makeText(MainActivity.this, "登录成功",
Toast.LENGTH_LONG).show();
}else{Toast.makeText(MainActivity.this, "禁止登录",
Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
用户登录保存数据实例(慕课笔记 使用SharedPreferences保存用户名)的更多相关文章
- Redis缓存Mysql模拟用户登录Java实现实例[www]
Redis缓存Mysql模拟用户登录Java实现实例 https://jingyan.baidu.com/article/09ea3ede1dd0f0c0aede3938.html redis+mys ...
- JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- ubuntu14.04使用root用户登录桌面 分类: 学习笔记 linux ubuntu 2015-07-05 10:30 199人阅读 评论(0) 收藏
ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...
- python实现用户登录、注册实例
python面向函数式编程,模拟用户登录验证.注册的代码实现. 主要有以下两个文件: 1.user.txt文档文件,相当于数据库的用户信息表,主要是记录用户名和密码. 注意:1)此文档需要与.py文件 ...
- 使用SharePreferences存取数据(慕课笔记 )
0.视频地址:http://www.imooc.com/video/3265 1.使用SharePreferences存取数据: public class MainActivity extends A ...
- Redis缓存Mysql模拟用户登录Java实现实例
https://blog.csdn.net/suneclipse/article/details/50920396
- Android学习笔记-保存数据的实现方法2-SharedPreferences
Android下,数据的保存,前面介绍过了,把数据保存到内存以及SD卡上,这次我们就介绍一下,更为常用的采用SharedPreferences的方式来保存数据, 1,得到SharedPreferenc ...
- Java基础:String类详解,案例用户登录实现,案例手机号截取实现,案例敏感词替换实现;StringBuilder类详解,StringBuilder和String相互转换,附练习案例.
1.API 1.1 API概述-帮助文档的使用 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK ...
- DRF 商城项目 - 用户( 登录, 注册,登出,个人中心 ) 逻辑梳理
用户登录 自定义用户登录字段处理 用户的登录时通过 手机号也可以进行登录 需要重写登录验证逻辑 from django.contrib.auth.backends import ModelBacken ...
随机推荐
- 0003_Linux基础之常用命令
1.pwd:查看当前所在目录 2.cd :切换目录 3.ls:查看当前目录下的文件及文件夹: 4.ls -l :列出当前目录下文件及详细信息 drwxr-xr-x 第一个字符为d则 ...
- 数据结构&算法 索引
最近在看牛客网的算法课,准备开个坑,根据自己的理解,把课程讲到的内容列一个大纲,明年找工作的时候用. 再加上看算法第四版的内容,整理一套明年找工作能够直接拿出来用的代码. 此篇博文作为索引.
- [poj2891]Strange Way to Express Integers(扩展中国剩余定理)
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...
- asn编译常见报错
TypeError: unsupported operand type(s) for -: 'str' and 'int' 可能是该用列表的地方没用列表. 1. ’-‘不支持,需改为'_' asn1t ...
- 【机器学习】随机森林RF
随机森林(RF, RandomForest)包含多个决策树的分类器,并且其输出的类别是由个别树输出的类别的众数而定.通过自助法(boot-strap)重采样技术,不断生成训练样本和测试样本,由训练样本 ...
- [翻译]Nativescript 中 Web 视图与 Android/IOS 的双向通信
English document From http://shripalsoni.com/blog/nativescript-webview-native-bi-directional-communi ...
- Celery 基本使用
1. 认识 Celery Celery 是一个 基于 Python 开发的分布式异步消息任务队列,可以实现任务异步处理,制定定时任务等. 异步消息队列:执行异步任务时,会返回一个任务 ID 给你,过一 ...
- Find First and Last Position of Element in Sorted Array
问题:给定一个有序数组和一个目标值,输出目标值在数组中的起始位置和终止位置,如果目标值不在数组中,则输出[-1,-1] 示例: 输入:nums = [1,2,3,5,5,7] target = 5 输 ...
- PHP 扩展篇 _ 持续更新
记住这个网站:http://pecl.php.net/ PHP-Redis扩展更新时间:2019/05/06 PHP安装Redis 1:下载目前最新版的redis插件 wget http://pecl ...
- Java中对象拷贝的两种方式
引用的拷贝 //引用拷贝 private static void copyReferenceObject(){ Person p = new Person(23, "zhang") ...