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. ModelSim之命令行仿真入门

    下面是我们的Tcl仿真步骤:启动ModelSim SE, 首先看到在在ModelSim SE右边的窗口有ModelSim> 这样的提示符.在提示符后,顺序运行以下命令:    vlib work ...

  2. webstom 配置git 后左侧菜单栏配色调整

    ws配置了git进行版本管理,但是最近改了主题,发现左侧列表的文件名的颜色都是一个颜色了.我想要的是,对未提交的文件用颜色区分,新建的,修改的,冲突的分别设置不同的颜色,不知在哪里能设置,求助 老规矩 ...

  3. Android 设计随便说说之简单实践(消息流动)

    在上面两篇分别说明了设计中较为简单也是很关键的实践点. 第一模块划分,它是根据每个模块所承载的业务,进行划分,是应用程序一个静态的描述. 第二合理组合,它是是将每个模块调动起来,共同实现业务,是一个准 ...

  4. 20151211jquery ajax进阶代码备份

    //数据处理 $('form input[type=button]').click(function() { //json处理 /*$.ajax({ type:'POST', url:'test.js ...

  5. Android测试分析3

    一个基本的测试用例-- 如果是在eclipse中开发,那么需要在AndroidManifest.xml中加入如下两段代码:    <uses-library android:name=" ...

  6. oracle从各个表取得数据保存到另一个表

    从各个表中取得数据保存另一个表中: CREATE VIEW PARAMETER_view ASWITH tall AS ( SELECT p.PI_NO,--产品序列号 p.SERIALNO,--产品 ...

  7. struts2框架加载配置文件的顺序

    struts-default.xml:该文件保存在struts2-core-x.x.x.jar文件中: struts-plugin.xml:该文件保存在 struts2-Xxx-x.x.x.jar等S ...

  8. 如何在Angular2中使用Forms

    在Angular2中形成两个基本对象是Control和ControlGroup. 控制用户输入 Control 一个控制表示一个输入字段(ngFormControl) 一个控制可以绑定到一个input ...

  9. LevelDB windows vs2013 c++编译和测试

    引用: (src1) :http://download.csdn.net/detail/flyfish1986/8881263(这里有下载地址) (src2) :http://blog.csdn.ne ...

  10. 04_过滤器Filter_01_入门简述

    [简述] Filter也称之为过滤器.通过Filter技术,对web服务器管理的所有资源(如:Jsp.Servlet.静态图片文件.静态HTML文件等)进行拦截,从而实现一些特殊的功能.例如实现URL ...