用户登录使用java的IO流实现将数据保存到data目录下

一、效果展示
1、最初可以看到data/data目录下该项目只有一个cache文件夹和lib文件
在这里插入图片描述
第一次运行程序,用户名和密码皆为空。
在这里插入图片描述
当我们输入用户名和密码后,点击登录项目目录下出现名为info.txt的文档,打开它可以看到原先输入的用户名和密码。
在这里插入图片描述

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

第二次运行,可以看到用户名和密码输入框内都已经有内容了
在这里插入图片描述

二、设计知识点
java 的IO流
checkbox单选框
三、实现步骤
1、编写布局文件,这里就不详细解释了,很简单的。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <EditText
                android:id="@+id/tv_username"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:hint="请输入用户名"

                />
        <EditText
                android:id="@+id/tv_pwd"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:inputType="textPassword"
                android:hint="请输入密码"
                />
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <CheckBox
                    android:id="@+id/cb_remember"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="记住用户名"/>
            <Button
                    android:id="@+id/btn_login"
                    android:onClick="login"
                    android:layout_alignParentRight="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="登录"/>
    </RelativeLayout>

</LinearLayout>

2、定义一个UserInfoUtils类,通过IO流实现保存数据的方法和读取数据的方法。
在这里插入图片描述
代码如下:

package com.example.a15114.itcast_logindemo;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class UserInfoUtils {
    //保存用户名和密码的业务方法
    public static boolean saveInfo(String username,String pwd){

        try {
            //[1]创建file类指定我们要把数据存储位置
            String result=username+"##"+pwd;
            //[2]创建一个文件输出
            File file=new File("/data/data/com.example.a15114.itcast_logindemo/info.txt");
            FileOutputStream fos=new FileOutputStream(file);
            fos.write(result.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    //读取用户的信息
    public static Map<String,String> readInfo() {
        //[1]定义map

        try {
            Map<String,String> maps=new HashMap<String, String>();
            File file=new File("/data/data/com.example.a15114.itcast_logindemo/info.txt");
            FileInputStream fis= null;
            fis = new FileInputStream(file);
            BufferedReader bufr=new BufferedReader(new InputStreamReader(fis));
            String content=bufr.readLine();//读取数据

            //切割字符串 封装到map集合中
            String[]splis=content.split("##");
            String name=splis[0];
            String pwd=splis[1];
            //把name和pwd放入map中
            maps.put("name",name);
            maps.put("pwd",pwd);
            fis.close();
            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}

3、编写安卓程序主java类,
代码如下:

package com.example.a15114.itcast_logindemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    EditText et_username;
    EditText et_userpassword;
    CheckBox cb_ischeck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //[1]找到我们关心的控件
        et_username=findViewById(R.id.tv_username);
        et_userpassword=findViewById(R.id.tv_pwd);
        cb_ischeck=findViewById(R.id.cb_remember);
        //[1.1]读取data/data 下 info.txt信息
        Map<String,String> maps=UserInfoUtils.readInfo();
        if (maps!=null){
            //把name和pwd取出来
            String name=maps.get("name");
            String pwd=maps.get("pwd");
            //[1.2]把name和pwd显示到edittext上

            et_username.setText(name);
            et_userpassword.setText(pwd);

        }
    }

    //[2]写按钮的点击事件
    public void login(View v){
        //[2.1]获取用户名和密码
        String name=et_username.getText().toString().trim();
        String pwd=et_userpassword.getText().toString().trim();
        //[2.2]判断name和pwd是否为空
        if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
            Toast.makeText(MainActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();

        }else {
            //[2.3]进行登录的逻辑
            System.out.println("连接服务器进行登录 网络编程之后实现");

            if (cb_ischeck.isChecked()){
               //[2.4]把已经输入的用户名和密码给存起来
                boolean result=UserInfoUtils.saveInfo(name,pwd);
                if (result){
                    Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(MainActivity.this,"保存失败",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(MainActivity.this,"请勾选cb",Toast.LENGTH_SHORT).show();

            }
        }
    }
}

本内容重点掌握java的IO流,难点用户名和密码对应(使用map集合),若有疑问可留言。

猜你喜欢

转载自blog.csdn.net/liyunfu233/article/details/84072958
今日推荐