Android註冊登陸,SharedPreference儲存實現記住密碼
阿新 • • 發佈:2019-01-10
本文記錄了在AS環境下實現SharedPreference儲存記住密碼的功能。
首先實現佈局檔案:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="18sp" android:text="Accont:"/> <EditText android:id="@+id/account" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="18sp" android:text="PassWord:"/> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/rember_pass" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="Remember Password"/> </LinearLayout> <Button android:id="@+id/login" android:layout_height="60dp" android:layout_width="match_parent" android:text="Login" /> </LinearLayout>
效果圖:
之後進行程式碼邏輯編寫:
package com.example.qing.secondd; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class Main3Activity extends AppCompatActivity { private SharedPreferences pref; private SharedPreferences.Editor editor; private EditText accountEdit,passwordEdit; private Button login; private CheckBox remberPass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); pref=PreferenceManager.getDefaultSharedPreferences(this); accountEdit=(EditText)findViewById(R.id.account); passwordEdit=(EditText)findViewById(R.id.password); remberPass=(CheckBox) findViewById(R.id.rember_pass); login=(Button)findViewById(R.id.login); boolean isRemember=pref.getBoolean("remember_password",false); if(isRemember){ String account=pref.getString("account",""); String password=pref.getString("password",""); accountEdit.setText(account); passwordEdit.setText(password); remberPass.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String account=accountEdit.getText().toString(); String password=passwordEdit.getText().toString(); if(account.equals("admin")&&password.equals("123")){ editor=pref.edit(); if(remberPass.isChecked()){ editor.putBoolean("remember_password",true); editor.putString("account",account); editor.putString("password",password); } else{ editor.clear(); } editor.apply(); Toast.makeText(Main3Activity.this, "Login Success!", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(Main3Activity.this,"wrong!",Toast.LENGTH_LONG).show(); } } }); } }
本文是一個簡單實現登陸的模板,當用戶名:admin,密碼:123,密碼正確,顯示登陸成功。
在實際專案中密碼的儲存還是需要進行加密儲存。