irpas技术客

Android简单登录界面,保存账号和密码(基础,详解)_努力做好AC吧_android 登录界面

大大的周 7906

一 问题描述:

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

二 解题思路: ? ? ? ? 文件储存:

????????文件存储是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; } } } 运行效果:?

第一次运行:

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

停止运行并再次运行:


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Android #登录界面 # #问题描述