安卓对本地文件的存储和读取,实现密码自动填充

安卓对本地文件的存储和读取,实现密码自动填充

package com.example.filelianxi;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SharedPreferencesTest extends AppCompatActivity {
  private EditText et_pwd;
   private String pwd;
   private  SharedPreferences pref;
   private SharedPreferences.Editor editor;
   private EditText accountEdit;
   private EditText passwordEdit;
   private CheckBox remember_pass;
   private Button login;
   private  String account;
   private  String password;
    @Override
    protected void onCreate(Bundle saveadInstanceState){
        super.onCreate(saveadInstanceState);
        setContentView(R.layout.activity_shared_preferences_test);
        Button saveData = findViewById(R.id.save_data);
        //et_pwd =findViewById(R.id.et_pwd);

        pref = PreferenceManager.getDefaultSharedPreferences(this);
        accountEdit = findViewById(R.id.et_name);
        passwordEdit = findViewById(R.id.et_pwd);
        remember_pass = findViewById(R.id.remember_pass);
        login = findViewById(R.id.login);

        boolean isRemember = pref.getBoolean("Remmember_pwd",false);
        if (isRemember) {
            String account =pref.getString("account","");
            String password = pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            remember_pass.setChecked(true);
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account =accountEdit.getText().toString();
                 password =passwordEdit.getText().toString();
                if (account.equals("admin")&&password.equals("123456")){
                    editor = pref.edit();
                    if (remember_pass.isChecked()){
                        editor.putBoolean("Remmember_pwd",true);
                        editor.putString("account",account);
                        editor.putString("password",password);
                    }else{
                        editor.clear();
                    }
                    editor.apply();
                    Toast.makeText(SharedPreferencesTest.this,"登录成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SharedPreferencesTest.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
            }

        });


        saveData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putString("name","tom");
                    editor.putInt("age",28);
                    editor.putString("pwd","123456789");
                    editor.putBoolean("married",false);
                    editor.apply();
                    editor.commit();

            }
        });
        Button restoreData = findViewById(R.id.restore_data);
        restoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
                String name = pref.getString("name",null);
                int age = pref.getInt("age",0);
                boolean married = pref.getBoolean("married",false);
                pwd = pref.getString("pwd",null);
                Log.d("Maintivity",name+age+married+pwd);
                Log.d("shu",pwd+"");

            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:textSize="20sp"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/et_name"
        android:text=""
        />
    <EditText
        android:textSize="20sp"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:password="true"
        android:id="@+id/et_pwd"
        android:text=""
        />
    <CheckBox
        android:id="@+id/remember_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存密码"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/login"
        android:text="登录"
        />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="重置"
    android:id="@+id/save_data"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:id="@+id/restore_data"
        />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信工院李平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值