1.布局

<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="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.logindemo.MainActivity" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入用户名" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_username"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入密码" />
<EditText
android:inputType="textPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cb_remember"
android:text="记住密码"
android:checked="true"
/>
<Button
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="登录"
android:id="@+id/btn_login" />
</RelativeLayout> </LinearLayout>

2.MainActivity.java

package com.example.logindemo;

import java.util.Map;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import com.example.logindemo.service.LoginService; public class MainActivity extends ActionBarActivity { private EditText et_username;
private EditText et_pwd;
private CheckBox cb_remember; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText)this.findViewById(R.id.et_username);
et_pwd = (EditText)this.findViewById(R.id.et_password);
cb_remember = (CheckBox)this.findViewById(R.id.cb_remember); Map<String,String> map = LoginService.getUserInfo(this);
if(map!=null)
{
et_username.setText(map.get("username"));
et_pwd.setText(map.get("password"));
}
} public void login(View v){ String username = et_username.getText().toString();
String pwd = et_pwd.getText().toString(); if(TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)){ Toast.makeText(MainActivity.this, "username or password is empty", Toast.LENGTH_SHORT).show();
return;
}
else
{ if(cb_remember.isChecked())
{
boolean result = LoginService.saveUserInfo(this, username, pwd);
if(result)
{
Toast.makeText(MainActivity.this, "save user info success", Toast.LENGTH_SHORT).show();
}
} if("test".equals(username) && "123".equals(pwd))
{
Toast.makeText(MainActivity.this, "login success", Toast.LENGTH_SHORT).show();
}
else
{ Toast.makeText(MainActivity.this, "username or password is error", Toast.LENGTH_SHORT).show();
}
} }
}

3.service

package com.example.logindemo.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.content.Context; public class LoginService { public static boolean saveUserInfo(Context context,String username,String pwd)
{
try
{
File file = new File(context.getFilesDir(),"info.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write((username+"##"+pwd).getBytes());
fos.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
} } public static Map<String,String> getUserInfo(Context context)
{
try
{
File file = new File(context.getFilesDir(),"info.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String strs = br.readLine();
String arr[] = strs.split("##");
Map<String,String> map = new HashMap<String,String>();
map.put("username", arr[0]);
map.put("password", arr[1]);
return map; }
catch(Exception e)
{
e.printStackTrace();
return null;
} } }

android 简单文件操作的更多相关文章

  1. Android简单文件浏览器源代码 (转)

    Android简单文件浏览器源代码 (转) activity_main .xml <LinearLayout xmlns:android="http://schemas.android ...

  2. android的文件操作

    http://blog.csdn.net/fenghome/article/details/5668598 android的文件操作要有权限: <uses-permission android: ...

  3. android通用文件操作

    最经用到android的SCCard的文件操作,因此稍作了整理,将它写成一个简单的工具类.其中,可以判断SDCard的是否可用,可用存储空间,文件的创建以及写入数据.经过测试,可以正常使用.代码如下: ...

  4. Android FileUtils 文件操作类

    系统路径 Context.getPackageName(); // 用于获取APP的所在包目录 Context.getPackageCodePath(); //来获得当前应用程序对应的apk文件的路径 ...

  5. 也发一个自己实现的android简单文件选择器代码。支持多卡,排序

    一个很简单的文件选择器对话框,支持双sd卡,当然前提是要有sd卡..并且实现了排序效果. 只有100多行的代码,基本的思路就是用listview显示目录下的所有子文件,再判断是文件还是目录. 利用Co ...

  6. python简单文件操作

    写软件著作申请,需要将所有源代码代码贴入一个word中,在源代码前后要添加一些标记,如下: //////////////////////////// //filename1 ///////////// ...

  7. Android 图片文件操作、屏幕相关、.9图片的理解

     一:Android图片操作 1.存储bitmap到本地文件系统 public static void bitmapToFile(Bitmap bitmap, String uri) { if(!ex ...

  8. android中文件操作的四种枚举

    1.MODE_PRIVATE:默认操作模式,代表该文件是私有数据,只能被应用自身访问,在该模式下,写入的的内容会覆盖原文件中的内容. 2.MODE_APPEND:该模式会检查文件是否存在,存在就往文件 ...

  9. Android写入文件操作权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses- ...

随机推荐

  1. 破解xlsm文件的VBA项目密码和xlsx的工作簿保护密码

    工具 待破解的xlsm文件.Excel2010.Hex Editor 步骤 1.修改.xlsm后缀为.zip 2.使用压缩软件打开,进入xl目录找到vbaProject.bin文件,解压出来 3.使用 ...

  2. (String)强制转换、toString()和String.valueOf()的区别

    String:毫无疑问,这种就是强转形式,简单方便,效率高.java程序员可能看到效率高或许有些激动,但是它有他的不好,那就是局限性.在java的世界里没有什么东西是没有缺点的,总有一些好的方面同时也 ...

  3. LinkedList 实现 Queue

    package cn.com.example; import java.util.LinkedList; /** * Created by Jack on 2017/3/8. */ public cl ...

  4. 【Linux】工作管理

    在进行工作管理的行为中,其实每个工作都是目前bash的子进程,即彼此间是有相关性的.我们无法以job control的方式由tty1的环境去管理tty2的bash 当只有一个终端时,可以出现提示符让你 ...

  5. length、length()、size()区别 List与String相互转换

      字符串 数组 List对象 定义 String str = ""; String[] s = new String[5]; char[] s; List<String&g ...

  6. day11 匿名函数

    格式 lambda 形参 :逻辑运算方式 lambda x:x+1 普通的方式计算 卧槽.这么长! def calc(x): return x+1 res = calc(10) print(res) ...

  7. 【比赛】NOIP2018 总结

    一.考试过程 Day1: 先看了一遍题目,得到的结论是没有题是直接秒掉的,然后一道一道认真看. 看T1的时候开始并没想起来有一道原题,只是脑海中有一个印象,好像求差分和可以.然后自测了一下小样例,发现 ...

  8. 自学Zabbix12.5 Zabbix命令-zabbix_proxy

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.5 Zabbix命令-zabbix_proxy 1. zabbix prox ...

  9. BZOJ 400题纪念

    应该是最后一次纪念了吧! 想当年,我可是发过"BZOJ 10题纪念"的人--那时候(一年前?)的自己真的好菜啊,只能说掌握了c++的基础语法的样子.当时觉得省选级别的BZOJ题是世 ...

  10. 洛谷 P2466 Sue的小球 解题报告

    P2466 [SDOI2008]Sue的小球 题目描述 Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当 ...