版权声明:本文为博主原创文章,未经博主允许不得转载。
在很多应用程序中,都需要注册账号和密码,并且都会有一个初始密码,刚好在刚做的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>
<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>
<?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();
- //第一次打开应用程序,设置默认密码为12345678
- if (count == 0) {
- editor.putString("pwd", "12345678");
- }
- editor.putInt("count", ++count);
- editor.commit();
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();
//第一次打开应用程序,设置默认密码为12345678
if (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;
<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();
- }
<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下载地址:设置初始密码