Android简易实战教程--第十六话《SharedPreferences保存用户名和密码》
之前在Android简易实战教程--第七话《在内存中存储用户名和密码》
那里是把用户名和密码保存到了内存中,这一篇把用户名和密码保存至SharedPreferences文件。为了引起误导,声明实际开发中不会用到这两种方式,这里指示提供一种思路和给初学者学习简单的api。
由于内容和之前的基本一样,不做过多的解释。直接上代码:
xml文件:
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
> <EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
/>
<EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名和密码"
android:layout_centerVertical="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="登录"
android:layout_alignParentRight="true"
android:onClick="login"
/>
</RelativeLayout>
</LinearLayout>
接着是mainactivity:
package com.itydl.rwinrom; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader; import org.apache.http.entity.InputStreamEntity; import com.itheima.sharedpreference.R; import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name;
private EditText et_pass; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name);
et_pass = (EditText) findViewById(R.id.et_pass); readAccount();//打开程序回显保存的数据 } public void readAccount(){
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
String name = sp.getString("name", "");//get数据的时候,不用判定文件是否存在了。因为文件没有文件的话就得带一个空字符串 ""
String pass = sp.getString("pass", ""); et_name.setText(name);
et_pass.setText(pass);
} public void login(View v){ String name = et_name.getText().toString();
String pass = et_pass.getText().toString(); CheckBox cb = (CheckBox) findViewById(R.id.cb);
//判断选框是否被勾选
if(cb.isChecked()){
//使用sharedPreference来保存用户名和密码
//路径在data/data/com.itydl.sharedpreference/share_
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
//拿到sp的编辑器
Editor ed = sp.edit();
//使用编辑器存取数据,数据是以键值对的方式存取的
ed.putString("name", name);
ed.putString("pass", pass);
//提交
ed.commit();
} //创建并显示吐司对话框
Toast.makeText(this, "登录成功", 0).show();
} }
保存的路径截图:
运行结果:
再次进入该程序后,数据也会回显成功
Android简易实战教程--第十六话《SharedPreferences保存用户名和密码》的更多相关文章
- Android简易实战教程--第二十六话《网络图片查看器在本地缓存》
本篇接第二十五话 点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...
- Android简易实战教程--第二十九话《创建图片副本》
承接第二十八话加载大图片,本篇介绍如何创建一个图片的副本. 安卓中加载的原图是无法对其修改的,因为默认权限是只读的.但是通过创建副本,就可以对其做一些修改,绘制等了. 首先创建一个简单的布局.一个放原 ...
- Android简易实战教程--第十五话《在外部存储中读写文件》
第七话里面介绍了在内部存储读写文件 点击打开链接. 这样有一个比较打的问题,假设系统内存不够用,杀本应用无法执行,或者本应用被用户卸载重新安装后.以前保存的用户名和密码都不会得到回显.所以,有必要注意 ...
- Android简易实战教程--第二十八话《加载大图片》
Android系统以ARGB表示每个像素,所以每个像素占用4个字节,很容易内存溢出.假设手机内存比较小,而要去加载一张像素很高的图片的时候,就会因为内存不足导致崩溃.这种异常是无法捕获的 内存不足并不 ...
- Android简易实战教程--第二十五话《网络图片查看器》
访问网络已经有了很成熟的框架.这一篇只是介绍一下HttpURLConnection的简单用法,以及里面的"注意点".这一篇可以复习或者学习HttpURLConnection.han ...
- Android简易实战教程--第二十四话《画画板》
今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》
昨晚写博客太仓促,代码结构有问题,早上测试发现没法监听文本变化!今日更改一下.真心见谅啦,哈哈!主活动的代码已经改好了,看截图这次的确实现了文本监听变化情况. 监听文本输入情况,仅仅限于土司略显low ...
- Android简易实战教程--第十八话《ListView显示,简单的适配器SimpleAdapter》
本篇介绍Listview的显示,对于listview有许多的适配器,如ArrayAdapter,BaseAdapter,SimpleAdapter等等.本篇先热身一下,介绍最简单的SimpleAdap ...
- Android简易实战教程--第十四话《模仿金山助手创建桌面Widget小部件》
打开谷歌api,对widget小部件做如下说明: App Widgets are miniature application views that can be embedded in otherap ...
随机推荐
- USACO 2017 US Open
只会做T1,FallDream T2 n^2暴力AC,太强啦. T1.Modern Art 题目大意:有一个n*n的矩阵,一开始都是0,你有n^2种颜色,编号1到n^2,每次可以选出一种颜色涂满一个子 ...
- 2015 多校联赛 ——HDU5389(dp)
Sample Input 4 3 9 1 1 2 6 3 9 1 2 3 3 5 2 3 1 1 1 1 1 9 9 9 1 2 3 4 5 6 7 8 9 Sample Output 1 0 1 ...
- 使用PL/SQL Developer连接远程DB(本机不安装Oracle客户端)
本文内容亲测可行环境: 服务端:centos6.7 ,oracle 11g r2 ,动态注册,监听端口号:1521 用户端:win7 ,没有安装ORACLE及其客户端 准备文件:plsql ...
- URI与URL
为了区分URI与URL,我们要引入URN URI = Universal Resource Identifier 统一资源标志符URL = Universal Resource Locator 统一资 ...
- TensorFlow-Slim image classification library:TensorFlow-Slim 图像分类库
TensorFlow-Slim 图像分类库 TF-slim是用于定义,训练和评估复杂模型的TensorFlow(tensorflow.contrib.slim)的新型轻量级高级API. 该目录包含用于 ...
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- 07_Linux目录文件操作命令4解压缩,文件查找_我的Linux之路
这一节还是一样学习操作目录文件的命令 在这一节,我会讲到解压压缩tar以及zip命令,以及文本查找命令grep tar 打包压缩命令 tar命令可以为linux的文件和目录创建档案 首先要弄清两个概念 ...
- sea.js及三种加载方式的异同
一.前言 浏览器本身并不提供模块管理的机制,过去网页开发中,为了使用各种模块,不得不在加入一大堆script标签.这样就使得网页体积臃肿,难以维护,还产生大量的HTTP请求,拖慢显示速度, ...
- spark升级后 集成hbase-1.0.0-cdh5.4.5异常
.具体场景如下: spark1.6 升级 spark2.2 后 分析查询hbase 数据报异常: 具体错误如下: ERROR TableInputFormat: java.io ...
- Window下使用ftp命令往Linux中发送文件
操作步骤:首先,切换到文件目录1.ftp ip地址2.连接成功后,输入正确的用户名和密码.3.binary(表示以二进制的格式传送)4.put/get 文件名(或文件的绝对路径) 退出:bye