发布于 2017-10-06 02:35:03 | 164 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android如何实现在内存中存储用户名和密码的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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(); 
  } 
   
} 

最后是截图:


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

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务