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. matplotlib绘图

    fig = plt.figure() ax=plt.gca() timeList = np.array(timeList) timeList=timeList*100 timeList1 = np.a ...

  2. Lodop客户端本地角色注册号常见误区

    之前写过一篇关于Lodop和c-lodop注册号的区别:LODOP.C-LODOP注册号的区别第一种角色客户端本地打印角色是最常见的角色,最常见的场景,关于c-lodop云打印,它的第一种角色是取代L ...

  3. maven 引用另一个jar包 需要先打包在仓库里面 并在pom里面配置 才可以引用

    maven 引用另一个jar包 需要先打包在仓库里面 并在pom里面配置 才可以引用

  4. Luogu4195 【模板】exBSGS(exBSGS)

    如果a和p互质,用扩欧求逆元就可以直接套用普通BSGS.考虑怎么将其化至这种情况. 注意到当x>=logp时gcd(ax,p)是一个定值,因为这样的话每个存在于a中的质因子,其在ax中的出现次数 ...

  5. 【Linux】Screen命令

    1.运行screen [root@master2 ~]# screen 2.执行脚本 [root@master2 ~]# sh mgr.sh 命令帮助 更详细的请使用 man screen查看 htt ...

  6. easyui webapi

    今天算是踩雷了.... 先说一下,由于项目需要,我目前开发PO模块, 由于需要提供手机端,所以我在mvc项目中创建了  webapi.提供手机端调用. 然后我就考虑,easyui也使用webapi来提 ...

  7. ubuntu16.04 NFS系统挂载

    一:服务器端 step1:关闭防火墙 sudo ufw disable step2:安装nfs sudo apt-get install nfs-kernel-server step3: 打开/etc ...

  8. java访问权限表

    private(私有的) 默认的(什么都不写) protected(受保护的) public(公共的 ) 同一个类中 yes   yes yes   yes 同一个包中不同类之间 no yes yes ...

  9. MT【53】对数平均做数列放缩

    [从最简单的做起]--波利亚 请看下面三道循序渐进不断加细的题. 评:随着右边的不断加细,解决问题的方法也越来越"高端".当然最佳值$ln2$我们可以用相对 容易的方法来证明: $ ...

  10. 说说Java 位运算

    前言 我们都知道,在计算机世界里,再复杂,再美的程序,到最后都会变成0与1.也就是我们常说的:二进制.二进制相信大家都很熟悉.与现实世界不同的是,在现实世界里,我们通常都是用十进制来表示的,也就是遇十 ...