日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 保存QQ密码(数据存储:文件存储、SharedPreferences)

發布時間:2024/3/24 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 保存QQ密码(数据存储:文件存储、SharedPreferences) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

源碼【工程文件】:https://gitee.com/lwx001/saveQQ

MainActivity.java:

package cn.lwx.saveqq;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 androidx.appcompat.app.AppCompatActivity;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private EditText etNumber;private EditText etPassword;private Button btnLogin;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//1、初始化界面initView();//2、如果用戶保存了信息,進行數據的回顯Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);//工具類if (userInfo != null) {etNumber.setText(userInfo.get("number"));etPassword.setText(userInfo.get("password"));}}private void initView() {//1、完成控件的初始化etNumber = (EditText) findViewById(R.id.et_number);etPassword = (EditText) findViewById(R.id.et_password);btnLogin = (Button) findViewById(R.id.btn_login);//2、設置按鈕的點擊事件btnLogin.setOnClickListener(this); //當前類 implements View.OnClickListener}@Overridepublic void onClick(View v) {//1、當單擊“登錄”按鈕時,獲取QQ賬號和密碼String number = etNumber.getText().toString().trim();String password = etPassword.getText().toString();//2、檢驗賬號和密碼是否正確(是否為空)if (TextUtils.isEmpty(number)) {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();//3、保存用戶信息boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, number, password);if (isSaveSuccess) {Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "保存失敗!", Toast.LENGTH_SHORT).show();}} }

FileSaveQQ.java:

package cn.lwx.saveqq;import android.content.Context;import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map;public class FileSaveQQ { //工具類、保存賬號、密碼信息//保存QQ賬號和登錄密碼到data.txt文件中//靜態方法,通過類可直接調用。Context上下文public static boolean saveUserInfo(Context context, String number, String password) {try {//1、通過上下文獲取文件輸出流FileOutputStream fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);//保存信息到文件中。獲取文件輸出流。文件名、模式//2、把數據寫到文件中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.available()]fis.read(buffer);//把數據讀到緩沖區中content = new String(buffer);Map<String, String> userMap = new HashMap<String, String>();String[] infos = content.split(":");userMap.put("number", infos[0]);userMap.put("password", infos[1]);fis.close();return userMap;} catch (Exception e) {e.printStackTrace();return null;}} }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"><ImageViewandroid:id="@+id/iv"android:layout_width="70dp"android:layout_height="70dp"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:background="@drawable/head" /><LinearLayoutandroid:id="@+id/ll_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/iv"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_marginTop="15dp"android:layout_marginRight="10dp"android:layout_marginBottom="5dp"android:background="#ffffff"><TextViewandroid:id="@+id/tv_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="賬號:"android:textColor="#000"android:textSize="20sp" /><EditTextandroid:id="@+id/et_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/ll_number"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="#ffffff"><TextViewandroid: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" /><EditTextandroid:id="@+id/et_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_toRightOf="@id/tv_password"android:background="@null"android:inputType="textPassword"android:padding="10dp" /></LinearLayout><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/ll_password"android:layout_marginLeft="10dp"android:layout_marginTop="50dp"android:layout_marginRight="10dp"android:background="#3C8DC4"android:text="登錄"android:textColor="#ffffff"android:textSize="20sp" /> </RelativeLayout>

運行截圖:

SharedPreferences :

SPSaveQQ.java :

package cn.lwx.saveqq;import android.content.Context; import android.content.SharedPreferences;import java.util.HashMap; import java.util.Map;public class SPSaveQQ {// 保存QQ賬號和登錄密碼到data.xml文件中public static boolean saveUserInfo(Context context, String number, String password) {//得到SharedPreferences實例 / Context上下文 / Context.MODE_PRIVATE私有模式SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);SharedPreferences.Editor edit = sp.edit();//得到編輯器edit.putString("userName", number);//存儲數據edit.putString("pwd", password);//存儲數據edit.commit();//提交return true;}//讀取操作 / 從data.xml文件中獲取存儲的QQ賬號和密碼public static Map<String, String> getUserInfo(Context context) {SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);String number = sp.getString("userName", null);//找不到數據,返回空String password = sp.getString("pwd", null);Map<String, String> userMap = new HashMap<String, String>();//存儲數據userMap.put("number", number);userMap.put("password", password);return userMap;}}

更改 MainActivity.java 文件 :

package cn.lwx.saveqq;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 androidx.appcompat.app.AppCompatActivity;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private EditText etNumber;private EditText etPassword;private Button btnLogin;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//1、初始化界面initView();//2、如果用戶保存了信息,進行數據的回顯//Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);//工具類Map<String, String> userInfo = SPSaveQQ.getUserInfo(this);if (userInfo != null) {etNumber.setText(userInfo.get("number"));etPassword.setText(userInfo.get("password"));}}private void initView() {//1、完成控件的初始化etNumber = (EditText) findViewById(R.id.et_number);etPassword = (EditText) findViewById(R.id.et_password);btnLogin = (Button) findViewById(R.id.btn_login);//2、設置按鈕的點擊事件btnLogin.setOnClickListener(this); //當前類 implements View.OnClickListener}@Overridepublic void onClick(View v) {//1、當單擊“登錄”按鈕時,獲取QQ賬號和密碼String number = etNumber.getText().toString().trim();String password = etPassword.getText().toString();//2、檢驗賬號和密碼是否正確(是否為空)if (TextUtils.isEmpty(number)) {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();//3、保存用戶信息//boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, number, password);boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this, number, password);if (isSaveSuccess) {Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "保存失敗!", Toast.LENGTH_SHORT).show();}} }

查看數據:


前情提要1 :靜態頁面-簡易QQ登錄頁面
(純activity_main.xml頁面)

【https://blog.csdn.net/weixin_44949135/article/details/104504728】

前情提要2 :動態頁面-簡單保存QQ密碼
(數據存儲:文件存儲、SharedPreferences)

【https://blog.csdn.net/weixin_44949135/article/details/104965413】




驗證QQ密碼 ,在前情提要2的基礎上,實現了如下功能:
【https://blog.csdn.net/weixin_44949135/article/details/106001065】
如果賬號存在,且密碼正確,在app頁面上,顯示 “ 賬號存在!密碼正確!登陸成功!” 的 文本提示;
如果賬號存在,但密碼不正確,在app頁面上,顯示 “ 賬號存在!密碼錯誤!登陸失敗!” 的 文本提示;
如果賬號不存在,且密碼不為空,則注冊賬號(保存用戶名與密碼)。


局限性 :

只能 存儲 一個 賬號的信息(賬號、密碼)。

如果在 “ 賬號不存在 and 密碼不為空 ” 的情況下,點擊 “ 登錄 ” 按鈕,

將自動注冊賬號,保存賬號信息(賬號、密碼),原賬號信息 將被 新賬號信息 替代。

轉發請附上原文鏈接。點個贊再走啊~ 謝謝~

總結

以上是生活随笔為你收集整理的Android 保存QQ密码(数据存储:文件存储、SharedPreferences)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。