Android實戰(zhàn)教程第七篇之如何在內(nèi)存中存儲用戶名和密碼
本文實例為大家分享了Android內(nèi)存中存儲用戶名和密碼的方法,供大家參考,具體內(nèi)容如下
首先是配置文件:
<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中讀取原因是,活動一創(chuàng)建就讀取用戶名和密碼進行回顯。
}
public void readAccount(){
File file = new File("data/data/com.itydl.rwinrom/info.txt");
if(file.exists()){
try {
// FileInputStream fis = new FileInputStream(file);
// //把字節(jié)流轉(zhuǎn)換成字符流
// 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:這就是內(nèi)部存儲空間的路徑
File file = new File("data/data/com.itydl.rwinrom/info.txt");//這個路徑是安卓特有的文件夾
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
//勾選了復(fù)選框,會把用戶名密碼存入內(nèi)部存儲位置
fos.write((name + "##" + pass).getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//創(chuàng)建并顯示吐司對話框
Toast.makeText(this, "登錄成功", 0).show();
}
}
最后是截圖:

當(dāng)退出程序,再進入時,會發(fā)現(xiàn)用戶名和密碼都回顯。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中實現(xiàn)Runnable接口簡單例子
這篇文章主要介紹了Android中實現(xiàn)Runnable接口簡單例子,著重點在如何實現(xiàn)run()方法,需要的朋友可以參考下2014-06-06
Android開發(fā)中應(yīng)用程序分享功能實例
這篇文章主要介紹了Android開發(fā)中應(yīng)用程序分享功能,結(jié)合實例形式分析了基于Intent實現(xiàn)Android程序分享功能的技巧,需要的朋友可以參考下2016-02-02
Android編程實現(xiàn)修改標(biāo)題欄位置使其居中的方法
這篇文章主要介紹了Android編程實現(xiàn)修改標(biāo)題欄位置使其居中的方法,涉及Android布局設(shè)置的簡單實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Material Design系列之自定義Behavior支持所有View
這篇文章主要為大家詳細介紹了Material Design系列之自定義Behavior支持所有View,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
詳解Android開發(fā)之MP4文件轉(zhuǎn)GIF文件
這篇文章介紹的是將錄下來的視頻選取一小段轉(zhuǎn)為 GIF 文件,不僅時間段可以手動選取,而且還需要支持截取視頻的局部區(qū)域轉(zhuǎn)為 GIF,網(wǎng)上調(diào)研了一下技術(shù)方案,覺得還是有必要把實現(xiàn)過程拿出來分享下,有需要的可以直接拿過去用。下面來一起看看。2016-08-08
Android實現(xiàn)Activity界面切換添加動畫特效的方法
這篇文章主要介紹了Android實現(xiàn)Activity界面切換添加動畫特效的方法,非常實用的技巧,需要的朋友可以參考下2014-08-08

