Android實現(xiàn)保存QQ賬號與密碼功能(文件存儲)
寫在前面:今天用保存QQ賬號和密碼的實戰(zhàn)演練,帶大家掌握Android存儲中最基本的文件存儲方式
文件存儲是Android中最基本的一種數(shù)據(jù)存儲方式,它與Java中的文件存儲類似,都是通過I/O流形式把數(shù)據(jù)直接存儲到文件中,下面我們一起來看一下如何用Android實現(xiàn)文件存儲功能吧!
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="賬號:" 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)部存儲使用的是Context提供的openFileOutput()
方法這個方法能夠返回進行寫操作的FileOutputStream對象,示例如下:
FileOutputStream fos = openFileOutput(String name, int mode);
其中參數(shù)name表示文件名,mode表示文件的操作模式,也就是讀寫文件的方式。mode的取值有4種,具體如下:
MODE_PRIVATE
:該文件只能被當前程序讀寫MODE_APPEND
:該文件的內(nèi)容可以追加MODE_WORLD_READABLE
:該文件的內(nèi)容可以被其他程序讀MODE_WORLD_WRITEABLE
:該文件的內(nèi)容可以被其他程序?qū)?/li>
存儲數(shù)據(jù)時,使用FileOutputStream對象將數(shù)據(jù)存儲到文件中,創(chuàng)建了一個saveUserInfo()
方法,用于將QQ賬號和密碼保存到data.txt文件中。
//保存QQ賬號和登錄密碼到data.txt文件中 public static boolean saveUserInfo(Context context, String account, String password) { FileOutputStream fos = null; try { //獲取文件的輸出流對象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()
方法這個方法能夠返回進行寫操作的FileInputStream對象,示例如下:
FileInputStream fos = openFileInput(String name);
創(chuàng)建了一個getUserInfo()
方法,用于從data.txt文件中獲取QQ賬號和密碼。
需要注意的是,這里的存儲和獲取都是需要用字節(jié)碼的形式,所以存取完再改為String類型。
//從data.txt文件中獲取存儲的QQ賬號和密碼 public static Map<String, String> getUserInfo(Context context) { String content = ""; FileInputStream fis = null; try { //獲取文件的輸入流對象fis fis = context.openFileInput("data.txt"); //將輸入流對象中的數(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(":");//將字符串以“:”分隔后形成一個數(shù)組的形式 userMap.put("account", infos[0]); //將數(shù)組中的第一個數(shù)據(jù)放入userMap集合中 userMap.put("password", infos[1]); //將數(shù)組中的第二個數(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賬號和密碼信息
Map<String, String> userInfo = FileSaveQQ.getUserInfo(this); if (userInfo != null) { et_account.setText(userInfo.get("account")); //將獲取的賬號顯示到界面上 et_password.setText(userInfo.get("password")); //將獲取的密碼顯示到界面上 }
2)按鈕監(jiān)聽事件
創(chuàng)建一個initView()
方法,用于初始化界面控件。再對onClick()
方法重寫,添加點擊登錄事件后的響應。
private EditText et_account; //賬號輸入框 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); //設置按鈕的點擊監(jiān)聽事件 btn_login.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_login: //當點擊登錄按鈕時,獲取界面上輸入的QQ賬號和密碼 String account = et_account.getText().toString().trim(); String password = et_password.getText().toString(); //檢驗輸入的賬號和密碼是否為空 if (TextUtils.isEmpty(account)) { Toast.makeText(this, "請輸入QQ賬號", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(this, "請輸入密碼", 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.運行程序
在界面中輸入賬號和密碼,點擊“登錄”按鈕,會彈出“登錄成功”與”保存成功“的提示信息
5.查看文件所處位置
1)View——Tool Windows ——Device
2)右側(cè)的Device File Explorer ——data ——data ——項目包名——files
到此這篇關于Android保存QQ賬號與密碼的文章就介紹到這了,更多相關android qq賬號與密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android編程開發(fā)之RadioGroup用法實例
這篇文章主要介紹了Android編程開發(fā)之RadioGroup用法,結(jié)合實例形式分析了Android中RadioGroup單選按鈕的具體使用技巧,需要的朋友可以參考下2015-12-12Android搜索框(SearchView)的功能和用法詳解
這篇文章主要為大家詳細介紹了Android搜索框SearchView的功能和用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Android之ScrollView嵌套ListView和GridView沖突的解決方法
由于ListView,GridView本身都繼承于ScrollView,一旦在ScrollView中嵌套ScrollView,在ScrollView中嵌套使用ListView或者GridView,ListView只會顯示一行多一點。兩者進行嵌套,即會發(fā)生沖突2013-09-09Android UI設計與開發(fā)之使用ViewPager實現(xiàn)歡迎引導頁面
這篇文章主要為大家詳細介紹了Android UI設計與開發(fā)之使用ViewPager實現(xiàn)歡迎引導頁面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08