android SharedPreferences设置初始密码,并修改密码
来源:互联网 发布:好莱坞艳照事件 知乎 编辑:程序博客网 时间:2024/05/17 05:00
在很多应用程序中,都需要注册账号和密码,并且都会有一个初始密码,刚好在刚做的APP中要实现这个功能,要APP实现具有初始密码的功能,就是要判断用户是不是第一次使用这个APP,在保存用户设置多用的是SharedPreferences这个来存储,所以在SharedPreferences用保存一个用户使用APP的次数,第一次使用的时候就保存一个初始密码,其他时候就不保存。
两个布局文件
<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" android:padding="10dp" 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" > <LinearLayout android:id="@+id/linearlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入密码"/> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:drawable/editbox_background"/> </LinearLayout> <Button android:id="@+id/check_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/linearlayout" android:text="确定"/> <Button android:id="@+id/setting_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/check_pwd" android:text="修改密码"/></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="20sp" android:text="修改密码"/> <TableLayout android:id="@+id/setting_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true"> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="原密码:"/> <EditText android:id="@+id/pwd_0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textPassword" android:background="@android:drawable/editbox_background"/> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="新密码:"/> <EditText android:id="@+id/pwd_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textPassword" android:background="@android:drawable/editbox_background"/> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="确定密码:"/> <EditText android:id="@+id/pwd_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textPassword" android:background="@android:drawable/editbox_background"/> </TableRow> </TableLayout><Button android:id="@+id/setting_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/setting_pwd" android:textSize="20sp" android:text="确定"/></RelativeLayout>验证密码Activity
pref = getSharedPreferences("password", MODE_PRIVATE);//第一次打开,保存一个初始密码//SharedPreferences.Editor editor = getSharedPreferences("password", MODE_PRIVATE).edit();int count = pref.getInt("count", 0);System.out.println("count---" + count);Editor editor = pref.edit();//第一次打开应用程序,设置默认密码为12345678if (count == 0) {editor.putString("pwd", "12345678");}editor.putInt("count", ++count);editor.commit();首先获得一个只能本程序可读的pref,读取password中保存的count数据,如果count的值为0,设置pwd的值为12345678,然后count的值自增,重新写入SharedPreferences。
验证密码:
<span style="white-space:pre"></span>String input = editTextName.getText().toString(); pref.getString("pwd", ""); System.out.println("密码----" + pref.getString("pwd", "")); if (input.equals(pref.getString("pwd", ""))) { Toast.makeText(this, "密码正确!", Toast.LENGTH_SHORT).show();}else {new AlertDialog.Builder(MainActivity.this).setTitle("请输入密码").setMessage("密码错误").setPositiveButton("确定", null).show();return;获取pwd的值并与输入的值比较,密码正确则弹出一个“密码正确”。
修改密码Activity
<span style="white-space:pre"></span>SharedPreferences pref = getSharedPreferences("password", MODE_PRIVATE);if (current_pwd.getText().toString().equals(pref.getString("pwd", ""))) {if (new_pwd1.getText().toString().equals(new_pwd2.getText().toString()) && new_pwd1.getText().toString().length() != 0) {SharedPreferences.Editor editor = getSharedPreferences("password", MODE_PRIVATE).edit();editor.putString("pwd", new_pwd1.getText().toString());editor.commit();Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();finish();}else {Toast.makeText(this, "两次密码不匹配", Toast.LENGTH_SHORT).show();}}else {Toast.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();}很简单的一个判断,就不细说了。
demo下载地址:设置初始密码
0 0
- android SharedPreferences设置初始密码,并修改密码
- android SharedPreferences设置初始密码,并修改密码
- Mysql修改初始密码/忘记密码
- mysql 修改密码 修改root初始密码
- mysql默认密码忘记,如何修改初始密码
- 设置ubuntu root默认密码(初始密码)
- 安装Mysql5.7并修改初始密码
- 批量创建linux用户并设置初始密码
- ubuntu root默认密码(初始密码)的修改
- mysql-5.7.19-winx64 初始密码登录以及修改密码
- Centos7.3 安装Mysql5.7并修改初始密码
- Centos7.3 安装Mysql5.7并修改初始密码
- Android通过修改配置文件设置wifi密码
- Android修改、设置锁屏密码
- Android中 记住密码(SharedPreferences)
- MYSQL修改密码并设置允许远程访问
- Mysql 修改root密码,并设置远程访问
- Android登录用户名记住密码——偏好设置(SharedPreferences)
- ServletContextListener使用详解
- 方式
- 至自己的两个故事
- UML概述
- VS2010开发Qt程序,显示qDebug信息(添加DOS窗口)
- android SharedPreferences设置初始密码,并修改密码
- DevExpress GridView 两个问题
- java对redis的基本操作
- hdu 1754 I hate it(线段树单点更新)
- Mac下svn提交.a文件
- GLUT 回调函数小结
- cordova(phonegap)搭建和使用sqlite数据库
- PAT.Advanced.P1068
- window.showModalDialog以及window.open用法简介