JAVA登陆界面的数据保存回显的操作.doc_第1页
JAVA登陆界面的数据保存回显的操作.doc_第2页
JAVA登陆界面的数据保存回显的操作.doc_第3页
JAVA登陆界面的数据保存回显的操作.doc_第4页
JAVA登陆界面的数据保存回显的操作.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、计算机交流平台:计算机故障_计算机论坛www.it168 wyJAVA登陆界面的数据保存回显的操作 java IMG alt= src= java package com.example.day02_file; import java.util.Map; import com.example.lession02_file.service.FileService; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; impor

2、t android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity public static FileService fileService = null; / 声明获取得用户与密码的组件 public EditText edit_name, edit_pass; / 声明登陆按钮对象 p

3、ublic Button btn_login; / 声明CheckBox组件对象 public CheckBox box_remember; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); / 设置显示视图 setContentView(R.layout.activity_login); / 实例化业务对象 fileService = new FileService(this); edit_name = (EditText) findViewById(

4、R.id.edit_name); edit_pass = (EditText) findViewById(R.id.edit_pass); btn_login = (Button) findViewById(R.id.btn_login); box_remember = (CheckBox) findViewById(R.id.file_Chickbox); btn_login.setOnClickListener(new MyOnClickListener(); / 回显数据 Map map = fileService.readFile(private.txt); if (map != nu

5、ll) edit_name.setText(map.get(edit_name); edit_pass.setText(map.get(edit_pass); Override public boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.login, menu); return true; / 内部类 class MyOnClickListener imp

6、lements View.OnClickListener Override public void onClick(View v) int id = v.getId(); if (id = btn_login.getId() String name = edit_name.getText().toString(); String pass = edit_pass.getText().toString(); if (TextUtils.isEmpty(name) | TextUtils.isEmpty(pass) Toast.makeText(LoginActivity.this, 用户名或密码

7、不能为空, Toast.LENGTH_LONG).show(); return; else / 如果记住密码勾选上了 if (box_remember.isChecked() / 进行保存 / 调用业务对象的业务方法 LoginActivity.this.fileService.saveToRom(name, pass, private.txt); Toast.makeText(LoginActivity.this, 用户名和密码需要保存, Toast.LENGTH_LONG).show(); else / 不保存 Toast.makeText(LoginActivity.this, 用户名和

8、密码不需要保存, Toast.LENGTH_LONG).show(); package com.example.day02_file;import java.util.Map;import com.example.lession02_file.service.FileService;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.widget.But

9、ton;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends Activity public static FileService fileService = null; / 声明获取得用户与密码的组件 public EditText edit_name, edit_pass; / 声明登陆按钮对象 public Button btn_login; / 声明CheckBox组件对象 public Ch

10、eckBox box_remember; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); / 设置显示视图 setContentView(R.layout.activity_login); / 实例化业务对象 fileService = new FileService(this); edit_name = (EditText) findViewById(R.id.edit_name); edit_pass = (EditText) findViewBy

11、Id(R.id.edit_pass); btn_login = (Button) findViewById(R.id.btn_login); box_remember = (CheckBox) findViewById(R.id.file_Chickbox); btn_login.setOnClickListener(new MyOnClickListener(); / 回显数据 Map map = fileService.readFile(private.txt); if (map != null) edit_name.setText(map.get(edit_name); edit_pas

12、s.setText(map.get(edit_pass); Override public boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.login, menu); return true; / 内部类 class MyOnClickListener implements View.OnClickListener Override public void

13、onClick(View v) int id = v.getId(); if (id = btn_login.getId() String name = edit_name.getText().toString(); String pass = edit_pass.getText().toString(); if (TextUtils.isEmpty(name) | TextUtils.isEmpty(pass) Toast.makeText(LoginActivity.this, 用户名或密码不能为空, Toast.LENGTH_LONG).show(); return; else / 如果

14、记住密码勾选上了 if (box_remember.isChecked() / 进行保存 / 调用业务对象的业务方法 LoginActivity.this.fileService.saveToRom(name, pass, private.txt); Toast.makeText(LoginActivity.this, 用户名和密码需要保存, Toast.LENGTH_LONG).show(); else / 不保存 Toast.makeText(LoginActivity.this, 用户名和密码不需要保存, Toast.LENGTH_LONG).show(); java java java

15、package com.example.lession02_file.service; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import com.example.lession02_file.util.StreamTools; import android.content.Context; public class FileServ

16、ice / 上下文的对象 public Context context; public FileService(Context context) this.context = context; /* * 往手机内存上存储用户名与密码的操作 * * param name * param pass * param fileName * return */ public boolean saveToRom(String name, String pass, String fileName) / 上下文对象的api try / 通过openFileOutput()方法获取一个文件的输出流对象 File

17、OutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); / 拼接用户名与密码 String result = name + : + pass; / 写入 fos.write(result.getBytes(); fos.flush(); fos.close(); catch (Exception e) e.printStackTrace(); return false; return true; / 读取数据 public Map readFile(String fileName) Map map =

18、 null;/ new HashMap(); try FileInputStream fis = context.openFileInput(fileName); String value = StreamTools.getValue(fis); String values = value.split(:); if (values.length 0) map = new HashMap(); map.put(name, values0); map.put(pass, values1); catch (Exception e) e.printStackTrace(); return map; p

19、ackage com.example.lession02_file.util; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; public class StreamTools public static String getValue(FileInputStream fis) throws Exception /字节的输出流对象 ByteArrayOutputStream stream = new ByteArrayOutputStream(); byte buffer = new byte1024;

20、 int length = -1; while (length = fis.read(buffer) != -1) stream.write(buffer, 0, length); stream.flush(); stream.close(); String value = stream.toString(); return value; day02_file Settings Hello world! 用户名 密 码 登 陆 保存密码 htmlpackage com.example.lession02_file.service; import java.io.ByteArrayOutputS

21、tream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import com.example.lession02_file.util.StreamTools; import android.content.Context; public class FileService / 上下文的对象 public Context context; public FileService(Context context) th

22、is.context = context; /* * 往手机内存上存储用户名与密码的操作 * * param name * param pass * param fileName * return */ public boolean saveToRom(String name, String pass, String fileName) / 上下文对象的api try / 通过openFileOutput()方法获取一个文件的输出流对象 FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);

23、/ 拼接用户名与密码 String result = name + : + pass; / 写入 fos.write(result.getBytes(); fos.flush(); fos.close(); catch (Exception e) e.printStackTrace(); return false; return true; / 读取数据 public Map readFile(String fileName) Map map = null;/ new HashMap(); try FileInputStream fis = context.openFileInput(file

24、Name); String value = StreamTools.getValue(fis); String values = value.split(:); if (values.length 0) map = new HashMap(); map.put(name, values0); map.put(pass, values1); catch (Exception e) e.printStackTrace(); return map; package com.example.lession02_file.service;import java.io.ByteArrayOutputStr

25、eam;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.HashMap;import java.util.Map;import com.example.lession02_file.util.StreamTools;import android.content.Context;public class FileService / 上下文的对象 public Context context; public FileService(Context context) this.contex

26、t = context; /* * 往手机内存上存储用户名与密码的操作 * * param name * param pass * param fileName * return */ public boolean saveToRom(String name, String pass, String fileName) / 上下文对象的api try / 通过openFileOutput()方法获取一个文件的输出流对象 FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); / 拼接用户名与密

27、码 String result = name + : + pass; / 写入 fos.write(result.getBytes(); fos.flush(); fos.close(); catch (Exception e) e.printStackTrace(); return false; return true; / 读取数据 public Map readFile(String fileName) Map map = null;/ new HashMap(); try FileInputStream fis = context.openFileInput(fileName); St

28、ring value = StreamTools.getValue(fis); String values = value.split(:); if (values.length 0) map = new HashMap(); map.put(name, values0); map.put(pass, values1); catch (Exception e) e.printStackTrace(); return map; java java htmlpackage com.example.lession02_file.util; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; public class StreamTools pub

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论