網站首頁 編程語言 正文
本文實例為大家分享了Android保存QQ密碼功能的具體代碼,供大家參考,具體內容如下
技術要點:
使用文件儲存的方式保存數據
實現步驟:
①用戶交互界面的設計與實現
②工具類(FileSaveQQjava )的設計與實現
③界面邏輯代碼的設計與實現
頁面布局請看:Android開發實現簡單QQ登錄頁面
MainActivity.java代碼:
package com.example.saverqq;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
? ? private Button etLogin;
? ? private EditText etPassword;
? ? private EditText etNumber;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //初始化view
? ? ? ? initView();
? ? ? ? //如果用戶已經保存過就進行數據回顯
? ? ? ? Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
? ? ? ? if (userInfo!=null) {
? ? ? ? ? ? etNumber.setText(userInfo.get("number"));
? ? ? ? ? ? etPassword.setText(userInfo.get("password"));
? ? ? ? }
? ? }
? ? private void initView() {
//初始化控件
? ? ? ? etNumber = (EditText) findViewById(R.id.et_number);
? ? ? ? etPassword = (EditText) findViewById(R.id.et_password);
? ? ? ? etLogin = (Button) findViewById(R.id.btn_login);
? ? ? ? //設置按鈕點擊事件
? ? ? ? etLogin.setOnClickListener(this);
? ? }
? ? @Override
? ? public void onClick(View view) {
? ? ? ? //點擊按鈕獲取賬號密碼
? ? ? ? String number = etNumber.getText().toString().trim();
? ? ? ? String password = etPassword.getText().toString().trim();
? ? ? ? if (TextUtils.isEmpty(number)) {
? ? ? ? ? ? Toast.makeText(this, "請輸入QQ賬號", Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (TextUtils.isEmpty(password)) {
? ? ? ? ? ? Toast.makeText(this, "請輸入QQ密碼", Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? Toast.makeText(this, "登錄成功", Toast.LENGTH_LONG).show();
//保存用戶信息
? ? ? ? boolean isSaveSucess = FileSaveQQ.saveUserInfo(this, number, password);
? ? ? ? if (isSaveSucess) {
? ? ? ? ? ? Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();
? ? ? ? } else {
? ? ? ? ? ? Toast.makeText(this, "保存失敗", Toast.LENGTH_LONG).show();
? ? ? ? }
? ? }
}
FileSaveQQ.java文件代碼:
package com.example.saverqq;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class FileSaveQQ {
? ? //保存用戶信息
? ? public static boolean saveUserInfo(Context context, String number, String password) {
? ? ? ? try {
? ? ? ? ? ? //通過上下流獲取文件輸出流
? ? ? ? ? ? FileOutputStream fos = context.openFileOutput("data.txt", context.MODE_PRIVATE);
? ? ? ? ? ? //把數據寫到文件中
? ? ? ? ? ? fos.write((number + ":" + password).getBytes());
? ? ? ? ? ? fos.close();
? ? ? ? ? ? return true;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
? ? //從data.txt文件中讀取QQ賬號和密碼
? ? public static Map<String, String> getUserInfo(Context context) {
? ? ? ? String content = "";
? ? ? ? try {
? ? ? ? ? ? FileInputStream fis = context.openFileInput("data.txt");
? ? ? ? ? ? byte[] buffer = new byte[fis.available()];//設置緩沖區的大小
? ? ? ? ? ? fis.read(buffer);//讀到緩沖區
? ? ? ? ? ? Map<String, String> userMap = new HashMap<String, String>();
? ? ? ? ? ? content=new String(buffer);
? ? ? ? ? ? String[] infos = content.split(":");//以 :切割字符串
? ? ? ? ? ? userMap.put("number", infos[0]);
? ? ? ? ? ? userMap.put("password", infos[1]);
? ? ? ? ? ? fis.close();
? ? ? ? ? ? return userMap;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
}
參考圖書《Android移動開發基礎案例教程》
原文鏈接:https://blog.csdn.net/qq_50982405/article/details/113179932
相關推薦
- 2023-03-15 pandas計算相關系數corr返回空的問題解決_python
- 2022-09-09 PyCharm?設置數據庫,查詢數據庫語句方式_python
- 2022-08-03 python數據類型可變與不可變深入分析_python
- 2023-02-17 Python的OptionParser模塊示例教程_python
- 2022-10-08 Pandas數據分析-pandas數據框的多層索引_python
- 2022-12-22 Object?arrays?cannot?be?loaded?when?allow_pickle=F
- 2022-09-12 iOS開發TableView網絡請求及展示預加載實現示例_IOS
- 2023-03-15 Android?Studio格式化(Format)代碼快捷鍵介紹_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支