保存密保在手机内存
来源:互联网 发布:云计算 网络强国 编辑:程序博客网 时间:2024/04/28 02:15
<span style="font-size:18px;">package com.melody.savepasswd;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import com.melody.savepasswd.R;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_name;private EditText et_passwd;private CheckBox cb;private FileOutputStream fos;private FileInputStream fis;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();readAccount();}//读取进度方法private void readAccount() {//读取文件,回显数据//File file = new File("data/data/"+getPackageName()+"/login.txt");File file = new File(getCacheDir(),"login.txt");if(file.exists()){try {fis = new FileInputStream(file);//把字节流转换成字符流BufferedReader br = new BufferedReader(new InputStreamReader(fis));//读出来的数据存入到文本 一行一行读String text = br.readLine();//回显数据 split 字符切割String s[] = text.split("&&");//给输入框设置文本et_name.setText(s[0]);et_passwd.setText(s[1]);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}}private void init() {et_name = (EditText) findViewById(R.id.et_name);et_passwd = (EditText) findViewById(R.id.et_passwd);cb = (CheckBox) findViewById(R.id.cb);}public void login(View v){//获取用户名和密码String name = et_name.getText().toString();String passwd = et_passwd.getText().toString();//判断复选框是否选中if(cb.isChecked()){//创建一个文件保存用户名和密码 手机内部存储路径 data/data/包名/文件名//File file = new File("data/data/"+getPackageName()+"/login.txt");//返回一个File对象,封装路径是 data/data/包名/files = getFilesDir()//File file = new File(getFilesDir(),"login.txt");//返回一个File对象,封装路径是 data/data/包名/cache = getCacheDir()File file = new File(getCacheDir(),"login.txt"); try {fos = new FileOutputStream(file);//把帐号和密码写如文件fos.write((name+"&&"+passwd).getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fos != null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}Toast.makeText(this, "登录成功", 0).show();}}</span>
//////////////////////////////////////////布局文件/////////////////////////////////////////////////////////
<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"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入帐号" /> <EditText android:id="@+id/et_passwd" android:layout_width="match_parent" android:layout_height="wrap_content" <span style="white-space:pre"></span>android:hint="请输入密码" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"><span style="white-space:pre"></span> <CheckBox <span style="white-space:pre"></span> android:id="@+id/cb"<span style="white-space:pre"></span> android:layout_width="wrap_content"<span style="white-space:pre"></span> android:layout_height="wrap_content"<span style="white-space:pre"></span> android:text="记住帐号和密码"<span style="white-space:pre"></span> android:layout_centerVertical="true"<span style="white-space:pre"></span> /><span style="white-space:pre"></span><span style="white-space:pre"></span> <Button <span style="white-space:pre"></span> android:layout_width="wrap_content"<span style="white-space:pre"></span> android:layout_height="wrap_content"<span style="white-space:pre"></span> android:layout_alignParentRight="true"<span style="white-space:pre"></span> android:onClick="login"<span style="white-space:pre"></span> android:text="登录"/> </RelativeLayout></LinearLayout>
0 0
- 保存密保在手机内存
- 如何在百度推广里加入两个密保手机
- 保存数据到手机内存
- 保存数据到手机内存
- 保存文件到手机内存
- 保存文件到手机内存
- 保存数据到手机内存
- 保存文件到手机内存
- 诺基亚手机保密码
- 保存 log 到手机内存的代码
- 5 保存文件到手机内存
- android保存文件到手机内存
- android保存到手机内存【数据存储】
- 保存数据到手机内存(QQ登录保存密码)
- android,保存文件到手机内存和读取
- 保存文件到手机内存与使用密码登陆
- 24、25_保存文件到手机内存
- Android---09---保存数据到手机内存和存储卡
- php字符串处理函数大全
- python下配置matplotlib开发环境
- document.createElement()用法中appendChild()、 insertBefore()的区别
- Javascript模块化编程(三):require.js的用法
- 写在前面 把自己的学习之路记录下来
- 保存密保在手机内存
- 解决微信浏览器苹果手机复制的时候连旁边的也复制的办法
- (转自系统信息)Java中Integer类的方法
- 建立一个隐式意图
- 时间是最好的老师
- 在Ubuntu15.10安装F.lux
- 远程连接linux上的mysql过程资料合集
- GPIO模拟I2C操作调试注意事项
- Oracle 收缩表空间