Skip to content

Commit

Permalink
实现记住密码功能
Browse files Browse the repository at this point in the history
  • Loading branch information
salmonzhang committed Dec 9, 2019
1 parent 381076f commit 56f4462
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.broadcastbestpractice;

import android.content.Intent;
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;

Expand All @@ -13,6 +16,9 @@ public class LoginActivity extends BaseActivity {
private EditText mAccountEdit;
private EditText mPasswordEdit;
private Button mLogin;
private SharedPreferences mPref;
private CheckBox mRememberPass;
private SharedPreferences.Editor mEditor;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -22,12 +28,32 @@ protected void onCreate(Bundle savedInstanceState) {
mPasswordEdit = (EditText) findViewById(R.id.password);
mLogin = (Button) findViewById(R.id.login);

mPref = PreferenceManager.getDefaultSharedPreferences(this);
mRememberPass = (CheckBox) findViewById(R.id.remember_pass);
boolean isRemember = mPref.getBoolean("remember_password", false);
if (isRemember) {
String accout = mPref.getString("account", "");
String password = mPref.getString("password", "");
mAccountEdit.setText(accout);
mPasswordEdit.setText(password);
mRememberPass.setChecked(true);
}

mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = mAccountEdit.getText().toString();
String password = mPasswordEdit.getText().toString();
if ("admin".equals(account) && "123456".equals(password)) {
mEditor = mPref.edit();
if (mRememberPass.isChecked()) {
mEditor.putBoolean("remember_password", true);
mEditor.putString("account", account);
mEditor.putString("password", password);
} else {
mEditor.clear();
}
mEditor.apply();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/remember_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:textAllCaps="false"
android:text="Remember password"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
Expand Down

0 comments on commit 56f4462

Please sign in to comment.