Android SharedPreferences实现记住密码功能

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xmlduxie.liziguo.mxl.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <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="帐号:" />

            <EditText
                android:id="@+id/zh"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

        <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="密码:" />

            <EditText
                android:id="@+id/mm"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="textPassword" />
        </LinearLayout>

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

            <CheckBox
                android:id="@+id/box"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="记住密码" />

            <Button
                android:id="@+id/bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登录" />
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity

package com.xmlduxie.liziguo.mxl;

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

public class MainActivity extends AppCompatActivity {
    private Button bt;
    private EditText te,tee;
    private CheckBox box;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt=findViewById(R.id.bt);
        te=findViewById(R.id.zh);
        tee=findViewById(R.id.mm);
        box=findViewById(R.id.box);
        SharedPreferences sharedp=getSharedPreferences("lhzlz",0);
        if(sharedp.getBoolean("jzmm",false)){
            te.setText(sharedp.getString("zh",null));
            tee.setText(sharedp.getString("mm",null));
            box.setChecked(true);

        }

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor sharedped=getSharedPreferences("lhzlz",0).edit();
                if(box.isChecked()){
                    sharedped.putString("zh",te.getText().toString());
                    sharedped.putString("mm",tee.getText().toString());
                    sharedped.putBoolean("jzmm",true);
                }else{
//                    sharedped.remove("zh");//删除字段zh
//                    sharedped.remove("mm");
                    sharedped.clear();//删除全部内容
                }
                sharedped.apply();//应用
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/u010756046/article/details/80407457