Android学习笔记(4)——登陆界面模拟及存储文件到内存
来源:互联网 发布:手机端js图片上传裁剪 编辑:程序博客网 时间:2024/05/08 15:11
搬运自本人博客:xge技术博客
原文:http://www.xgezhang.com/android_login_save_file.html
在本机或服务器上保存文件是比较简单的一件事,那么在安卓系统下我们该如何存储文件呢?这里我们借用登陆界面常见的“记住登陆用户名密码”的为例,来介绍一下如何把文件保存到手机内存,也综合复习和练习一下之前的内容:
首先我们还是先做界面:
对应的xml布局文件如下,采用的是线性布局加上相对布局来实现的:
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
tools:context
=
"com.xgezhang.login.MainActivity"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/please_input_user_name"
/>
<
EditText
android:id
=
"@+id/et_username"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:singleLine
=
"true"
/>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/please_input_password"
/>
<
EditText
android:id
=
"@+id/et_password"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:singleLine
=
"true"
android:password
=
"true"
/>
<
RelativeLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
>
<
TextView
android:id
=
"@+id/tv_remenber_user_info"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_centerVertical
=
"true"
android:text
=
"@string/remenber_user_info"
/>
<
CheckBox
android:id
=
"@+id/cb"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_toRightOf
=
"@id/tv_remenber_user_info"
android:checked
=
"true"
/>
<
Button
android:id
=
"@+id/bt_login"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentRight
=
"true"
android:text
=
"@string/bt_login"
/>
</
RelativeLayout
>
</
LinearLayout
>
以上xml很容易理解,只是注意以下几点:
- Edittext要设置为singleLine=true;
- 密码内容要设置password=true,不然会是明文显示;
- Checkbox中可以通过指定checked参数的值来选择默认勾选情况;
接下来我们来看MainActivity代码,稍微理下思路:我们首先需要注册一个登陆按钮的事件,当点击登陆按钮之后,会读取et_username和et_password的值,判断是否为空,如果不为空,则验证是否是正确的用户名和密码。同时,如果我们选择了保存用户登陆信息,则需要把用户名密码通过文本的方式存储到手机中。
当然,每次程序运行的时候,会自动读取这个文本,如果文本中有内容,则自动填写到两个EditText里面,相当于保存内容的回显。
接下来我们来看看代码,因为这里只是模拟登陆,并不是真正的把数据发送到服务器再从database里面select并验证,所以就采用很基本的方式判断了。
MainActivity.java
package
com.xgezhang.login;
import
java.util.HashMap;
import
java.util.Map;
import
com.xgezhang.login.service.loginService;
import
android.app.Activity;
import
android.os.Bundle;
import
android.text.TextUtils;
import
android.util.Log;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.CheckBox;
import
android.widget.EditText;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
implements
OnClickListener {
private
static
final
String TAG =
"MainActivity"
;
private
EditText et_username;
private
EditText et_password;
private
CheckBox cb;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb = (CheckBox) findViewById(R.id.cb);
Map <String,String> userInfoMap = loginService.getUserInfoMap(
this
);
//回显用户已经保存的登陆数据
if
(userInfoMap!=
null
){
et_username.setText(userInfoMap.get(
"username"
));
et_password.setText(userInfoMap.get(
"password"
));
}
Button bt_login = (Button) findViewById(R.id.bt_login);
bt_login.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.bt_login:
// 登陆事件
String username = et_username.getText().toString().trim();
String password = et_password.getText().toString().trim();
if
(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(
this
,
"用户名或密码为空"
, Toast.LENGTH_SHORT).show();
}
else
{
if
(cb.isChecked()) {
boolean
result = loginService.saveUserInfo(
this
, username, password);
if
(result){
Toast.makeText(
this
,
"登陆信息已保存"
, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(
this
,
"登陆信息保存异常"
, Toast.LENGTH_SHORT).show();
}
}
if
(username.equals(
"zhangsan"
) && password.equals(
"123456"
)) {
Toast.makeText(
this
,
"登陆成功"
, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(
this
,
"登陆失败,用户名或密码错误"
, Toast.LENGTH_SHORT).show();
}
}
break
;
default
:
break
;
}
}
}
其中有两个函数是需要我们进一步实现的,分别是saveUserInfo和getUserInfoMap。出于方便的目的,我们新建一个package命名为com.xgezhang.login.service,并新建一个class名为loginService,来存放登陆会用到的一些函数。
我们先介绍saveUserInfo函数,这里涉及到安卓系统数据保存的问题。我们需要知道的是,在Android系统下有一个/data/data路径,这里存放了所有安装的app的数据,在Eclipse中我们可以通过DDMS来查看:
每个app对应的文件夹里面默认有files和cache,其中cache用于存储缓存文件。
了解了存储的原理,我们就可以开始写代码了:
static
public
boolean
saveUserInfo(Context context, String username,String password) {
try
{
File file =
new
File(context.getFilesDir(),
"userinfo.txt"
);
FileOutputStream fos =
new
FileOutputStream(file);
fos.write((username +
"###"
+ password).getBytes());
fos.close();
return
true
;
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
}
上述代码比较容易理解,和Java中文件操作没有两样,只是注意几个问题,一个是new File的路径不建议写成固定的路径,而是通过传递一个context,通过context.getFilesDir()函数来直接获取路径;另外,由于有文件的操作,这里需要加上try块来避免异常,并且如果出现异常将其catch掉,并返回false,供调用函数去判断。
这里的用户名密码是通过“###”符号来分割的,肯定是不太合理的,这里不用深究。
接下来是getUserInfoMap函数,和上述代码差不多,
static
public
Map<String,String> getUserInfoMap(Context context){
try
{
File file =
new
File(context.getFilesDir(),
"userinfo.txt"
);
FileInputStream fis =
new
FileInputStream(file);
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(fis));
String str = br.readLine();
String[] userinfos = str.split(
"###"
);
Map<String, String> map =
new
HashMap<String, String>();
map.put(
"username"
, userinfos[
0
]);
map.put(
"password"
,userinfos[
1
]);
return
map;
}
catch
(Exception e) {
e.printStackTrace();
return
null
;
}
}
这里的异常是返回null,所以外部只用判断返回的Map是否为null即可。
另外,由于这两个函数显然都不需要将类实体化,所以我们可以直接写成static,在Android项目中推荐大家使用static函数,可以提高程序运行效率。
完整的loginService.java代码如下:
package
com.xgezhang.login.service;
import
java.io.BufferedReader;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStreamReader;
import
java.util.HashMap;
import
java.util.Map;
import
android.R.string;
import
android.content.Context;
public
class
loginService {
/**
* @param context 上下文
* @param username 用户名
* @param password 密码
* @return boolean true表示保存成功 false表示保存失败
*/
static
public
boolean
saveUserInfo(Context context, String username,String password) {
try
{
File file =
new
File(context.getFilesDir(),
"userinfo.txt"
);
FileOutputStream fos =
new
FileOutputStream(file);
fos.write((username +
"###"
+ password).getBytes());
fos.close();
return
true
;
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
}
/**
* 获取用户保存的登陆信息
* @param context
* @return Map
*/
static
public
Map<String,String> getUserInfoMap(Context context){
try
{
File file =
new
File(context.getFilesDir(),
"userinfo.txt"
);
FileInputStream fis =
new
FileInputStream(file);
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(fis));
String str = br.readLine();
String[] userinfos = str.split(
"###"
);
Map<String, String> map =
new
HashMap<String, String>();
map.put(
"username"
, userinfos[
0
]);
map.put(
"password"
,userinfos[
1
]);
return
map;
}
catch
(Exception e) {
e.printStackTrace();
return
null
;
}
}
}
至此,程序结束,可以用虚拟机运行测试一下。
欢迎转载,请注明出处。
- Android学习笔记(4)——登陆界面模拟及存储文件到内存
- android移动应用开发学习笔记(五)——模拟qq登陆界面
- Android登陆界面采用文件存储实现
- Android学习笔记(一)——仿qq聊天的welcome界面登陆的实现
- Android学习笔记——文件存储
- android学习笔记(3)---QQ登陆界面
- Android学习笔记(19)————实现一个记住密码的Android登陆界面
- Android学习笔记(19)————实现一个记住密码的Android登陆界面 .
- android初学——登陆界面及跳转设计
- Android学习笔记12——内存卡操作及文件下载功能的实现
- android 学习笔记 数据存储到文件中
- windows程序设计——飞机大战笔记(单文档文件登陆界面)
- 学习笔记3——存储,内存
- OpenCV学习笔记(五)—内存存储
- Android学习系列(二): 保存文件到手机内存-模拟用户登录保存用户信息
- 井位坐标添加到Las文件功能——Tkinter界面开发学习笔记一
- PHP网站——返回上一页(登陆后自动到登陆前的界面)
- Android学习笔记(十)--OKHttp持久化cookies实现模拟登陆
- Linux 内核中的 Device Mapper 机制
- 链接
- Pythonxy'import sitecustomize'错误的解决办法
- [LeetCode]200.Number of Islands
- Ubuntu 14.04 Nvidia显卡驱动安装及设置
- Android学习笔记(4)——登陆界面模拟及存储文件到内存
- ZOJ 3776 Pokemon Master
- 蓝桥杯 地宫取宝 DP
- STM32学习笔记一(LED,跑马灯,呼吸灯)
- python 删除 ._文件
- 三水一男子利用手机店作掩护,售卖淫秽视频,最终被警方抓获
- Android---利用android-async-http开源项目返回json数据
- js实现上传图片--上传前预览图片
- 【Android面试】(一):Android中activity保存状态和数据到底该在哪个方法中进行