您现在的位置是:群英 > 开发技术 > 移动开发
Android实现保存QQ账号和密码的方法是什么
Admin发表于 2022-05-17 11:49:37820 次浏览
相信很多人对“Android实现保存QQ账号和密码的方法是什么”都不太了解,下面群英小编为你详细解释一下这个问题,希望对你有一定的帮助

写在前面:今天用保存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账号和密码的方法是什么”已经有了进一步的了解,更多相关的问题,欢迎关注群英网络或到群英官网咨询客服。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-11-11 17:47:10 
摘要:return在c语言中的意思为“返回”。return表示把程序流程从被调函数转向主调函数并把表达式的值带回主调函数,实现函数值的返回,返回时可附带一个返回值,由return后面的参数指定。
2022-08-27 17:49:41 
摘要:本篇文章带大家继续angular的学习,对比一下tslint和eslint,介绍一下Angular中怎么将迁移tslint至eslint,希望对大家有所帮助!
2021-11-24 17:53:56 
摘要:这篇文章给大家分享的是关于python面向对象的内容,python面向对象的特性包括继承、封装(隐藏)、多态,接下来我们就具体的了解一下python面向对象的这三大特性,对大家学习或者工作都有一定的参考,感兴趣的朋友就继续往下看吧。
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部