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

来源:互联网 发布:mac pro 无法安装win7 编辑:程序博客网 时间:2024/06/07 02:43

LoginActivity.java

[java] view plaincopyprint?
  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. }


FileService.java

[java] view plaincopyprint?
  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. }


StreamTools.java

[java] view plaincopyprint?
  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. }


login_activity.xml

[html] view plaincopyprint?
  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>


String.xml

[html] view plaincopyprint?
  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>
原创粉丝点击