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. 设计模式笔记:适配器模式(Adapter)

    1. 适配器模式简介 1.1 模式定义 适配器模式:通过一个类的接口转换成客户希望的另外一个接口,使原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器从结构上分为:类适配器和对象适配器.其 ...

  2. BZOJ4385[POI2015]Wilcze doły——单调队列+双指针

    题目描述 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. 输入 第一行包含三个整数n,p ...

  3. linux screen 命令 :离线运行程序

    screen工具是linux下虚拟终端的一个常用工具.在 发现这个工具之前,笔者经常在远程ssh中运行需要长时间处理数据的命令,比如远程编译安装软件,如果在编译的过程中网络断开,那这个编译进程就会停止 ...

  4. SharePoint 2013 event id 8321 错误

    SharePoint 2013里会报8321的错误: A certificate validation operation took 15011.1412 milliseconds and has e ...

  5. emwin之在WM_INIT_DIALOG分支下使用带触发功能的函数的程序框架

    @2018-08-29 [小记] 为避免在窗口创建时由于使用了带触发功能的函数导致执行一些在初始化过程中不允许的操作,特整理一个流程架构 --① 定义一个初始化完成的标志 unsigned ; --② ...

  6. Spring Cloud(三) --- hystrix

    Hystrix 说到Hystrix就得先说一下产生的背景等等,那就是雪崩效应. 在微服务中肯定存在多个服务层之间的调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服 ...

  7. javascript面向对象精要第六章对象模式整理精要

    混入是一种给对象添加功能同时避免继承的强有力的方式,混入时将一个属性从一个对象 复制到另一个,从而使得接收者在不需要继承的情况下获得其功能.和继承不同,混入之后 对象无法检查属性来源.因此混入最适宜用 ...

  8. R语言:多个因变量时,如何在plot函数中画多条曲线(plot,points,lines,legend函数)

    最近阅读一篇文献<Regional and individual variations in the function of the human eccrine sweat gland>, ...

  9. 'RegAsm.exe' 不是内部或外部命令

    我想从cmd运行regasm.exe.它在c:\windows \Microsoft.net\framework\2.057 中可用 我喜欢这个c:\ regasm.exe 它给予 regasm无法识 ...

  10. python 正则括号的使用及踩坑

    直接先上结论: 若匹配规则里有1个括号------返回的是括号所匹配到的结果, 若匹配规则里有多个括号------返回多个括号分别匹配到的结果, 若匹配规则里没有括号------就返回整条语句所匹配到 ...