大家好,我是汤姆凯特。

写在前面:今天用保存qq账号和密码的实战演练,带大家掌握android存储中最基本的文件存储方式

文件存储是android中最基本的一种数据存储方式,它与java中的文件存储类似,都是通过i/o流形式把数据直接存储到文件中,下面我们一起来看一下如何用android实现文件存储功能吧!

1.ui界面

1)垂直线性布局为整体框架

2)头像获取

3)子线性布局编辑框和密码框

4)登录button按钮

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6e6e6"
    android:orientation="vertical"
    android:padding="10dp">
    <imageview
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerhorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_margintop="30dp"
        android:src="@drawable/head" />
    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margintop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号:"
            android:textcolor="#000"
            android:textsize="20sp" />
        <edittext
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginleft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </linearlayout>
    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margintop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <textview
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码:"
            android:textcolor="#000"
            android:textsize="20sp" />
        <edittext
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginleft="5dp"
            android:background="@null"
            android:inputtype="textpassword"
            android:padding="10dp" />
    </linearlayout>
    <button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margintop="25dp"
        android:background="#3c8dc4"
        android:text="登录"
        android:textcolor="@android:color/white"
        android:textsize="20sp" />
</linearlayout>

2.构建工具类

1)将数据存入文件

android开发中,内部存储使用的是context提供的openfileoutput()方法这个方法能够返回进行写操作的fileoutputstream对象,示例如下:

fileoutputstream fos = openfileoutput(string name, int mode);

其中参数name表示文件名,mode表示文件的操作模式,也就是读写文件的方式。mode的取值有4种,具体如下:

  • mode_private:该文件只能被当前程序读写
  • mode_append:该文件的内容可以追加
  • mode_world_readable:该文件的内容可以被其他程序读
  • mode_world_writeable:该文件的内容可以被其他程序写

存储数据时,使用fileoutputstream对象将数据存储到文件中,创建了一个saveuserinfo()方法,用于将qq账号和密码保存到data.txt文件中。

    //保存qq账号和登录密码到data.txt文件中
    public static boolean saveuserinfo(context context, string account, string
            password) {
        fileoutputstream fos = null;
        try {
            //获取文件的输出流对象fos
            fos = context.openfileoutput("data.txt",
                    context.mode_private);
            //将数据转换为字节码的形式写入data.txt文件中
            fos.write((account + ":" + password).getbytes());
            return true;
        } catch (exception e) {
            e.printstacktrace();
            return false;
        }finally {
            try {
                if(fos != null){
                    fos.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }

2)从文件中读取数据

使用context提供的openfileoutput()方法这个方法能够返回进行写操作的fileinputstream对象,示例如下:

fileinputstream fos = openfileinput(string name);

创建了一个getuserinfo()方法,用于从data.txt文件中获取qq账号和密码。

需要注意的是,这里的存储和获取都是需要用字节码的形式,所以存取完再改为string类型。

 //从data.txt文件中获取存储的qq账号和密码
    public static map<string, string> getuserinfo(context context) {
        string content = "";
        fileinputstream fis = null;
        try {
            //获取文件的输入流对象fis
            fis = context.openfileinput("data.txt");
            //将输入流对象中的数据转换为字节码的形式
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);//通过read()方法读取字节码中的数据
            content = new string(buffer); //将获取的字节码转换为字符串
            map<string, string> usermap = new hashmap<string, string>();
            string[] infos = content.split(":");//将字符串以“:”分隔后形成一个数组的形式
            usermap.put("account", infos[0]);   //将数组中的第一个数据放入usermap集合中
            usermap.put("password", infos[1]); //将数组中的第二个数据放入usermap集合中
            return usermap;
        } catch (exception e) {
            e.printstacktrace();
            return null;
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }

3.编写界面交互代码

1)读取文件

通过工具类filesaveqq中的getuserinfo()方法获取qq账号和密码信息

        map<string, string> userinfo = filesaveqq.getuserinfo(this);

        if (userinfo != null) {
            et_account.settext(userinfo.get("account"));   //将获取的账号显示到界面上
            et_password.settext(userinfo.get("password")); //将获取的密码显示到界面上
        }

2)按钮监听事件

创建一个initview()方法,用于初始化界面控件。再对onclick()方法重写,添加点击登录事件后的响应。

    private edittext et_account;   //账号输入框
    private edittext et_password;  //密码输入框
    private button btn_login;       //登录按钮
	private void initview() {
        et_account =  findviewbyid(r.id.et_account);
        et_password =  findviewbyid(r.id.et_password);
        btn_login = findviewbyid(r.id.btn_login);
        //设置按钮的点击监听事件
        btn_login.setonclicklistener(this);
    }
    @override
    public void onclick(view v) {
        switch (v.getid()) {
            case r.id.btn_login:
                //当点击登录按钮时,获取界面上输入的qq账号和密码
                string account = et_account.gettext().tostring().trim();
                string password = et_password.gettext().tostring();
                //检验输入的账号和密码是否为空
                if (textutils.isempty(account)) {
                    toast.maketext(this, "请输入qq账号", toast.length_short).show();
                    return;
                }
                if (textutils.isempty(password)) {
                    toast.maketext(this, "请输入密码", toast.length_short).show();
                    return;
                }
                toast.maketext(this, "登录成功", toast.length_short).show();
                break;
        }
    }

3)保存登录信息

调用工具类filesaveqq中的saveuserinfo()方法将登录信息保存到本地文件中。

 boolean issavesuccess = filesaveqq.saveuserinfo(this, account,password);

                if (issavesuccess) {
                    toast.maketext(this, "保存成功", toast.length_short).show();
                } else {
                    toast.maketext(this, "保存失败", toast.length_short).show();
                }

4.运行程序

在界面中输入账号和密码,点击“登录”按钮,会弹出“登录成功”与”保存成功“的提示信息

5.查看文件所处位置

1)view——tool windows ——device

2)右侧的device file explorer ——data ——data ——项目包名——files

到此这篇关于android保存qq账号与密码的文章就介绍到这了,更多相关android qq账号与密码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!