android SharedPreferences设置初始密码,并修改密码
生活随笔
收集整理的這篇文章主要介紹了
android SharedPreferences设置初始密码,并修改密码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在很多應(yīng)用程序中,都需要注冊(cè)賬號(hào)和密碼,并且都會(huì)有一個(gè)初始密碼,剛好在剛做的APP中要實(shí)現(xiàn)這個(gè)功能,要APP實(shí)現(xiàn)具有初始密碼的功能,就是要判斷用戶是不是第一次使用這個(gè)APP,在保存用戶設(shè)置多用的是SharedPreferences這個(gè)來(lái)存儲(chǔ),所以在SharedPreferences用保存一個(gè)用戶使用APP的次數(shù),第一次使用的時(shí)候就保存一個(gè)初始密碼,其他時(shí)候就不保存。
兩個(gè)布局文件
<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="請(qǐng)輸入密碼"/><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> 驗(yàn)證密碼Activity
pref = getSharedPreferences("password", MODE_PRIVATE);//第一次打開,保存一個(gè)初始密碼 // SharedPreferences.Editor editor = getSharedPreferences("password", MODE_PRIVATE).edit();int count = pref.getInt("count", 0);System.out.println("count---" + count);Editor editor = pref.edit();//第一次打開應(yīng)用程序,設(shè)置默認(rèn)密碼為12345678if (count == 0) {editor.putString("pwd", "12345678");}editor.putInt("count", ++count);editor.commit();首先獲得一個(gè)只能本程序可讀的pref,讀取password中保存的count數(shù)據(jù),如果count的值為0,設(shè)置pwd的值為12345678,然后count的值自增,重新寫入SharedPreferences。
驗(yàn)證密碼:
<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("請(qǐng)輸入密碼").setMessage("密碼錯(cuò)誤").setPositiveButton("確定", null).show();return;獲取pwd的值并與輸入的值比較,密碼正確則彈出一個(gè)“密碼正確”。
修改密碼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, "原密碼錯(cuò)誤", Toast.LENGTH_SHORT).show();}很簡(jiǎn)單的一個(gè)判斷,就不細(xì)說(shuō)了。
demo下載地址:設(shè)置初始密碼
總結(jié)
以上是生活随笔為你收集整理的android SharedPreferences设置初始密码,并修改密码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: youbank是什么意思
- 下一篇: Android 利用shareprefe