Android實(shí)戰(zhàn)教程第七篇之如何在內(nèi)存中存儲(chǔ)用戶名和密碼
本文實(shí)例為大家分享了Android內(nèi)存中存儲(chǔ)用戶名和密碼的方法,供大家參考,具體內(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>
活動(dòng)中的代碼如下:
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中讀取原因是,活動(dòng)一創(chuàng)建就讀取用戶名和密碼進(jìn)行回顯。 } 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("##");//正則表達(dá) 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)部存儲(chǔ)空間的路徑 File file = new File("data/data/com.itydl.rwinrom/info.txt");//這個(gè)路徑是安卓特有的文件夾 FileOutputStream fos; try { fos = new FileOutputStream(file); //勾選了復(fù)選框,會(huì)把用戶名密碼存入內(nèi)部存儲(chǔ)位置 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)退出程序,再進(jìn)入時(shí),會(huì)發(fā)現(xiàn)用戶名和密碼都回顯。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)3種側(cè)滑效果(仿qq側(cè)滑、抽屜側(cè)滑、普通側(cè)滑)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多種側(cè)滑效果,包括仿qq側(cè)滑,抽屜側(cè)滑,普通側(cè)滑三種效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04android AsynTask處理返回?cái)?shù)據(jù)和AsynTask使用get,post請求
本文主要介紹了android AsynTask處理返回?cái)?shù)據(jù)和AsynTask使用get,post請求方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01Android編程實(shí)現(xiàn)的首頁左右滑動(dòng)切換功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)的首頁左右滑動(dòng)切換功能,涉及Android事件監(jiān)聽及響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android輕量級(jí)存儲(chǔ)SharedPreferences?MMKV?Jetpack?DataStore方案
這篇文章主要為大家介紹了Android輕量級(jí)存儲(chǔ)SharedPreferences?MMKV?Jetpack?DataStore方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(一)
這篇文章主要介紹了Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(一) 的相關(guān)資料,需要的朋友可以參考下2016-01-01Android左滑返回功能的實(shí)現(xiàn)示例代碼
本篇文章主要介紹了Android左滑返回的實(shí)現(xiàn)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07android設(shè)置adb自帶screenrecord錄屏命令
這篇文章主要介紹了android設(shè)置adb自帶screenrecord錄屏命令,需要的朋友可以參考下2018-11-11基于Android 監(jiān)聽ContentProvider 中數(shù)據(jù)變化的相關(guān)介紹
本篇文章小編為大家介紹,基于Android 監(jiān)聽ContentProvider 中數(shù)據(jù)變化的相關(guān)介紹。需要的朋友參考下2013-04-04android中AutoCompleteTextView的簡單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡單用法(自動(dòng)提示),有需要的可以了解一下。2016-11-11