Android實(shí)現(xiàn)保存QQ賬號(hào)與密碼功能(文件存儲(chǔ))

寫在前面:今天用保存QQ賬號(hào)和密碼的實(shí)戰(zhàn)演練,帶大家掌握Android存儲(chǔ)中最基本的文件存儲(chǔ)方式
文件存儲(chǔ)是Android中最基本的一種數(shù)據(jù)存儲(chǔ)方式,它與Java中的文件存儲(chǔ)類似,都是通過I/O流形式把數(shù)據(jù)直接存儲(chǔ)到文件中,下面我們一起來看一下如何用Android實(shí)現(xiàn)文件存儲(chǔ)功能吧!
1.UI界面
1)垂直線性布局為整體框架
2)頭像獲取
3)子線性布局編輯框和密碼框
4)登錄button按鈕
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:src="@drawable/head" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="賬號(hào):"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="密碼:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:inputType="textPassword"
android:padding="10dp" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#3C8DC4"
android:text="登錄"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
2.構(gòu)建工具類
1)將數(shù)據(jù)存入文件
Android開發(fā)中,內(nèi)部存儲(chǔ)使用的是Context提供的openFileOutput()方法這個(gè)方法能夠返回進(jìn)行寫操作的FileOutputStream對(duì)象,示例如下:
FileOutputStream fos = openFileOutput(String name, int mode);
其中參數(shù)name表示文件名,mode表示文件的操作模式,也就是讀寫文件的方式。mode的取值有4種,具體如下:
MODE_PRIVATE:該文件只能被當(dāng)前程序讀寫MODE_APPEND:該文件的內(nèi)容可以追加MODE_WORLD_READABLE:該文件的內(nèi)容可以被其他程序讀MODE_WORLD_WRITEABLE:該文件的內(nèi)容可以被其他程序?qū)?/li>
存儲(chǔ)數(shù)據(jù)時(shí),使用FileOutputStream對(duì)象將數(shù)據(jù)存儲(chǔ)到文件中,創(chuàng)建了一個(gè)saveUserInfo()方法,用于將QQ賬號(hào)和密碼保存到data.txt文件中。
//保存QQ賬號(hào)和登錄密碼到data.txt文件中
public static boolean saveUserInfo(Context context, String account, String
password) {
FileOutputStream fos = null;
try {
//獲取文件的輸出流對(duì)象fos
fos = context.openFileOutput("data.txt",
Context.MODE_PRIVATE);
//將數(shù)據(jù)轉(zhuǎn)換為字節(jié)碼的形式寫入data.txt文件中
fos.write((account + ":" + password).getBytes());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
try {
if(fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2)從文件中讀取數(shù)據(jù)
使用Context提供的openFileOutput()方法這個(gè)方法能夠返回進(jìn)行寫操作的FileInputStream對(duì)象,示例如下:
FileInputStream fos = openFileInput(String name);
創(chuàng)建了一個(gè)getUserInfo()方法,用于從data.txt文件中獲取QQ賬號(hào)和密碼。
需要注意的是,這里的存儲(chǔ)和獲取都是需要用字節(jié)碼的形式,所以存取完再改為String類型。
//從data.txt文件中獲取存儲(chǔ)的QQ賬號(hào)和密碼
public static Map<String, String> getUserInfo(Context context) {
String content = "";
FileInputStream fis = null;
try {
//獲取文件的輸入流對(duì)象fis
fis = context.openFileInput("data.txt");
//將輸入流對(duì)象中的數(shù)據(jù)轉(zhuǎn)換為字節(jié)碼的形式
byte[] buffer = new byte[fis.available()];
fis.read(buffer);//通過read()方法讀取字節(jié)碼中的數(shù)據(jù)
content = new String(buffer); //將獲取的字節(jié)碼轉(zhuǎn)換為字符串
Map<String, String> userMap = new HashMap<String, String>();
String[] infos = content.split(":");//將字符串以“:”分隔后形成一個(gè)數(shù)組的形式
userMap.put("account", infos[0]); //將數(shù)組中的第一個(gè)數(shù)據(jù)放入userMap集合中
userMap.put("password", infos[1]); //將數(shù)組中的第二個(gè)數(shù)據(jù)放入userMap集合中
return userMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.編寫界面交互代碼
1)讀取文件
通過工具類FileSaveQQ中的getUserInfo()方法獲取QQ賬號(hào)和密碼信息
Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
if (userInfo != null) {
et_account.setText(userInfo.get("account")); //將獲取的賬號(hào)顯示到界面上
et_password.setText(userInfo.get("password")); //將獲取的密碼顯示到界面上
}2)按鈕監(jiān)聽事件
創(chuàng)建一個(gè)initView()方法,用于初始化界面控件。再對(duì)onClick()方法重寫,添加點(diǎn)擊登錄事件后的響應(yīng)。
private EditText et_account; //賬號(hào)輸入框
private EditText et_password; //密碼輸入框
private Button btn_login; //登錄按鈕
private void initView() {
et_account = findViewById(R.id.et_account);
et_password = findViewById(R.id.et_password);
btn_login = findViewById(R.id.btn_login);
//設(shè)置按鈕的點(diǎn)擊監(jiān)聽事件
btn_login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
//當(dāng)點(diǎn)擊登錄按鈕時(shí),獲取界面上輸入的QQ賬號(hào)和密碼
String account = et_account.getText().toString().trim();
String password = et_password.getText().toString();
//檢驗(yàn)輸入的賬號(hào)和密碼是否為空
if (TextUtils.isEmpty(account)) {
Toast.makeText(this, "請(qǐng)輸入QQ賬號(hào)", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "請(qǐng)輸入密碼", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登錄成功", Toast.LENGTH_SHORT).show();
break;
}
}
3)保存登錄信息
調(diào)用工具類FileSaveQQ中的saveUserInfo()方法將登錄信息保存到本地文件中。
boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, account,password);
if (isSaveSuccess) {
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "保存失敗", Toast.LENGTH_SHORT).show();
}4.運(yùn)行程序
在界面中輸入賬號(hào)和密碼,點(diǎn)擊“登錄”按鈕,會(huì)彈出“登錄成功”與”保存成功“的提示信息

5.查看文件所處位置
1)View——Tool Windows ——Device

2)右側(cè)的Device File Explorer ——data ——data ——項(xiàng)目包名——files

到此這篇關(guān)于Android保存QQ賬號(hào)與密碼的文章就介紹到這了,更多相關(guān)android qq賬號(hào)與密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android開發(fā)文件存儲(chǔ)實(shí)例
- Android 文件存儲(chǔ)與SharedPreferences存儲(chǔ)方式詳解用法
- 淺析Android文件存儲(chǔ)
- android I/0流操作文件(文件存儲(chǔ))
- 詳解Android 中的文件存儲(chǔ)
- Android存儲(chǔ)字符串?dāng)?shù)據(jù)到txt文件
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲(chǔ)卡的方法
- Android?文件存儲(chǔ)系統(tǒng)原理
相關(guān)文章
深入剖析Android中Service和Thread區(qū)別
下面小編就為大家?guī)硪黄钊肫饰鯝ndroid中Service和Thread區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android 實(shí)現(xiàn)永久性開啟adb 的root權(quán)限
這篇文章主要介紹了Android 實(shí)現(xiàn)永久性開啟adb 的root權(quán)限,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
使用android隱藏api實(shí)現(xiàn)亮度調(diào)節(jié)的方法
使用android隱藏api實(shí)現(xiàn)亮度調(diào)節(jié)的方法,需要的朋友可以參考一下2013-05-05
Android RecyclerView使用GridLayoutManager間距設(shè)置的方法
本篇文章主要介紹了Android RecyclerView使用GridLayoutManager間距設(shè)置的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android Studio新建工程默認(rèn)在build.gradle中加入maven阿里源的問題
這篇文章主要介紹了Android Studio新建工程默認(rèn)在build.gradle中加入maven阿里源的問題,本文通過實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android使用自定義View繪制漸隱漸現(xiàn)動(dòng)畫
這篇文章主要介紹了Android使用自定義View繪制漸隱漸現(xiàn)動(dòng)畫效果的相關(guān)內(nèi)容,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

