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. Modify the average program to promote for intergers repeatedly.stop when a nagetive number is entere

    #include<stdio.h> int main(void) { intcount ,sum,aninterger; printf("enterthe interger an ...

  2. (转)OpenVPN使用HTTP代理连接服务器

    原文地址:http://www.365mini.com/page/18.htm 在一些公司或者其他受限的网络环境中,使用的是HTTP代理服务器上网.在这种情况下,使用OpenVPN客户端可能无法连接服 ...

  3. JQuery对象与DOM对象分析

    一.定义: DOM对象(文档对象模型):暂时这么理解:通过JavaScript获取的HTML元素,称为DOM对象.如:var domID=document.getElementById("i ...

  4. Oracle删除多张表

    项目中遇到要删除多张表,发现不能同时删除,可以先查询出SQL语句,然后批量执行 1.查询出SQL语句: select 'drop table '||table_name || ';' from use ...

  5. SQL 有父标识的 递归查询

    递归查询,临时表的高级应用 WITH temp AS ( --父项 SELECT * FROM Ar_Area WHERE Ar_Parent = UNION ALL --递归结果集中的下级 SELE ...

  6. java Email发送及中文乱码处理。

    public class mail { private String pop3Server=""; private String smtpServer=""; ...

  7. mysql 账户操作

    1.授权 mysql> grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’; 权限1,权限2,…权限n代表selec ...

  8. 08_一个java.lang.NullPointException报错

    [报错代码] public class UserDaoTest{ private SqlSessionFactory sqlSessionFactory; //此方法在执行testFindUserBy ...

  9. 九度OJ 1082 代理服务器 -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1082 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务 ...

  10. (转)UIColor,CGColor,CIColor三者的区别和联系

    最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...