Android简易实战教程--第七话《在内存中存储用户名和密码》
来源:互联网 发布:安正时尚集团 知乎 编辑:程序博客网 时间:2024/05/21 01:52
首先是配置文件:
<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: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" 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_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="请输入密码" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" ><CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名和密码" android:layout_centerVertical="true" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="登录" android:layout_alignParentRight="true" android:onClick="login" /></RelativeLayout></LinearLayout>
活动中的代码如下:
package com.itydl.rwinrom;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.InputStreamReader;import org.apache.http.entity.InputStreamEntity;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.view.Menu;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_pass;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_pass = (EditText) findViewById(R.id.et_pass); readAccount();//在onCreate中读取原因是,活动一创建就读取用户名和密码进行回显。 } public void readAccount(){ File file = new File("data/data/com.itydl.rwinrom/info.txt"); if(file.exists()){ try {// FileInputStream fis = new FileInputStream(file);// //把字节流转换成字符流// BufferedReader br = new BufferedReader(new// InputStreamReader(fis)); BufferedReader br = new BufferedReader(new FileReader(file));//读取txt文件里的用户名和密码String text = br.readLine();String[] s = text.split("##");//正则表达 et_name.setText(s[0]);//ctrl+1提取全局变量 et_pass.setText(s[1]);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} } } public void login(View v){ String name = et_name.getText().toString(); String pass = et_pass.getText().toString(); CheckBox cb = (CheckBox) findViewById(R.id.cb); //判断选框是否被勾选 if(cb.isChecked()){ //data/data/com.itheima.rwinrom:这就是内部存储空间的路径 File file = new File("data/data/com.itydl.rwinrom/info.txt");//这个路径是安卓特有的文件夹 FileOutputStream fos;try {fos = new FileOutputStream(file);//勾选了复选框,会把用户名密码存入内部存储位置fos.write((name + "##" + pass).getBytes());fos.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} } //创建并显示吐司对话框 Toast.makeText(this, "登录成功", 0).show(); } }
最后是截图:
当退出程序,再进入时,会发现用户名和密码都回显。
0 0
- Android简易实战教程--第七话《在内存中存储用户名和密码》
- Android简易实战教程--第十六话《SharedPreferences保存用户名和密码》
- Android开发中保存用户名和密码
- Android简易实战教程--第十五话《在外部存储中读写文件》
- float在内存中存储
- 变量和数据是如何存储在内存中
- 整型值和字符数组在内存中存储方式
- 字符数组和字符串指针在内存中存储
- 整型和浮点型在内存中存储方式
- java中数据在内存中的存储
- 细看代码在内存中如何存储
- java中数据在内存中的存储
- 列存储在内存中如何管理
- 将图片存储在内存中
- 整型数据在内存中如何存储?
- 数据在内存中存储方式学习
- 浮点数在内存中存储
- 浮点数在内存中存储
- 在类定义时指定泛型的上限
- NYOJ笨小熊问题 62题
- linux关于readlink函数获取运行路径的小程序
- The Skyline Problem
- 元数据(metadata)
- Android简易实战教程--第七话《在内存中存储用户名和密码》
- 文章标题
- 从指定范围获取指定个数的正序排列的不重复随机数
- 文章标题
- 关于红黑树和AVL树,以下哪种说法不正确?----腾讯2016研发工程师在线模拟笔试题
- 并查集的学习
- 无线网覆盖问题 NYOJ 199
- python并行(2):python多进程与多线程
- ZERO