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. html5 简介

    html5基于html.html dom.xhtml的新版本. 为html5建立的一些新规则: 基于html.dom.xhtml和javascript: 减少对外部插件的需求,比如flash: 更多取 ...

  2. js timeout

    setTimeOut(“”,毫秒):調用函數時,不是立刻執行,而是間隔一定的時間后在執行: clearTimeOut():清除計時器

  3. 学习笔记之form表单

    form表单提交的数据 是字典类型 这样 方便在create时候 直接解压

  4. Dictionary字典

    泛型,键值对 Dictionary<int,string> dic=new Dictionary<int,string>(); dic.Add(,"张三") ...

  5. BZOJ4541 HNOI2016矿区(平面图转对偶图)

    考虑先将平面图转化为对偶图.具体地,将无向边拆成两条有向边.每次考虑找到包围一个区域的所有边.对当前考虑的边,找到该边的反向边在该边终点的出边集中,按极角序排序的后继,这条后继边也是包围该区域的边.这 ...

  6. MT【235】两道函数题

    已知$g(x)=x^2-ax+4a$,记$h(x)=|\dfrac{x}{g(x)}|$,若$h(x)$在$(0,1]$上单调递增,求$a$的取值范围. 解答: 已知$$g(x)=\begin{cas ...

  7. (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)

    前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...

  8. 【转】在单片机中,C语言的一些误用和总结!

    在学习单片机的时候才真正知道C语言是什么,它是来干什么的~但是C语言用到嵌入式只是它小小的一部分应用,还有很多地方呢. 我们是不是在写程序的时候,错误很多就算编译通过了也达不到我们预期的结果,完了自己 ...

  9. 青蛙跳台阶(C、Python)

    C语言: /* ----------------------------------- 当n = 1, 只有1中跳法:当n = 2时,有两种跳法:当n = 3 时,有3种跳法:当n = 4时,有5种跳 ...

  10. js中if()条件中变量为false的情况

    <html> <head> <script type="text/javascript" src="jquery-3.1.1.min.js& ...