Android实战教程第七篇之如何在内存中存储用户名和密码

所属分类: 软件编程 / Android 阅读数: 62
收藏 0 赞 0 分享

本文实例为大家分享了Android内存中存储用户名和密码的方法,供大家参考,具体内容如下

首先是配置文件:

<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 block 
        e.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 block 
        e.printStackTrace(); 
      } 
    } 
     
    //创建并显示吐司对话框 
    Toast.makeText(this, "登录成功", 0).show(); 
  } 
   
} 

最后是截图:


当退出程序,再进入时,会发现用户名和密码都回显。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多