Android简单登录界面,保存账号和密码(基础,详解)

一 问题描述:

        制作一个简单的登录界面,并使用文件储存方式储存用户名和密码,在下次打开应用时自动获取上次储存的账户和密码

二 解题思路:

        文件储存:

        文件存储是Android中最基本的一种数据存储方式,其与java中的文件存储类似,都是通过I/O流的形式把数据直接存储到文件中。

        内部存储使用的是Context提法的openFileOutput()方法和openFileInput()方法,这两个方法能够返回进行读写操作的FileOutputStream和FileInputStream对象。


        openFileOutput():用于打开应用程序中对应的输出流,将数据存储到指定的文件中
        openFileInput():用于打开应用程序对应的输入流,读取指定文件的数据

操作模式:

        MODE_PRIVATE:该文件只能被当前程序读写
        MODE_APPEND:该文件的内容可以追加
        MODE_WORLD_READABLE:该文件的内容可以被其他程序读
        MODE_WORLD_WRITEABLE:该文件的内容可以被其他程序写


Android默认任何应用创建的文件都是私有的,其他程序无法访问。但是一旦拥有权限其他用户就可以查看用户的私有目录,所以这种村存储账号密码的方式也不是很安全。在真正的应用上是一定要经过加密的。

        布局线框图:

三 登录界面代码:

布局代码:

        使用一个ImageView控件来显示用户的头像,TextView和EditView来组合成输入界面,Button按钮实现登录按钮。

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="#E6E6E6"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">

    <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>

创建工具类:

        在程序包中创建一个工具类FileLoginInterface(我这里命名不太合理,命名的时候建议使用FileSave类似的比较直观)

FileLoginInterface.java

package com.example.logininterface;

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 FileLoginInterface {
    //保存密码和账号到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);
            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();
            }
        }
    }

    //从文件中获取储存的账号和密码
    public static Map<String, String> getUserInfor(Context context){
        String content = "";
        FileInputStream fis = null;
        try{
            //获取文件的输入流对象
            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集合中
            userMap.put("account",infos[0]);
            //将数组中的第二个数据放入userMap集合中
            userMap.put("password",infos[1]);
            return userMap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

四 界面交互代码:

MainActivity.java

package com.example.logininterface;

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{
    private EditText et_account; //账号输入框
    private EditText et_password;//密码输入框
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //通过工具类FileLoginInterface中的getUserInfor()方法获取账号和密码信息
        Map<String, String> userInfor = FileLoginInterface.getUserInfor(this);
        if(userInfor != null){
            //将获取的账号显示到界面上
            et_account.setText(userInfor.get("account"));
            //将获取的密码显示到界面上
            et_password.setText(userInfor.get("password"));
        }
    }

    private void initView(){
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById((R.id.btn_login));
        //设置按钮的监听事件
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_login:
                //当点击登录按钮时,获取界面上的账号和密码
                String account = et_account.getText().toString().trim();
                String password = et_password.getText().toString();
                //检验输入的账号和密码是否为空
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                //保存用户信息
                boolean isSaveSuccess = FileLoginInterface.saveUserInfo(this,account,password);
                if(isSaveSuccess){
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                }
            break;
        }
    }
}

运行效果: 

第一次运行:

输入账号和密码,点击登录

停止运行并再次运行:

  • 21
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Android底层开发技术实战详解:内核、移植和驱动》是一本涵盖Android底层开发技术的实战性书籍。Android系统的内核是整个系统的核心,它负责管理系统的资源、执行任务和提供与硬件交互的接口。在这本书中,作者详细介绍了如何进行Android内核的开发和调试。 内容涵盖了内核的编译和配置、移植以及设备驱动的开发。首先,该书讲解了如何编译和配置Android内核,包括各种编译选项以及内核配置的常见问题和解决方案。其次,书中介绍了Android系统的移植方法和流程,包括移植到新的硬件平台以及在不同处理器架构上进行移植的技术要点和指导。最后,该书还探讨了设备驱动的开发,包括设备驱动的概念、编写和调试方法。 通过学习《Android底层开发技术实战详解:内核、移植和驱动》,读者可以掌握Android底层开发的重要知识和技能。这对于那些希望深入了解Android系统,并且想要进行系统优化、调试和定制的开发者来说,是一本非常实用的参考书。无论是从事Android系统开发的工程师,还是对Android底层开发感兴趣的爱好者,都能够从中获益良多。 总而言之,《Android底层开发技术实战详解:内核、移植和驱动》是一本内容丰富、实用性强的Android底层开发指南。它覆盖了内核、移植和驱动的方方面面,并且提供了大量实例和案例,帮助读者更好地理解和应用所学知识。无论是对于初学者来说,还是对于有一定经验的开发者来说,都是一本值得阅读的好书。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值