android SharedPreferences设置初始密码,并修改密码

在很多应用程序中,都需要注册账号和密码,并且都会有一个初始密码,刚好在刚做的APP中要实现这个功能,要APP实现具有初始密码的功能,就是要判断用户是不是第一次使用这个APP,在保存用户设置多用的是SharedPreferences这个来存储,所以在SharedPreferences用保存一个用户使用APP的次数,第一次使用的时候就保存一个初始密码,其他时候就不保存。

两个布局文件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dp"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.   
  12.     <LinearLayout   
  13.         android:id="@+id/linearlayout"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_centerInParent="true"  
  17.         android:orientation="horizontal">  
  18.         <TextView   
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="请输入密码"/>  
  22.         <EditText   
  23.             android:id="@+id/password"  
  24.             android:layout_width="0dp"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_weight="1"  
  27.             android:background="@android:drawable/editbox_background"/>  
  28.     </LinearLayout>  
  29.     <Button   
  30.         android:id="@+id/check_pwd"  
  31.         android:layout_width="match_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_below="@id/linearlayout"  
  34.         android:text="确定"/>  
  35.       
  36.     <Button   
  37.         android:id="@+id/setting_pwd"  
  38.         android:layout_width="match_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_below="@id/check_pwd"  
  41.         android:text="修改密码"/>  
  42.   
  43. </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>
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dp">  
  6.      <TextView  
  7.          android:layout_width="match_parent"  
  8.          android:layout_height="wrap_content"  
  9.          android:gravity="center"  
  10.          android:textSize="20sp"  
  11.          android:text="修改密码"/>  
  12.     <TableLayout   
  13.         android:id="@+id/setting_pwd"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_centerInParent="true">  
  17.         <TableRow >  
  18.             <TextView   
  19.                 android:layout_width="wrap_content"  
  20.                 android:layout_height="wrap_content"  
  21.                 android:textSize="20sp"  
  22.                 android:text="原密码:"/>  
  23.             <EditText   
  24.                 android:id="@+id/pwd_0"  
  25.                 android:layout_width="wrap_content"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:layout_weight="1"  
  28.                 android:inputType="textPassword"  
  29.                 android:background="@android:drawable/editbox_background"/>  
  30.         </TableRow>  
  31.         <TableRow >  
  32.             <TextView   
  33.                 android:layout_width="wrap_content"  
  34.                 android:layout_height="wrap_content"  
  35.                 android:textSize="20sp"  
  36.                 android:text="新密码:"/>  
  37.             <EditText   
  38.                 android:id="@+id/pwd_1"  
  39.                 android:layout_width="wrap_content"  
  40.                 android:layout_height="wrap_content"  
  41.                 android:layout_weight="1"  
  42.                 android:inputType="textPassword"  
  43.                 android:background="@android:drawable/editbox_background"/>  
  44.         </TableRow>  
  45.         <TableRow >  
  46.             <TextView   
  47.                 android:layout_width="wrap_content"  
  48.                 android:layout_height="wrap_content"  
  49.                 android:textSize="20sp"  
  50.                 android:text="确定密码:"/>  
  51.             <EditText   
  52.                 android:id="@+id/pwd_2"  
  53.                 android:layout_width="wrap_content"  
  54.                 android:layout_height="wrap_content"  
  55.                 android:layout_weight="1"  
  56.                 android:inputType="textPassword"  
  57.                 android:background="@android:drawable/editbox_background"/>  
  58.         </TableRow>  
  59.     </TableLayout>  
  60.     <Button   
  61.         android:id="@+id/setting_button"  
  62.         android:layout_width="match_parent"  
  63.         android:layout_height="wrap_content"  
  64.         android:layout_below="@id/setting_pwd"  
  65.          android:textSize="20sp"  
  66.         android:text="确定"/>  
  67. </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

  1. pref = getSharedPreferences("password", MODE_PRIVATE);  
  2.         //第一次打开,保存一个初始密码  
  3. //      SharedPreferences.Editor editor = getSharedPreferences("password", MODE_PRIVATE).edit();  
  4.         int count = pref.getInt("count"0);  
  5.         System.out.println("count---" + count);  
  6.         Editor editor = pref.edit();  
  7.         //第一次打开应用程序,设置默认密码为12345678  
  8.         if (count == 0) {  
  9.             editor.putString("pwd""12345678");  
  10.         }  
  11.         editor.putInt("count", ++count);  
  12.         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。

验证密码:

  1. <span style="white-space:pre">        </span>String input = editTextName.getText().toString();  
  2.             pref.getString("pwd""");  
  3.             System.out.println("密码----" + pref.getString("pwd"""));  
  4.             if (input.equals(pref.getString("pwd"""))) {  
  5.                 Toast.makeText(this"密码正确!", Toast.LENGTH_SHORT).show();  
  6.             }else {  
  7.                 new AlertDialog.Builder(MainActivity.this)  
  8.                 .setTitle("请输入密码")  
  9.                 .setMessage("密码错误")  
  10.                 .setPositiveButton("确定"null)  
  11.                 .show();  
  12.                 return;  
  13.           
<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

  1. <span style="white-space:pre">            </span>SharedPreferences pref = getSharedPreferences("password", MODE_PRIVATE);  
  2.             if (current_pwd.getText().toString().equals(pref.getString("pwd"""))) {  
  3.                 if (new_pwd1.getText().toString().equals(new_pwd2.getText().toString()) && new_pwd1.getText().toString().length() != 0) {  
  4.                     SharedPreferences.Editor editor = getSharedPreferences("password", MODE_PRIVATE).edit();  
  5.                     editor.putString("pwd", new_pwd1.getText().toString());  
  6.                     editor.commit();  
  7.                     Toast.makeText(this"密码修改成功", Toast.LENGTH_SHORT).show();  
  8.                     finish();  
  9.                 }else {  
  10.                     Toast.makeText(this"两次密码不匹配", Toast.LENGTH_SHORT).show();  
  11.                 }  
  12.             }else {  
  13.                 Toast.makeText(this"原密码错误", Toast.LENGTH_SHORT).show();  
  14.             }  
<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下载地址:设置初始密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值