Android-登录记住密码[SharedPreferences]

版权声明:Leo.All Rights Reserved. https://blog.csdn.net/qq_41113081/article/details/86666357

使用SharedPreferences实现的记住密码功能,运行截图:

 登录界面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context=".LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPassword"
            android:maxLines="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:textAllCaps="false"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录"
            android:textAllCaps="false"
            android:textSize="25sp" />

    </LinearLayout>
</LinearLayout>

登录活动逻辑:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText account;
    private EditText password;
    private CheckBox checkBox;
    private Button login;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private boolean isRemember;
    private Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        checkBox = findViewById(R.id.checkbox);
        login = findViewById(R.id.btn_login);
        login.setOnClickListener(this);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        editor = sharedPreferences.edit();
        isRemember = sharedPreferences.getBoolean("isRemember", false);
        intent = new Intent(LoginActivity.this, MainActivity.class);

        if (isRemember) {
            account.setText(sharedPreferences.getString("account", null));
            password.setText(sharedPreferences.getString("password", null));
            checkBox.setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        if (account.getText().toString().equals("admin") && password.getText().toString().equals("123")) {
            if (checkBox.isChecked()) {
                editor.putBoolean("isRemember", true)
                        .putString("account", account.getText().toString())
                        .putString("password", password.getText().toString());

            } else {
                editor.clear();
            }
            editor.apply();
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "账号或密码错误!", Toast.LENGTH_SHORT).show();
        }
    }
}

注意:要把启动活动变为登录的活动

猜你喜欢

转载自blog.csdn.net/qq_41113081/article/details/86666357