JAVA登陆界面的数据保存回显的操作
《JAVA登陆界面的数据保存回显的操作》由会员分享,可在线阅读,更多相关《JAVA登陆界面的数据保存回显的操作(15页珍藏版)》请在装配图网上搜索。
1、JAVA登陆界面的数据保存回显的操作 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; import android.view.View; import android.widget.Button;
2、 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
3、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
4、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
5、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
6、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 / 如果
7、记住密码勾选上了 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(); package com.ex
8、ample.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.Button;import android.widget.CheckBox;import android.
9、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 CheckBox box_remember; Override protected void onCre
10、ate(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) findViewById(R.id.edit_pass); btn_login = (Button) findViewB
11、yId(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_pass.setText(map.get(edit_pass); Override public bool
12、ean 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 onClick(View v) int id = v.getId(); if (id = btn_l
13、ogin.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 / 如果记住密码勾选上了 if (box_remember.isChecked() / 进行保存 / 调用业
14、务对象的业务方法 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 javapackage com.example.lession02_file.service; import
15、 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 FileService / 上下文的对象 public Context context; public FileSe
16、rvice(Context context) this.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
17、, 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 = null;/ new HashMap(); try FileInputStream fis = c
18、ontext.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; package com.example.lession02_file.util; import jav
19、a.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; int length = -1; while (length = fis.read(buffer)
20、 != -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.ByteArrayOutputStream; import java.io.FileInputStream; import java
21、.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.context = context; /* * 往手机内存上存储用户名与密码的操作 * * p
22、aram 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); / 拼接用户名与密码 String result = name + : + pass; / 写入 f
23、os.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); String value = StreamTools.getValue(fis); S
24、tring 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.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.
25、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.context = context; /* * 往手机内存上存储用户名与密码的操作 * * param name
26、 * 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); / 拼接用户名与密码 String result = name + : + pass; / 写入 fos.write(
27、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); String value = StreamTools.getValue(fis); String val
28、ues = 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 cla
29、ss StreamTools public static String getValue(FileInputStream fis) throws Exception /字节的输出流对象 ByteArrayOutputStream stream = new ByteArrayOutputStream(); byte buffer = new byte1024; int length = -1; while (length = fis.read(buffer) != -1) stream.write(buffer, 0, length); stream.flush(); stream.close(
30、); String value = stream.toString(); return value; package 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 Byt
31、eArrayOutputStream(); byte buffer = new byte1024; int length = -1; while (length = fis.read(buffer) != -1) stream.write(buffer, 0, length); stream.flush(); stream.close(); String value = stream.toString(); return value; html EditText android:id=+id/edit_name android:layout_width=0dp android:layout_height=wrap_content android:layout_weight=1 android
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。