android中注册的账号密码储存在,android SharedPreferences实现用户的注册和保存账号密码...
在android开发中,我们做用户登录和注册的时候需要将用户名和密码保存,下次打开的时候记住应户名和密码。关于注册保存用户密码的方式和记住用户名和密码的保存方式有多种,这里介绍SharaedPreferenses保存方式。SharaedPreferenses是使用键值对的方式来存储数据的,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data//shared_prefs目录下。
下面就通过一个小例子来实现这两项功能
1、注册的功能
布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegisteActivity" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
android:layout_height="wrap_content"
android:text="账号:"/>
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="请输入您的账号"/>
android:layout_height="wrap_content"
android:text="密码:"/>
android:id="@+id/passeord"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定" />
在这里使用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";
@Override
protected 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() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.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、实现对用户名和密码的保存
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
android:layout_height="wrap_content"
android:text="账号:"/>
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="请输入您的账号"/>
android:layout_height="wrap_content"
android:text="密码:"/>
android:id="@+id/passeord"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
android:id="@+id/remember_password"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:text="记住密码"/>
android:id="@+id/registe"
android:layout_height="wrap_content"
android:text="注册"/>
android:id="@+id/login"
android:layout_height="wrap_content"
android:text="登陆"/>
在布局中添加了一个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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent 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() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String 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中注册的账号密码储存在,android SharedPreferences实现用户的注册和保存账号密码...相关推荐
- android中注册的账号密码储存在,Android中使用SharedPreferences完成记住账号密码的功能...
效果图: 记住密码后,再次登录就会出现账号密码,否则没有. 分析: SharedPreferences可将数据存储到本地的配置文件中 SharedPreferences会记录CheckBox的状态,如 ...
- android SharedPreferences实现用户的注册和保存账号密码
在android开发中,我们做用户登录和注册的时候需要将用户名和密码保存,下次打开的时候记住应户名和密码.关于注册保存用户密码的方式和记住用户名和密码的保存方式有多种,这里介绍SharaedPrefe ...
- 【Android 安装包优化】WebP 图片格式兼容与性能 ( Android 中的 WebP 图片格式兼容问题 | Android 中的 WebP 图片格式性能 )
文章目录 一.Android 中的 WebP 图片格式兼容问题 二.Android 中的 WebP 图片格式性能 三.参考资料 一.Android 中的 WebP 图片格式兼容问题 在 Android ...
- android中服务播放音乐,为什么我们需要Android中的服务?音乐播放也可以通过后台线程完成...
采访中有人问我这个问题.我不确定这是否是问这个问题的合适论坛,因为它不涉及任何代码,而是对android概念的理解. 问题是"当所有事情都可以通过Android中的后台线程完成时,为什么我们 ...
- android中白色怎么表示,通知栏图标在android 5中变成白色
接受的答案不完全正确.当然,它会使通知图标显示颜色,但是这样做有一个很大的缺点-将目标SDK设置为比AndroidLolliop低! 如果您按照建议将目标SDK设置为20来解决您的白色图标问题,您的应 ...
- android中接口的作用是什么意思,Android为什么要序列化
android为什么要序列化?什么是序列化,怎么进行序列化 why 为什么要了解序列化?-- 进行Android开发的时候,无法将对象的引用传给Activities或者Fragments,我们需要将这 ...
- android中获取应用程序(包)的信息,Android中获取应用程序(包)的信息PackageManager的使用(一).doc...
Android中获取应用程序(包)的信息PackageManager的使用(一) 本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等 ...
- 在android中在屏幕密度为160,在 android 中,在屏幕密度为160时,1pt 大概等于多少sp...
满意答案 han6039626 2013.08.11 采纳率:51% 等级:7 已帮助:158人 展开全部 在 Android 中, 1pt 大概等于 2.22sp以上供参考, 与分辨率无关的度 ...
- android中的mvp模式怎么定义,在android中使用MVP模式
1.MVP介绍java 随着UI建立技术的功能日益加强,UI层也履行着愈来愈多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专一于处理数 据的可视化以及与用户的交互,同时 ...
最新文章
- RStudio环境或者ggsave函数保存生成的图像为指定文件格式(pdf、jpeg、tiff、png、svg、wmf)、指定图像宽度、高度、分辨率(width、height、dpi)
- 2017 3月16日,上午
- 南京微盟计算机,南京微盟 ME6118A50B3G ME6119A33PG ME6119A50PG 稳压IC
- 宝塔同时安装苹果cms海洋cms_★苹果cms常见问题有哪些?100个常见问题的解决方法...
- delphi windows编程_2020年值得关注的新编程V语言Vlang,对标Golang、Rust、Swift
- spark sql定义RDD、DataFrame与DataSet
- 面向对象程序设计的术语
- 视频移动侦测VMD的实现
- 34. Differentiate between inheritance of interface and inheritance ofimplementations
- ubuntu phpmyadmin 404 not found
- leetcode 868. Binary Gap
- 七夕烟花c语言程序,C语言七夕必备神器,待那烟花灿烂时,依旧做个单身狗
- 路由器安装教程和使用方法
- 5、Hive数据仓库——Hive分区及动态分区
- 为防止办公用计算机上的数据,2019年9月计算机一级Ms Office提分练习题(总)
- Java将英文句子分解为单词
- STM32F103时钟系统
- 【Cherno的OpenGL视频】Vertex buffers and drawing a triangle in OpenGL
- 弘辽科技:胡润研究院发布《2020胡润中国10强电商》榜单,第二名很意外
- Ultimate Developer PC 2.0-第3部分-有关构建WEI 7.9和RFC(用于构建GOM)的更新(上帝拥有的机器)...
热门文章
- Soul网关源码学习(14)- hystrix,resilienc4j,sentinel 插件的使用和对比
- MyBatis 与 MyBatis-Plus 的区别
- 使用Termux在安卓手机上搭建本地Git服务器
- 使用VSLinux插件开发和调试
- windows下安装VMware Workstation14.0Pro(VMware系列一)
- CC00417.CloudKubernetes——|KuberNetesNetworkPolicy.V09|——|NetworkPolicy.v09|隔离中间件服务.v05|
- Node.js 环境搭建过程中可能遇到的问题解决方案
- Excel数据透视表: GetPivotData
- 苹果电脑常用的计算机英语怎么说,史上最强苹果电脑Mac Pro发布 有啥亮点(中英文)...
- echarts修改标题字体大小、颜色、位置、内容