【AndroidStudio】Android文件存储保存登录用户名和密码

MainActivity.java

package com.example.filesystem;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);
        String phone= sp.getString("phone_number","");
        String password = sp.getString("password_number", "");
        if(phone.equals("158")&&password.equals("123456")){
            Toast.makeText(MainActivity.this,"已自动填充账号和密码",Toast.LENGTH_SHORT).show();
            EditText phone_text = (EditText) findViewById(R.id.phone);
            EditText pass_text = (EditText) findViewById(R.id.pass);
            phone_text.setText(phone);
            pass_text.setText(password);
        }
    }

    public void lgoin(View view){
        EditText phone_text = (EditText) findViewById(R.id.phone);
        EditText pass_text = (EditText) findViewById(R.id.pass);
        Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();

        SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);

        SharedPreferences.Editor editor = sp.edit();
        editor.putString("phone_number", phone_text.getText().toString());              // 存入String类型数据
        editor.putString("password_number", pass_text.getText().toString());
        editor.commit();



        Intent intent = new Intent(MainActivity.this,MainActivity2.class);
        startActivity(intent);
    }
}

MainActivity2.java

package com.example.filesystem;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity2 extends AppCompatActivity {

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

layout > activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:clickable="true"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout3"
        android:layout_width="0dp"
        android:layout_height="373dp"
        android:layout_marginStart="68dp"
        android:layout_marginEnd="68dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:id="@+id/phone"
            android:layout_width="1000dp"
            android:layout_height="50dp"
            android:hint='手机号'
            android:maxLines='1'
            android:inputType="phone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/constraintLayout3" />

        <EditText
            android:id="@+id/pass"
            android:layout_width="1000dp"
            android:layout_height="50dp"
            android:layout_marginTop="100dp"
            android:hint='密码'
            android:maxLines='1'
            android:inputType="textPassword"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/phone" />


        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:onClick="lgoin"
            android:text="登录"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

layout > activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity2">

</androidx.constraintlayout.widget.ConstraintLayout>

运行效果

在这里插入图片描述

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

a9c93f2300

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

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

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

打赏作者

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

抵扣说明:

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

余额充值