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. Netbeans7.0完美中文+Consolas字体显示配置(亲测可用)

    最近把开发环境从Eclipse迁移到了Netbeans上面.因为Netbeans已经相当优秀,速度快功能也不必Eclipse差,但是一只有 一个问题一直让我对eclipse非常纠结:如果把字体选择为C ...

  2. Eval()和DataBinder Eval(Container DataItem,)的区别及用法

        ASP.NET 2.0改善了模板中的数据绑定操作把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fiel ...

  3. Working with BeforeProperties and AfterProperties on SPItemEventReceiver

    As many of you know, event receivers are a great way to hook into various SharePoint events.  These ...

  4. [转]jQuery选择器总结

    该文章转载自:http://www.cnblogs.com/onlys/articles/jQuery.html jQuery的选择器那绝对最强大的,各种你想不到,原先想总结一下,没想到搜索到这个比我 ...

  5. mvc Routing特性优化

    在mvc中,Url地址是利用routing特性来支持,但是这个Routing有个问题,多个不同的地址和指向同一个action方法, 例如: http://test.com (默认) http://te ...

  6. js单击显示元素,点击元素本身以外隐藏元素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 24种设计模式--策略模式【Strategy Pattern】

    刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手问题,嘿,还别说,真是解决了大问题,搞到最后是周瑜赔了夫人有折兵呀,那咱们先看看这个场景是什么样子的. 先说这个场景 ...

  8. Netty源码阅读(一) ServerBootstrap启动

    Netty源码阅读(一) ServerBootstrap启动 转自我的Github Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速 ...

  9. javascript 事件流及应用

    当页面元素触发事件的时候,该元素的容器以及整个页面都会按照特定顺序发生该元素的触发 事件,事件传播的顺序叫做事件流 1.事件流的分类: A.冒泡型事件(所有浏览器都支持)   由明确的事件源到最不确定 ...

  10. javascript 对象的创建,引用,释放,删除方法

    1.用函数构造 A.声明时同时设置属性和方法 function func(){  this.name = "myname";  this.say = function(){aler ...