在android开发中,我们做用户登录和注册的时候需要将用户名和密码保存,下次打开的时候记住应户名和密码。关于注册保存用户密码的方式和记住用户名和密码的保存方式有多种,这里介绍SharaedPreferenses保存方式。SharaedPreferenses是使用键值对的方式来存储数据的,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。

下面就通过一个小例子来实现这两项功能

1、注册的功能

布局文件

<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:orientation="vertical"tools:context=".RegisteActivity" ><TableLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:stretchColumns="1"><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="账号:"/><EditText android:id="@+id/account"android:layout_height="wrap_content"android:hint="请输入您的账号"/></TableRow><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="密码:"/><EditText android:id="@+id/passeord"android:layout_height="wrap_content"android:inputType="textPassword"/></TableRow></TableLayout><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="确定" /></LinearLayout>

在这里使用TableLayout使布局更加合理

package com.example.sharepreferences;import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class RegisteActivity extends Activity {private Button button;//private Button readButton;private EditText account;private EditText password;String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);account = (EditText) findViewById(R.id.account);password = (EditText) findViewById(R.id.passeord);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();editor.putString("name", account.getText().toString());editor.putString("password", password.getText().toString());editor.commit();finish();}});}}

在这个Activity中,通过按键的点击事件通过getSharedPreferences()方法指定一个data的文件,想这个文件里面添加name和password数据。通过查询data可以看到已经添加进去的数据。

这样数据就保存成功了。

2、实现对用户名和密码的保存

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:stretchColumns="1" ><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="账号:"/><EditText android:id="@+id/account"android:layout_height="wrap_content"android:hint="请输入您的账号"/></TableRow><TableRow ><TextViewandroid:layout_height="wrap_content"android:text="密码:"/><EditText android:id="@+id/passeord"android:layout_height="wrap_content"android:inputType="textPassword"/></TableRow><TableRow ><CheckBox android:id="@+id/remember_password"android:layout_height="wrap_content"/><TextViewandroid:layout_height="wrap_content"android:text="记住密码"/></TableRow><TableRow ><Button android:id="@+id/registe"android:layout_height="wrap_content"android:text="注册"/><Button android:id="@+id/login"android:layout_height="wrap_content"android:text="登陆"/></TableRow></TableLayout>

在布局中添加了一个CheckBox,用户通过是否选中的方式来选择是否保存账号和密码

Activity

package com.example.sharepreferences;import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;public class LoginActivity extends Activity{private EditText accountEdit;private EditText passwordEdit;private Button login;private SharedPreferences pref;private SharedPreferences.Editor editor;private CheckBox rememberpass;private Button registe;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login);accountEdit = (EditText) findViewById(R.id.account);passwordEdit = (EditText) findViewById(R.id.passeord);login = (Button) findViewById(R.id.login);rememberpass = (CheckBox) findViewById(R.id.remember_password);registe = (Button) findViewById(R.id.registe);registe.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(LoginActivity.this, RegisteActivity.class);startActivity(intent);}});pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());boolean isRemember = pref.getBoolean("remember_password", false);if(isRemember){//将账号和密码都设置在文本框内String account = pref.getString("account", "");String password = pref.getString("password", "");accountEdit.setText(account);passwordEdit.setText(password);rememberpass.setChecked(true);}login.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString account = accountEdit.getText().toString();String password = passwordEdit.getText().toString();SharedPreferences pre = getSharedPreferences("data", MODE_PRIVATE);if(account.equals(pre.getString("name", ""))&&password.equals(pre.getString("password", ""))){editor = pref.edit();if (rememberpass.isChecked()) {editor.putBoolean("remember_password", true);}else{editor.clear();}editor.commit();Intent intent = new Intent(LoginActivity.this, Mainactivity.class);startActivity(intent);finish();}else {Toast.makeText(LoginActivity.this, "用户名或密码不对", Toast.LENGTH_LONG).show();}}});}}

在这个Activity中获取一个pre,查询本地保存的账号和密码,并与用户输入的用户名和密码进行比较,匹配则登陆成功,否则不成功。

android SharedPreferences实现用户的注册和保存账号密码相关推荐

  1. android中注册的账号密码储存在,android SharedPreferences实现用户的注册和保存账号密码...

    在android开发中,我们做用户登录和注册的时候需要将用户名和密码保存,下次打开的时候记住应户名和密码.关于注册保存用户密码的方式和记住用户名和密码的保存方式有多种,这里介绍SharaedPrefe ...

  2. 搜狗浏览器怎么保存账号密码 搜狗浏览器保存账号密码教程

    搜狗浏览器是一款非常安全的电脑浏览器.该软件能够对用户密码有安全保障,下面小编就为您带来搜狗浏览器保存账号密码教程. 搜狗浏览器怎么保存账号密码 搜狗浏览器保存账号密码教程 搜狗浏览器怎么保存账号密码 ...

  3. linux的xshell怎么保存密码,Xshell保存账号密码方法

    Xshell安全的终端模拟器,用户可以轻松安全的从Windows PC上访问主机.经常使用xshell用户就会知道账号.密码是特别重要的东西,虽然xshell是可以自动登录的,但是在这个之前我们至少要 ...

  4. Android使用SharedPreferences保存账号密码

    有很多的应用都会有保存密码和账号的功能,比如QQ.接下来就讲讲使用SharedPreferences来保存密码和账号,也许有些人会考虑的数据库,但是我个人认为对于保存简单的数据,使用的数据库就大材小用 ...

  5. Android之模仿QQ保存账号密码

    先写个布局文件activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Relati ...

  6. 使用浏览器保存账号密码并不安全,你的密码可能被坏人记下

    很多人为了图个方便,喜欢使用浏览器保存账号和密码.绝大多数人认为,浏览器中的密码是加密存储的,没有什么安全隐患!然而,通过这篇文章告诉你,使用浏览器保存账号和密码并不安全!坏人要想查看你的密码非常简单 ...

  7. 取消微软应用edge自动保存账号密码

    今天在同事的edge浏览器登录了我自己的微软账号,用完后注销了账号,再次点登录的时候,发现账号还能看到,并且不需要再次输密码就能自动登录,这是一个极大的隐私隐患,而且账号不单单只关联了edge浏览器, ...

  8. git永久保存账号密码

    在git bash 中执行命令: git config --global credential.helper store 在输入一次账号密码就可以永久保存了,免去git重复输入账号密码操作

  9. 登录时本地保存账号密码及关闭ARC的方法

    对于登录时保存用户名和密码,苹果官方使用的是KeychainItemWrapper,但使用时有些不便,如在引入KeychainItemWrapper的类中都要关闭arc,不能自定义key,必须使用该类 ...

最新文章

  1. workerman在linux上怎么运行,linux系统中workerman的安装步骤
  2. python监控进程状态_python监控进程脚本
  3. 反网络爬虫以及解决方案
  4. linux ubuntu/deepin与Windows时间不同步解决办法(双系统)
  5. sql中exists,Intersect ,union 与union All的用法
  6. 当微信遇上 10 万战绩的「跳一跳」外挂,程序员还能“逍遥”多久?
  7. tomcat下如何才能运行shtml文件?
  8. linux网络 (三):网络测试
  9. jQuery中,$.extend,$obj.extend和$.fn.extend三者的区别
  10. 分享一个嘉立创封装库(内含AD和PADS两种格式)
  11. Android 应用程序签名
  12. cmd批处理剪切_批处理复制文件到剪切板,读取剪切板内容到目录
  13. 使用HTML5 select标签来实现更改网页背景颜色
  14. Serenity框架官方文档翻译前言(什么是Serenity平台)
  15. 人人都需要知道的理财知识
  16. 微信公众号(服务号)接入开发之微信授权登陆
  17. 简述raid 0 1 5 10的区别
  18. 在win20008上运行U890破解提示sorry,this application cannot run under a virtual machine
  19. IE不能下载MSG文件的解决方案
  20. 手机号码如何检测开通微信

热门文章

  1. AMBA、AHB、APB、AXI总线
  2. Linux / sudo、su、sudo su、sudo -i 使用和区别
  3. mysql 5.7 innodb 预热_mysql5.7 InnoDB数据表空间文件平滑迁移
  4. 对于ARM的启动,系统升级,烧写过程和文件系统等方面的总结分析
  5. linux进程命令解释,linux 进程命令top详解
  6. fclose会写入硬盘吗 linux_Qt linux文件同步写入
  7. Noip 2013 练习
  8. 【转载】Callable、FutureTask中阻塞超时返回的坑点
  9. 【Linux】——搭建redis
  10. iOS开发~sizeClass和autolayout