记住账号密码(本地存储)

待我称王封你为后i 2023-08-17 17:09 175阅读 0赞

1、activity_main.xml文件样式

2、MainActivity.java主页内容

3、创建一个保存文件的类

1.activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <ImageView
  8. android:id="@+id/iv"
  9. android:layout_width="70dp"
  10. android:layout_height="70dp"
  11. android:layout_centerHorizontal="true"
  12. android:layout_marginTop="40dp"
  13. android:background="@drawable/dongman"/>
  14. <LinearLayout
  15. android:id="@+id/ll_number"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_below="@+id/iv"
  19. android:layout_centerVertical="true"
  20. android:layout_marginTop="15dp"
  21. android:layout_marginLeft="10dp"
  22. android:layout_marginRight="10dp"
  23. android:layout_marginBottom="5dp"
  24. android:background="#ffffff">
  25. <TextView
  26. android:id="@+id/tv_number"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:padding="10dp"
  30. android:text="账号"
  31. android:textColor="#000"
  32. android:textSize="20sp"/>
  33. <EditText
  34. android:id="@+id/et_number"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:layout_marginLeft="5dp"
  38. android:background="@null"
  39. android:padding="10dp"/>
  40. </LinearLayout>
  41. <LinearLayout
  42. android:id="@+id/ll_password"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@+id/ll_number"
  46. android:layout_centerVertical="true"
  47. android:layout_marginTop="15dp"
  48. android:layout_marginLeft="10dp"
  49. android:layout_marginRight="10dp"
  50. android:layout_marginBottom="5dp"
  51. android:background="#ffffff">
  52. <TextView
  53. android:id="@+id/tv_password"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:padding="10dp"
  57. android:text="密码"
  58. android:textColor="#000"
  59. android:textSize="20sp"/>
  60. <EditText
  61. android:id="@+id/et_password"
  62. android:layout_width="match_parent"
  63. android:layout_height="wrap_content"
  64. android:layout_marginLeft="5dp"
  65. android:background="@null"
  66. android:padding="10dp"/>
  67. </LinearLayout>
  68. <Button
  69. android:id="@+id/btn_login"
  70. android:layout_width="match_parent"
  71. android:layout_height="wrap_content"
  72. android:layout_below="@+id/ll_password"
  73. android:layout_marginLeft="10dp"
  74. android:layout_marginRight="10dp"
  75. android:layout_marginTop="30dp"
  76. android:text="登录"
  77. android:background="#3C8DC4"
  78. android:textSize="20sp"/>
  79. </RelativeLayout>

2.MainActivity.java主页内容

  1. package com.example.remembernp;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. import java.util.Map;
  10. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  11. private EditText etNumber;
  12. private EditText etPassword;
  13. private Button btnLogin;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. Button btn = (Button)findViewById(R.id.btn_login);
  19. Map<String,String> userInfo = SaveFile.getUserInfo(this);
  20. initView();
  21. if(userInfo != null){
  22. etNumber.setText(userInfo.get("number"));
  23. etPassword.setText(userInfo.get("password"));
  24. }
  25. }
  26. private void initView(){
  27. etNumber = (EditText)findViewById(R.id.et_number);
  28. etPassword = (EditText)findViewById(R.id.et_password);
  29. btnLogin = (Button)findViewById(R.id.btn_login);
  30. btnLogin.setOnClickListener(this);
  31. }
  32. @Override
  33. public void onClick(View view) {
  34. //单击事件,获取账号密码
  35. String number = etNumber.getText().toString().trim();
  36. String password = etPassword.getText().toString().trim();
  37. //检查账号密码是否正确
  38. if(TextUtils.isEmpty(number)){
  39. Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
  40. return;
  41. }
  42. if(TextUtils.isEmpty(password)){
  43. Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
  44. return;
  45. }
  46. //否则登录成功
  47. Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
  48. //保存信息
  49. boolean isSaveSuccess = SaveFile.saveUserInfo(this,number,password);
  50. if(isSaveSuccess){
  51. Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
  52. }else{
  53. Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
  54. }
  55. }
  56. }

3.创建一个保存文件的类

  1. package com.example.remembernp;
  2. import android.content.Context;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. public class SaveFile {
  8. //把账号密码保存在data.txt文件中
  9. public static boolean saveUserInfo(Context context, String number, String password){
  10. try{
  11. FileOutputStream fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
  12. fos.write((number + ":" + password).getBytes());
  13. fos.close();
  14. return true;
  15. }catch (Exception e){
  16. e.printStackTrace();
  17. return false;
  18. }
  19. }
  20. //从data.txt中去获取刚刚保存的账号密码
  21. public static Map<String,String> getUserInfo(Context context) {
  22. String content = "";
  23. try {
  24. FileInputStream fis = context.openFileInput("data.txt");
  25. byte[] buffer = new byte[fis.available()];
  26. fis.read(buffer);//读取
  27. content = new String(buffer);
  28. Map<String ,String > userMap = new HashMap<String, String>();
  29. String[] infos = content.split(":");
  30. userMap.put("number",infos[0]);
  31. userMap.put("password",infos[1]);
  32. fis.close();
  33. return userMap;
  34. }catch (Exception e){
  35. e.printStackTrace();
  36. return null;
  37. }
  38. }
  39. }

转载于:https://www.cnblogs.com/Mr-Deng/p/11313106.html

发表评论

表情:
评论列表 (有 0 条评论,175人围观)

还没有评论,来说两句吧...

相关阅读