智能移动开发Android studio

数据存储

实例:实现一个登录界面
要求:
1.用户名
2.密码
3.记住密码
4.登录
5.方法一:把用户,密码,选择存储起来,读出来
6.方法二:用文件把用户,密码,选择存储起来,读出来

在这里插入图片描述

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/b"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="90dp"
        app:srcCompat="@drawable/a" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/edt_UserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"

            android:textColor="#000000" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="密 码:"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/edt_PassWord"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"

            android:textColor="#000000" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/cek_Save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="记住密码" />

    <Button
        android:id="@+id/btn_Save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#5267E6"
        android:text="登录(保存)SharedPreferences" />

    <Button
        android:id="@+id/btnSaveTxtFile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#5267E6"
        android:text="登录(保存)TXTFIle" />
    <Button
        android:id="@+id/btn_ReadTxtFile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#5267E6"
        android:text="读取TXTFIle" />
</LinearLayout>

MainActivity.java

package com.example.mysixapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    //1.定义变量
    private EditText edtUserName, edtPassWord;
    private CheckBox chkSave;
    private Button btnSave,btnSaveTxtFile,btnReadTxtFile;

    private Boolean checked;   //定义布尔变量



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //2.初始化变量
        edtUserName = findViewById(R.id.edt_UserName);
        edtPassWord = findViewById(R.id.edt_PassWord);
        chkSave = findViewById(R.id.cek_Save);
        btnSave = findViewById(R.id.btn_Save);
        checked = false;
        btnSaveTxtFile = findViewById(R.id.btnSaveTxtFile);
        btnReadTxtFile = findViewById(R.id.btn_ReadTxtFile);

        chkSave.setOnCheckedChangeListener(this);
        btnSave.setOnClickListener(this);
        btnSaveTxtFile.setOnClickListener(this);
        btnReadTxtFile.setOnClickListener(this);
        readSharedPreferences();
    }

    public void readSharedPreferences()
    {
        SharedPreferences sp = getSharedPreferences("mydata",MODE_PRIVATE);
        Boolean b=sp.getBoolean("checked",false);

        if(b)
        {
            edtUserName.setText(sp.getString("username",null));
            edtPassWord.setText(sp.getString("password",null));
            chkSave.setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_Save)
        {
            //edtUserName.getText().toString()+edtPassWord.getText().toString()+checked
            SharedPreferences sp =getSharedPreferences("mydata",MODE_PRIVATE);
            SharedPreferences.Editor ed = sp.edit();
            ed.putString("username",edtUserName.getText().toString());
            ed.putString("password",edtPassWord.getText().toString());
            ed.putBoolean("checked", checked);
            ed.commit();   //提交
            Toast.makeText(this,"保存成功!",Toast.LENGTH_LONG).show();
        }
        //实现方法
        if(v.getId()==R.id.btnSaveTxtFile)
        {
            try {    //抛出异常!!!
                FileOutputStream fos = openFileOutput("myfile.txt",MODE_PRIVATE);
                String str = edtUserName.getText().toString()+":"+edtPassWord.getText().toString()+":"+checked;
                fos.write(str.getBytes());
                fos.close();
                Toast.makeText(this,"保存TXT成功!",Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if(v.getId() == R.id.btn_ReadTxtFile)
        {
            try {
                FileInputStream fis = openFileInput("myfile.txt");
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                String str =new String(buffer);
                fis.close();
                Toast.makeText(this,str,Toast.LENGTH_LONG).show();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if(chkSave.isChecked())
        {
            checked =true;
        }
        else
        {
            checked = false;
        }
    }
}


在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

848698119

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

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

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

打赏作者

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

抵扣说明:

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

余额充值