移动应用开发 实验报告3 数据文件中的保存

一、实验要求

        通过线性布局相对布局来搭建一个用户登录界面,界面可自主设计,但至少需要包括以下控件:1个ImageView控件、2个TextView控件; 2个EditText控件、 1个Button控件。

        创建工具类FileSaveLogin,在该类中实现Login账号和密码存储与读取功能,使用文件存储的方式,保存与读取账号与密码信息。

        运行程序,输入账号和密码信息,点击“登录”按钮,实现登录功能。

二、实验结果展示

1.输入之后,很明显显示登录成功,保存成功

2.退出之后重新进入,输入的信息自动填充,证明保存确实成功了

3.以下是失败信息

三、代码展示

1. 主界面.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/lanse">

    <ImageView
        android:id="@+id/touxiang"
        android:layout_width="85dp"
        android:layout_height="96dp"
        android:background="@drawable/touxiang"
        android:layout_marginTop="100dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="TODO" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="280dp"
        android:background="@color/white">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="30sp"
            android:textColor="@color/teal_200"
            tools:ignore="VisualLintBounds" />
        <EditText
            android:id="@+id/account"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:textSize="30sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="340dp"
        android:background="@color/white">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="30sp"
            android:textColor="@color/teal_200"
            tools:ignore="VisualLintBounds" />
        <EditText
            android:id="@+id/password"
            android:inputType="textPassword"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:textSize="30sp"/>
    </LinearLayout>

    <Button
        android:id="@+id/denglu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="480dp"
        android:layout_centerHorizontal="true"
        android:textColor="@color/white"
        android:text="登录"
        android:textSize="30sp"/>
</RelativeLayout>

2.FileSaveLogin.java代码

package com.example.a3;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileSaveLogin {

    public static boolean save(Context context,String account,String password) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = context.openFileOutput("data.txt", Context.MODE_PRIVATE);
            fileOutputStream.write((account+":"+password).getBytes());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Map<String,String> getpassage(Context context) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = context.openFileInput("data.txt");
            byte[] buffer = new byte[fileInputStream.available()];
            fileInputStream.read(buffer);
            String content = new String(buffer);
            //用冒号分割开的一串字符串,使其变成两个,不然就要单独创建两个文件去收集,那样的话就太麻烦了
            String[] infos = content.split(":");
            //创建一个集合
            Map<String, String> map = new HashMap<String, String>();
            map.put("account", infos[0]);
            map.put("password", infos[1]);
            return map;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3.mainActivity.java代码

package com.example.a3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button denglu;
    EditText account, passward;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        denglu = findViewById(R.id.denglu);
        account = findViewById(R.id.account);
        passward = findViewById(R.id.password);
        //设置按钮监听事件
        denglu.setOnClickListener(this);
        //这里设置一个输入流,重启时会去保存的data.txt文件里面查询是否有上一次的账号密码,有的话就自动填上,没有就不变
        Map<String, String> map = FileSaveLogin.getpassage(this);
        if (map != null) {
            account.setText(map.get("account"));
            passward.setText(map.get("password"));
        }
    }

    @Override
    public void onClick(View view) {
        //这里登录其实就是保存按钮,调用输出流进行保存
        //先获取文本
        String a = account.getText().toString();
        String p = passward.getText().toString();
        //然后检查是否为空
        if (TextUtils.isEmpty(a)) {
            Toast.makeText(this,"请输入QQ号!",Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(p)) {
            Toast.makeText(this, "请输入密码!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"登录成功!",Toast.LENGTH_SHORT).show();
        }
        //保存账号和密码
        boolean b = FileSaveLogin.save(this, a, p);
        if (b) {
            Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"保存失败!",Toast.LENGTH_SHORT).show();
        }
    }
}

四、实验总结

1.带上static的方法才能在对应类中直接调用

2. TextUtils类是系统自带的一个工具类,里面包含了一些静态方法。是处理一些常见的有关Text的工具的集合方法类

它的构造方法是私有的,不能通过new来创建,它的方法都是static类型的,可以直接调用,构造函数私有化的意义不管声明几个对象,都只实例化了一个,也就是说,只占用了一个内存。

3.

editText可以设置inputType,其中可以设置textpassword,即密码格式,也就是全部都是****

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值