Android中 記住密碼(SharedPreferences)

來源:互聯網
上載者:User

標籤:記住密碼   sharedpreferences   xml   

    Android中登入介面的記住密碼功能實現,將使用者輸入的帳號和密碼以SharedPreferences方式儲存(注意的是,密碼要用MD5明文加密)。


介面xml檔案:

<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"    tools:context=".MainActivity" >    <EditText        android:id="@+id/et_qq"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="請輸入您的qq號碼" />    <EditText        android:inputType="textPassword"        android:id="@+id/et_password"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/et_qq"        android:hint="請輸入密碼" />    <CheckBox        android:id="@+id/cb_remeber_pwd"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/et_password"        android:text="記住密碼" />    <LinearLayout        android:layout_below="@id/cb_remeber_pwd"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/bt_ok"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="確定" />    </LinearLayout></RelativeLayout>


MainActivity.java檔案:

import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;/*** *  *1.建立一個SharedPreferences *2.初始化SharedPreferences  參數1 sp的檔案名稱 參數2 sp的儲存模式 *3.向sp裡面儲存資料 首先 擷取一個文字編輯器 Editor *4.儲存完畢資料記得執行commint()儲存資料 *5.讀取資料 sp.getString() sp.getInt(); */public class MainActivity extends Activity {private EditText et_qq;private EditText et_password;private CheckBox cb_remeber_pwd;private Button bt_ok;/** * android系統下用於資料存放區的一個方便的API */private SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 完成sp的初始化。sp = getSharedPreferences("config", MODE_PRIVATE);et_qq = (EditText) findViewById(R.id.et_qq);et_password = (EditText) findViewById(R.id.et_password);cb_remeber_pwd = (CheckBox) findViewById(R.id.cb_remeber_pwd);//擷取sp裡面儲存的資料String savedQQ = sp.getString("qq", "");String savedPassword = sp.getString("password", "");et_qq.setText(savedQQ);et_password.setText(savedPassword);bt_ok = (Button) findViewById(R.id.bt_ok);// 給按鈕註冊一個點擊事件。bt_ok.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String qq = et_qq.getText().toString();String password = et_password.getText().toString();if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(password)) {Toast.makeText(getApplicationContext(),"對不起,qq號" + "或者密碼不可為空", 0).show();} else {// 檢查使用者是否勾選了 記住密碼的選項。if (cb_remeber_pwd.isChecked()) {// 說明勾選框被選中了。把使用者名稱和密碼給記錄下來// 擷取到一個參數檔案的編輯器。Editor editor = sp.edit();editor.putString("qq", qq);editor.putString("password", MD5utils.encode(password));// 把資料給儲存到sp裡面editor.commit();Toast.makeText(getApplicationContext(), "使用者資訊已經儲存", 1).show();}}}});}}


MainActivity.java調用的MD5utils.java檔案:

package com.itheima.qqlogin;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import android.os.Message;public class MD5utils {/** * md5加密的工具類 *  * @param password * @return */public static String encode(String password) {try {MessageDigest digest = MessageDigest.getInstance("md5");byte[] results = digest.digest(password.getBytes());StringBuilder sb = new StringBuilder();for(byte b : results){int number = b&0xff;String hex = Integer.toHexString(number);if(hex.length()==1){sb.append("0");}sb.append(hex);}return sb.toString();} catch (Exception e) {e.printStackTrace();return "";}}}

帳號和密碼的儲存路徑如下:


匯出config.xml檔案,可以看到使用者密碼的MD5明文加密前後的xml檔案裡的資料:


Android中 記住密碼(SharedPreferences)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.