Android SharedPreferences实现记住密码功能

本文展示了一个使用XML定义的简单登录界面布局示例,并通过Java代码实现了利用SharedPreferences存储和读取用户登录信息的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局

<?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();//应用
            }
        });
    }
}

第一个参数getSharedPreferences(第一个参数,第二个参数);

第一个参数是存储时的名称,第二个参数则是文件的打开方式~

 两个参数,第一个参数是preferece的名称(比如:MyPref),第二个参数是打开的方式(一般选择private方式)

这样的话,就可以这样调用getSharedPreferences("lhzlz",Context.MODE_PRIVATE);得到你想要的。。。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值