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?完整購物商城界面的實(shí)現(xiàn)案例
這篇文章為大家?guī)硪粋€(gè)Android?完整購物商城的界面具體的實(shí)現(xiàn),案例中含有商品列表的顯示,為商城最重要的功能之一,感興趣的朋友來看看吧2022-03-03Android編程開發(fā)之RadioGroup用法實(shí)例
這篇文章主要介紹了Android編程開發(fā)之RadioGroup用法,結(jié)合實(shí)例形式分析了Android中RadioGroup單選按鈕的具體使用技巧,需要的朋友可以參考下2015-12-12Android搜索框(SearchView)的功能和用法詳解
這篇文章主要為大家詳細(xì)介紹了Android搜索框SearchView的功能和用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(十)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第十篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android之ScrollView嵌套ListView和GridView沖突的解決方法
由于ListView,GridView本身都繼承于ScrollView,一旦在ScrollView中嵌套ScrollView,在ScrollView中嵌套使用ListView或者GridView,ListView只會(huì)顯示一行多一點(diǎn)。兩者進(jìn)行嵌套,即會(huì)發(fā)生沖突2013-09-09Android UI設(shè)計(jì)與開發(fā)之使用ViewPager實(shí)現(xiàn)歡迎引導(dǎo)頁面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開發(fā)之使用ViewPager實(shí)現(xiàn)歡迎引導(dǎo)頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android實(shí)踐之帶加載效果的下拉刷新上拉加載更多
這篇文章主要給大家介紹了關(guān)于Android實(shí)踐之下拉刷新上拉加載更多的相關(guān)資料,實(shí)現(xiàn)的效果在現(xiàn)在的很多項(xiàng)目中都能用到,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12