Android开发(6)--完成登陆界面的数据保存回显的操作

本文详细介绍了如何在Android应用中实现登录功能,并通过文件存储优化用户信息的保存与加载。包括用户输入验证、密码存储安全性和登录状态的持久化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LoginActivity.java

  1. package com.example.login;

  2. import java.util.Map;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.CheckBox;
  10. import android.widget.EditText;
  11. import android.widget.Toast;

  12. import com.example.login.service.FileService;

  13. public class LoginActivityextends Activity {

  14. public EditText edit_name,edit_pass;
  15. public Button btn_login;
  16. public CheckBox box_remeber;

  17. public FileService fileService;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_login);
  22. fileService=new FileService(this);

  23. edit_name=(EditText) findViewById(R.id.edit_name);
  24. edit_pass=(EditText) findViewById(R.id.edit_pass);
  25. btn_login=(Button) findViewById(R.id.btn_login);
  26. box_remeber=(CheckBox) findViewById(R.id.cbx_remember);

  27. btn_login.setOnClickListener(new MyOnClickListener());

  28. Map<String, String> map=fileService.readFile("private.txt");
  29. if(map!=null){
  30. edit_name.setText(map.get("name"));
  31. edit_pass.setText(map.get("pass"));
  32. }
  33. }

  34. @Override
  35. public boolean onCreateOptionsMenu(Menu menu) {
  36. // Inflate the menu; this adds items to the action bar if it is present.
  37. getMenuInflater().inflate(R.menu.login, menu);
  38. return true;
  39. }

  40. class MyOnClickListenerimplements View.OnClickListener{

  41. @Override
  42. public void onClick(View v) {
  43. int id=v.getId();
  44. switch (id) {
  45. case R.id.btn_login:
  46. String name=edit_name.getText().toString();
  47. String pass=edit_pass.getText().toString();
  48. if(TextUtils.isEmpty(name)){
  49. Toast.makeText(LoginActivity.this,"用户名不能为空", Toast.LENGTH_SHORT).show();
  50. return;
  51. }else if(TextUtils.isEmpty(pass)){
  52. Toast.makeText(LoginActivity.this,"密码不能为空", Toast.LENGTH_SHORT).show();
  53. return;
  54. }else{
  55. if(box_remeber.isChecked()){
  56. LoginActivity.this.fileService.saveToRom(name, pass,"private.txt");
  57. Toast.makeText(LoginActivity.this,"用户名和密码已保存", Toast.LENGTH_SHORT).show();
  58. }else{
  59. Toast.makeText(LoginActivity.this,"用户名和密码不需要保存", Toast.LENGTH_SHORT).show();
  60. }
  61. }
  62. break;

  63. default:
  64. break;
  65. }


  66. /*if(id==btn_login.getId()){
  67. String name=edit_name.getText().toString();
  68. String pass=edit_pass.getText().toString();
  69. if(TextUtils.isEmpty(name)){
  70. Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
  71. return;
  72. }else if(TextUtils.isEmpty(pass)){
  73. Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
  74. return;
  75. }else{
  76. if(box_remeber.isChecked()){
  77. LoginActivity.this.fileService.saveToRom(name, pass, "private.txt");
  78. Toast.makeText(LoginActivity.this, "用户名和密码已保存", Toast.LENGTH_SHORT).show();
  79. }else{
  80. Toast.makeText(LoginActivity.this, "用户名和密码不需要保存", Toast.LENGTH_SHORT).show();
  81. }
  82. }
  83. }*/
  84. }

  85. }
  86. }
  1. package com.example.login;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.text.TextUtils;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.CheckBox;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. import com.example.login.service.FileService;  
  16.   
  17. public class LoginActivity extends Activity {  
  18.   
  19.     public EditText edit_name,edit_pass;  
  20.     public Button btn_login;  
  21.     public CheckBox box_remeber;  
  22.       
  23.     public FileService fileService;  
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_login);  
  28.         fileService=new FileService(this);  
  29.           
  30.         edit_name=(EditText) findViewById(R.id.edit_name);  
  31.         edit_pass=(EditText) findViewById(R.id.edit_pass);  
  32.         btn_login=(Button) findViewById(R.id.btn_login);  
  33.         box_remeber=(CheckBox) findViewById(R.id.cbx_remember);  
  34.           
  35.         btn_login.setOnClickListener(new MyOnClickListener());  
  36.           
  37.         Map<String, String> map=fileService.readFile("private.txt");  
  38.         if(map!=null){  
  39.             edit_name.setText(map.get("name"));  
  40.             edit_pass.setText(map.get("pass"));  
  41.         }  
  42.     }  
  43.   
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.login, menu);  
  48.         return true;  
  49.     }  
  50.   
  51.     class MyOnClickListener implements View.OnClickListener{  
  52.   
  53.         @Override  
  54.         public void onClick(View v) {  
  55.             int id=v.getId();  
  56.             switch (id) {  
  57.             case R.id.btn_login:  
  58.                 String name=edit_name.getText().toString();  
  59.                 String pass=edit_pass.getText().toString();  
  60.                 if(TextUtils.isEmpty(name)){  
  61.                     Toast.makeText(LoginActivity.this"用户名不能为空", Toast.LENGTH_SHORT).show();  
  62.                     return;  
  63.                 }else if(TextUtils.isEmpty(pass)){  
  64.                     Toast.makeText(LoginActivity.this"密码不能为空", Toast.LENGTH_SHORT).show();  
  65.                     return;  
  66.                 }else{  
  67.                     if(box_remeber.isChecked()){  
  68.                         LoginActivity.this.fileService.saveToRom(name, pass, "private.txt");  
  69.                         Toast.makeText(LoginActivity.this"用户名和密码已保存", Toast.LENGTH_SHORT).show();  
  70.                     }else{  
  71.                         Toast.makeText(LoginActivity.this"用户名和密码不需要保存", Toast.LENGTH_SHORT).show();  
  72.                     }  
  73.                 }  
  74.                 break;  
  75.   
  76.             default:  
  77.                 break;  
  78.             }  
  79.               
  80.               
  81.             /*if(id==btn_login.getId()){ 
  82.                 String name=edit_name.getText().toString(); 
  83.                 String pass=edit_pass.getText().toString(); 
  84.                 if(TextUtils.isEmpty(name)){ 
  85.                     Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show(); 
  86.                     return; 
  87.                 }else if(TextUtils.isEmpty(pass)){ 
  88.                     Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show(); 
  89.                     return; 
  90.                 }else{ 
  91.                     if(box_remeber.isChecked()){ 
  92.                         LoginActivity.this.fileService.saveToRom(name, pass, "private.txt"); 
  93.                         Toast.makeText(LoginActivity.this, "用户名和密码已保存", Toast.LENGTH_SHORT).show(); 
  94.                     }else{ 
  95.                         Toast.makeText(LoginActivity.this, "用户名和密码不需要保存", Toast.LENGTH_SHORT).show(); 
  96.                     } 
  97.                 } 
  98.             }*/  
  99.         }  
  100.           
  101.     }  
  102. }  


FileService.java

  1. package com.example.login.service;

  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.util.HashMap;
  5. import java.util.Map;

  6. import com.example.login.utils.StreamTools;

  7. import android.content.Context;

  8. public class FileService {

  9. public Context context;

  10. public FileService(Context context) {
  11. this.context = context;
  12. }

  13. public boolean saveToRom(String name,String pass,String fileName){
  14. try{
  15. FileOutputStream fos=context.openFileOutput(fileName, Context.MODE_PRIVATE);
  16. String result=name+":"+pass;
  17. fos.write(result.getBytes());
  18. fos.flush();
  19. fos.close();
  20. }catch(Exception e){
  21. e.printStackTrace();
  22. return false;
  23. }
  24. return true;
  25. }

  26. public Map<String,String> readFile(String fileName){
  27. Map<String,String> map=null;
  28. try{
  29. FileInputStream fis=context.openFileInput(fileName);
  30. String value=StreamTools.getValue(fis);
  31. String values[]=value.split(":");
  32. if(values.length>0){
  33. map=new HashMap<String, String>();
  34. map.put("name", values[0]);
  35. map.put("pass", values[1]);
  36. }

  37. }catch(Exception e){
  38. e.printStackTrace();
  39. }
  40. return map;
  41. }

  42. }
  1. package com.example.login.service;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import com.example.login.utils.StreamTools;  
  9.   
  10. import android.content.Context;  
  11.   
  12. public class FileService {  
  13.   
  14.     public Context context;  
  15.   
  16.     public FileService(Context context) {  
  17.         this.context = context;  
  18.     }  
  19.       
  20.     public boolean saveToRom(String name,String pass,String fileName){  
  21.         try{  
  22.             FileOutputStream fos=context.openFileOutput(fileName, Context.MODE_PRIVATE);  
  23.             String result=name+":"+pass;  
  24.             fos.write(result.getBytes());  
  25.             fos.flush();  
  26.             fos.close();  
  27.         }catch(Exception e){  
  28.             e.printStackTrace();  
  29.             return false;  
  30.         }  
  31.         return true;  
  32.     }  
  33.       
  34.     public Map<String,String> readFile(String fileName){  
  35.         Map<String,String> map=null;  
  36.         try{  
  37.             FileInputStream fis=context.openFileInput(fileName);  
  38.             String value=StreamTools.getValue(fis);  
  39.             String values[]=value.split(":");  
  40.             if(values.length>0){  
  41.                 map=new HashMap<String, String>();  
  42.                 map.put("name", values[0]);  
  43.                 map.put("pass", values[1]);  
  44.             }  
  45.               
  46.         }catch(Exception e){  
  47.             e.printStackTrace();  
  48.         }  
  49.         return map;  
  50.     }  
  51.       
  52. }  


StreamTools.java

  1. package com.example.login.utils;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileInputStream;

  4. public class StreamTools {
  5. public static String getValue(FileInputStream fis)throws Exception{
  6. ByteArrayOutputStream stream=new ByteArrayOutputStream();
  7. byte[] buffer=newbyte[1024];
  8. int length=-1;
  9. while((length=fis.read(buffer))!=-1){
  10. stream.write(buffer,0,length);
  11. }
  12. stream.flush();
  13. stream.close();
  14. String value=stream.toString();
  15. return value;
  16. }
  17. }
  1. package com.example.login.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.FileInputStream;  
  5.   
  6. public class StreamTools {  
  7.     public static String getValue(FileInputStream fis) throws Exception{  
  8.         ByteArrayOutputStream stream=new ByteArrayOutputStream();  
  9.         byte[] buffer=new byte[1024];  
  10.         int length=-1;  
  11.         while((length=fis.read(buffer))!=-1){  
  12.             stream.write(buffer,0,length);  
  13.         }  
  14.         stream.flush();  
  15.         stream.close();  
  16.         String value=stream.toString();  
  17.         return value;  
  18.     }  
  19. }  


login_activity.xml

  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".LoginActivity">

  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentLeft="true"
  14. android:layout_alignParentTop="true"
  15. android:orientation="vertical">

  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content">

  19. <TextView
  20. android:id="@+id/view_name"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="@string/text_name"/>

  24. <EditText
  25. android:id="@+id/edit_name"
  26. android:layout_width="0dp"
  27. android:layout_height="wrap_content"
  28. android:layout_weight="1"
  29. android:ems="10"
  30. android:inputType="textPersonName">

  31. <requestFocus/>
  32. </EditText>
  33. </LinearLayout>

  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content">

  37. <TextView
  38. android:id="@+id/view_pass"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:text="@string/text_pass"/>

  42. <EditText
  43. android:id="@+id/edit_pass"
  44. android:layout_width="0dp"
  45. android:layout_height="wrap_content"
  46. android:layout_weight="1"
  47. android:ems="10"
  48. android:inputType="textPassword"/>
  49. </LinearLayout>

  50. <LinearLayout
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content">

  53. <Button
  54. android:id="@+id/btn_login"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_weight="0.17"
  58. android:text="@string/text_login"/>

  59. <CheckBox
  60. android:id="@+id/cbx_remember"
  61. android:layout_width="wrap_content"
  62. android:layout_height="wrap_content"
  63. android:layout_marginLeft="80dp"
  64. android:text="@string/text_rember"/>

  65. </LinearLayout>

  66. </LinearLayout>

  67. </RelativeLayout>
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".LoginActivity" >  
  10.   
  11.     <LinearLayout  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentTop="true"  
  16.         android:orientation="vertical" >  
  17.   
  18.         <LinearLayout  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="wrap_content" >  
  21.   
  22.             <TextView  
  23.                 android:id="@+id/view_name"  
  24.                 android:layout_width="wrap_content"  
  25.                 android:layout_height="wrap_content"  
  26.                 android:text="@string/text_name" />  
  27.   
  28.             <EditText  
  29.                 android:id="@+id/edit_name"  
  30.                 android:layout_width="0dp"  
  31.                 android:layout_height="wrap_content"  
  32.                 android:layout_weight="1"  
  33.                 android:ems="10"   
  34.                 android:inputType="textPersonName">  
  35.   
  36.                 <requestFocus />  
  37.             </EditText>  
  38.         </LinearLayout>  
  39.   
  40.         <LinearLayout  
  41.             android:layout_width="match_parent"  
  42.             android:layout_height="wrap_content" >  
  43.   
  44.             <TextView  
  45.                 android:id="@+id/view_pass"  
  46.                 android:layout_width="wrap_content"  
  47.                 android:layout_height="wrap_content"  
  48.                 android:text="@string/text_pass" />  
  49.   
  50.             <EditText  
  51.                 android:id="@+id/edit_pass"  
  52.                 android:layout_width="0dp"  
  53.                 android:layout_height="wrap_content"  
  54.                 android:layout_weight="1"  
  55.                 android:ems="10"  
  56.                 android:inputType="textPassword" />  
  57.         </LinearLayout>  
  58.   
  59.         <LinearLayout  
  60.             android:layout_width="match_parent"  
  61.             android:layout_height="wrap_content" >  
  62.   
  63.             <Button  
  64.                 android:id="@+id/btn_login"  
  65.                 android:layout_width="wrap_content"  
  66.                 android:layout_height="wrap_content"  
  67.                 android:layout_weight="0.17"  
  68.                 android:text="@string/text_login" />  
  69.   
  70.             <CheckBox  
  71.                 android:id="@+id/cbx_remember"  
  72.                 android:layout_width="wrap_content"  
  73.                 android:layout_height="wrap_content"  
  74.                 android:layout_marginLeft="80dp"  
  75.                 android:text="@string/text_rember" />  
  76.   
  77.         </LinearLayout>  
  78.   
  79.     </LinearLayout>  
  80.   
  81. </RelativeLayout>  


String.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>

  3. <stringname="app_name">login</string>
  4. <stringname="action_settings">Settings</string>
  5. <stringname="hello_world">Login</string>
  6. <stringname="text_name">用户名:</string>
  7. <stringname="text_pass">密 码:</string>
  8. <stringname="text_login">登陆</string>
  9. <stringname="text_rember">记住密码</string>
  10. </resources>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值