package com.wuyou.login;

import java.io.IOException;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import com.wuyou.service.LoginService; public class MainActivity extends Activity { private CheckBox checkBox;
private EditText usernamEditText;
private EditText passwordeEditText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); checkBox = (CheckBox) this.findViewById(R.id.cb);
usernamEditText = (EditText) this.findViewById(R.id.username);
passwordeEditText = (EditText) this.findViewById(R.id.password); // 将之前保存在本地的账号密码取出并设置到文本框中
try {
Map<String, String> map = LoginService.getInfo(this);
usernamEditText.setText(map.get("username"));
passwordeEditText.setText(map.get("password")); } catch (Exception e) {
Log.i("main", "取不出账号密码");
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} /**
* 点击“登陆”按钮触发的时间
*
* @param v
*/
public void login(View v) {
Log.i("login", "在登陆中");
String username = usernamEditText.getText().toString().trim();
String password = passwordeEditText.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和账号不能为空!", Toast.LENGTH_SHORT).show();
} else {
// 判断是否选择了记住密码
if (checkBox.isChecked()) {
try {
LoginService.saveInfo(this, username, password);
Toast.makeText(this, "记住密码成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "无法记住密码", Toast.LENGTH_SHORT).show();
}
}
if ("zhangsan".equals(username) && "123".equals(password)) {
Toast.makeText(this, "登陆成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "账号密码有误!", Toast.LENGTH_SHORT).show();
}
} } }
<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=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入账号" /> <EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码" /> <EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="记住密码" /> <!-- 记住login的方法有一个参数View -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="login"
android:text="登陆" />
</RelativeLayout> </LinearLayout>
package com.wuyou.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map; import android.content.Context; public class LoginService { public static void saveInfo(Context context, String username,
String password) throws IOException {
File file = new File(context.getFilesDir(), "info.txt");
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(username + "##" + password);
fileWriter.close();
} public static Map<String, String> getInfo(Context context)
throws IOException {
     //这里只做简单的保存,如果要保存配置文件信息,可以使用SharedPreference类保存,请关注本类别的该文章
File file = new File(context.getFilesDir(),"info.txt");
Map<String, String> map = new HashMap<String, String>();
BufferedReader bf = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String info = bf.readLine();
String username = "";
String password = "";
if (info != null) {
String[] infos = info.split("##");
username = infos[0];
password = infos[1];
map.put("username", username);
map.put("password", password); }
bf.close();
return map;
}
}

Android 模拟登陆 保存密码(信息)到手机中 文件信息读取的更多相关文章

  1. Android忘记锁屏密码如何进入手机?

    Android忘记锁屏密码如何进入手机?     1.关闭手机 2.进入recovery模式(即恢复模式,记住不是挖煤模式.进入恢复模式不同手机有不同方法,三星的话安主页键,关机键和音量+(或-键), ...

  2. Android学习笔记——保存数据到SQL数据库中(Saving Data in SQL Databases)

    知识点: 1.使用SQL Helper创建数据库 2.数据的增删查改(PRDU:Put.Read.Delete.Update) 背景知识: 上篇文章学习了保存文件,今天学习的是保存数据到SQL数据库中 ...

  3. python保存二维列表到txt文件,读取txt文件里面的数据转化为二维列表

    源码: # 读文件里面的数据转化为二维列表 def Read_list(filename): file1 = open(filename+".txt", "r" ...

  4. 【ASP.NET 进阶】获取MP3文件信息并显示专辑图片

    突发奇想,想弄个显示MP3文件信息和专辑图片的小Demo,个人不是大牛,遂百度之,总算搞定,现分享如下. 效果图: GIF效果图: 主要是依靠2个DLL文件:ID3.dll 和 Interop.She ...

  5. Android——用户登陆及用户名和密码的保存

    Android——用户登陆及用户名和密码的保存   在之前的学习过程中已经将Android学习完了,但是在后面将近一年的时间里都没有进行过Android开发,所以对Android的所有的知识点又有点忘 ...

  6. [android] 手机卫士保存密码时进行md5加密

    一般的手机没有root权限,进不去data/data目录,当手机刷机了后,拥有root权限,就可以进入data/data目录,查看我们保存的密码文件,因此我们需要对存入的密码进行MD5加密 获取Mes ...

  7. HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息

    前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...

  8. Android模拟位置信息

    Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服 ...

  9. 让 Putty 保存密码,自动登陆的四种方法

    Putty 基本是我在紧急时候用来登陆 Linux/Unix 终端的不二之先,因其小,开源,界面也非常实用.可是当你要在私有的机器上,经常性的要登陆很多机器的时候就觉得烦琐了,不光打开一堆的窗口,还要 ...

随机推荐

  1. Zookeeper简介与安装

    Zookeeper:A Distributed Coordination Service for Distributed Applications. 一.Zookeeper简介 众所周知,协同服务是分 ...

  2. ubuntu系统安装redis

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串string,链表list,集 合set和有序集合zset.支持在服务器 ...

  3. 在AngularJS的controller外部直接获取$scope

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5560843.html ...

  4. 盘点 Github 所用到的开源项目

    http://www.php100.com/html/it/mobile/2014/0401/6736.html 在致力于开源事业的同时,Github也使用一些非常优秀的开源项目的来打造自己的平台与服 ...

  5. elfiner-servlet 2.x已开源!

    通过近一周的努力,elfiner-servlet 2.x基本搞定! 已提交github,开源之!请各位享用~~ 对elfinder不熟悉的请访问:http://elfinder.org 一个很不错的文 ...

  6. 针对wordpress的二次开发

    0.基础nginx\mysql\php\html\css\js 1. 搭建环境mac + nginx + mysql + wordpresshttp://segmentfault.com/a/1190 ...

  7. .NET程序编译原理

    导语: CPU只认识二进制代码,那么C#源代码是怎样变成CPU可识别的二进制代码的呢? 步骤如下: 1.C#源码 2.运用VS自带的命令提示窗口,使用csc命令将C#源码转成程序集(EXE文件或DLL ...

  8. 20160314 Servlet 入门

    一.Servlet 1.sun提供的一种动态web资源开发技术.本质上就是一段java小程序.可以将Servlet加入到Servlet容器中运行. *Servlet容器 -- 能够运行Servlet的 ...

  9. 关于ligerui 中 grid 表格的扩展搜索功能在远程数据加载时无法使用的解决办法

    要想使用grid里的扩展搜索功能,除了要引用ligerui主要的js文件外,还必须引入下面的JS文件: 1.Source\demos\filter\ligerGrid.showFilter.js 2. ...

  10. hadoop_集群安装_1

    这篇文章中主要介绍的是,如何基于VM安装Linux,以及如何在安装好Linux之后,基于操作系统安装VMTools. 在安装之前,应该先规划好 每个node*的IP地址,以及 hostname: no ...