在很多应用程序中,都需要注册账号和密码,并且都会有一个初始密码,刚好在刚做的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"><TextViewandroid: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下载地址:设置初始密码

android SharedPreferences设置初始密码,并修改密码相关推荐

  1. Mysql设置初始化密码和修改密码

     转自:Mysql设置初始化密码和修改密码 刚开始创建mysql的时候,一般需要修改密码:而且,本人碰到过在几台服务器上面操作,忘记了其中一台的root密码(用户密码记得,root特别久没用了), ...

  2. mysql修改root密码及修改密码过程中报错的解决方案

    mysql修改root密码及修改密码过程中报错的解决方案 参考文章: (1)mysql修改root密码及修改密码过程中报错的解决方案 (2)https://www.cnblogs.com/codema ...

  3. MySQL忘记密码怎么修改密码

    MySQL的 root 帐号密码默认为空,经常都有修改密码后忘记密码的事.如果忘记了root 帐号密码,那该怎么修改密码呢?这里有一个可行的方法,就是在MySQL安全模式下(跳过权限检查)修改密码的方 ...

  4. jwt用户注销 PHP,关于JWT用户主动注销、强制登出、忘记密码、修改密码的一些思考...

    JWT(JSON WEB TOKEN)的特点是无状态,通常用来作为验证登录以及鉴权,在这一方面,JWT体现出了他的优点. 然而,如果使用JWT实现,用户主动注销.强制登出(禁止登陆).忘记密码.修改密 ...

  5. centos查看密码及修改密码Host key verification failed

      大家好,我是爱编程的喵喵.双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中.从事机器学习以及相关的前后端开发工作.曾在阿里云.科大讯飞.CCF等比赛获得多次Top名次.现 ...

  6. ubuntu忘记旧密码怎么修改密码?系统关机关不掉?

    ** ubuntu忘记旧密码怎么修改密码?系统关机关不掉? ** 忘记旧密码怎么修改新密码? 1.重启ubuntu 2.按e键 3.找到 ro quiet splash $vt_handoff 修改为 ...

  7. Android移动开发之【Android实战项目】DAY12-App实现修改密码

    1.实现修改密码界面,主要是让用户在必要时修改自己的原始密码,从而保证用户信息的安全性. 2.界面效果如下:效果如下: <?xml version="1.0" encodin ...

  8. Android音乐播放器开发(4)—修改密码

    1. 说明 本音乐播放器基于Android开发,原为我和另外两个小伙伴在上学期间一起做的一个小项目,近来有时间整理一下.之前我有文章已经介绍了播放界面的功能实现(Android音乐播放器开发),但介绍 ...

  9. linux 设置文件为可修改密码,linux 为用户设定、修改密码 passwd

    passwd 语法结构: 首先,大家一定要知道"男人"说的话是很负责任的,所以有事找"男人".让我们看看"男人"对这个 passwd 怎么说 ...

最新文章

  1. Oracle SQL 空值排序(Nulls)
  2. leetcode算法题--有效的括号
  3. UVa 11520 Fill the Square 填充正方形
  4. 代码 直接调节显示设备亮度_投影仪太暗怎么调整?如何给投影机增加亮度?颜色也能调吗?这项功能必须要有...
  5. python 追加到字典_使用Python读取,写入和解析JSON
  6. text-overflow: ellipsis; 文本溢出隐藏
  7. Win10系统配置Python3.6+OpenGL环境详细步骤
  8. oracle怎么把in值列出来,oracle – SELECT的解析和绑定变量.. WHERE列IN(..)查询
  9. 鸟类的视力很惊人,如何从高空中发现食物
  10. linux下磁盘及文件系统基础知识(1)
  11. 个人档案php源码,php人事档案管理系统
  12. 极致CMS v1.7版本-一款完全开源,免授权商用,功能强大的PHP建站系统
  13. abap --MOVE-CORRESPONDING
  14. SDKMAN 安装软件失败
  15. oracle存储过程的赋值,ORACLE_存储过程_赋值
  16. 论文研读-多目标优化中的多源选择迁移框架
  17. 2020年中国羊肉行业供需现状、进出口情况及产业链分析,新西兰为羊肉主要进口国「图」
  18. 使用git_blame定位修改代码历史
  19. 【Wireshark】快速上手Wireshark
  20. linux应用程序的编写实验原理,操作系统实验 1.在linux下编写一个应用程序 联合开发网 - pudn.com...

热门文章

  1. OS / 5 种 IO 模型
  2. OS / Linux / Ubuntu 创建应用的快捷方式
  3. TCP/IP / SYN 攻击以及解决办法
  4. 判断字符串格式_Blind_pwn之格式化字符串
  5. 青岛智能物联网产才融合中心成立,山东大学携手百度飞桨海尔海纳云等共育AI人才
  6. San CLI 4.0 升级指南
  7. 如何做好Code Review
  8. 代码检查规则背景及总体介绍
  9. 团队任务4:第二次冲刺(Beta)
  10. Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS