日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android開發保存QQ密碼功能_Android

作者:Lssの老父親 ? 更新時間: 2022-06-22 編程語言

本文實例為大家分享了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

欄目分類
最近更新