欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)保存QQ密碼功能

 更新時(shí)間:2022年04月24日 14:48:45   作者:Lssの老父親  
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)保存QQ密碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android保存QQ密碼功能的具體代碼,供大家參考,具體內(nèi)容如下

技術(shù)要點(diǎn):

使用文件儲(chǔ)存的方式保存數(shù)據(jù)

實(shí)現(xiàn)步驟:

①用戶交互界面的設(shè)計(jì)與實(shí)現(xiàn)
②工具類(FileSaveQQjava )的設(shè)計(jì)與實(shí)現(xiàn)
③界面邏輯代碼的設(shè)計(jì)與實(shí)現(xiàn)

頁面布局請(qǐng)看:Android開發(fā)實(shí)現(xiàn)簡(jiǎn)單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();
? ? ? ? //如果用戶已經(jīng)保存過就進(jìn)行數(shù)據(jù)回顯
? ? ? ? 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);
? ? ? ? //設(shè)置按鈕點(diǎn)擊事件
? ? ? ? etLogin.setOnClickListener(this);
? ? }
? ? @Override
? ? public void onClick(View view) {
? ? ? ? //點(diǎn)擊按鈕獲取賬號(hào)密碼
? ? ? ? String number = etNumber.getText().toString().trim();
? ? ? ? String password = etPassword.getText().toString().trim();
? ? ? ? if (TextUtils.isEmpty(number)) {
? ? ? ? ? ? Toast.makeText(this, "請(qǐng)輸入QQ賬號(hào)", Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (TextUtils.isEmpty(password)) {
? ? ? ? ? ? Toast.makeText(this, "請(qǐng)輸入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);
? ? ? ? ? ? //把數(shù)據(jù)寫到文件中
? ? ? ? ? ? fos.write((number + ":" + password).getBytes());
? ? ? ? ? ? fos.close();
? ? ? ? ? ? return true;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
? ? //從data.txt文件中讀取QQ賬號(hào)和密碼
? ? public static Map<String, String> getUserInfo(Context context) {
? ? ? ? String content = "";
? ? ? ? try {
? ? ? ? ? ? FileInputStream fis = context.openFileInput("data.txt");
? ? ? ? ? ? byte[] buffer = new byte[fis.available()];//設(shè)置緩沖區(qū)的大小
? ? ? ? ? ? fis.read(buffer);//讀到緩沖區(qū)
? ? ? ? ? ? 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移動(dòng)開發(fā)基礎(chǔ)案例教程》

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論