Simple steps, use Android studio to save QQ account password, and simple QQ user login interface

Use the Android studio 2021 software to output a simple user login interface, which has an avatar and can save the account password entered by yourself. The file name is saveqq. The function realizes the input and storage of the account number and password, and the simple QQ interface layout.


foreword

This article will directly introduce how to realize the function of saving the account number and password when logging in to QQ, which is the main purpose. Based on the code I wrote and run, keep it simple, so that readers have a good viewing and operating experience. Configuration: Android studio 2021.1.1.21 windows

Finally achieve the effect:


1. Specific steps

1. Create a program

Open Android studio, create a new application in Android studio, and name it SaveQQ.

2. Realize interface layout

Write the interface layout under acyivity_main.xml under the layout package on the left side of the interface.

 The interface layout code is as follows (example):

<?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="140dp"
        android:layout_height="140dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:src="@drawable/handsome" />
    <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>

 3. Import pictures

Import the picture that needs to be used as the avatar into the drawable folder created in the program. Here the picture is named handsome. When using your own picture, you should pay attention to modify the picture name under the interface layout of acyivity_main.xml.

4.1. Create tool class

First create a tool class FileSaveQQ under com.example.saveqq under the java package to realize the function of storing and reading QQ account numbers and passwords.

 

FileSaveQQ code is as follows:

package com.example.saveqq;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileSaveQQ {

    public static boolean saveUserInfo(Context context, String account, String
            password) {
        FileOutputStream fos = null;
        try {

            fos = context.openFileOutput("data.txt",
                    Context.MODE_PRIVATE);

            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();
            }
        }
    }

    public static Map<String, String> getUserInfo(Context context) {
        String content = "";
        FileInputStream fis = null;
        try {

            fis = context.openFileInput("data.txt");

            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            content = new String(buffer);
            Map<String, String> userMap = new HashMap<String, String>();
            String[] infos = content.split(":");
            userMap.put("account", infos[0]);
            userMap.put("password", infos[1]);
            return userMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


 4.2. Create a tool class (based on using SharedPreferences storage method)

Also create a class named SPSaveQQ under com.example.saveqq under the java package, which also implements the storage function, but using the SharedPreferences storage method will be more simple and convenient. Therefore, it is more recommended to store data in the SharedPreferences storage method.

The SPSaveQQ code is as follows (example):

package com.example.saveqq;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

public class SPSaveQQ{

    public static boolean saveUserInfo(Context context, String account,
                                       String password) {
        SharedPreferences sp = context.getSharedPreferences("data",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("userName", account);
        edit.putString("pwd", password);
        edit.commit();
        return true;
    }

    public static Map<String, String> getUserInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences("data",
                Context.MODE_PRIVATE);
        String account = sp.getString("userName", null);
        String password = sp.getString("pwd", null);
        Map<String, String> userMap = new HashMap<String, String>();
        userMap.put("account", account);
        userMap.put("password", password);
        return userMap;
    }
}

5. Write logic code

Also create a class named MainActivity under com.example.saveqq under the java package, which is used to write logic codes and realize the interactive functions on the interface.

The MainActivity code is as follows (example):

package com.example.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 et_account;
    private EditText et_password;
    private Button btn_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        Map<String, String> userInfo = SPSaveQQ.getUserInfo(this);
        if (userInfo != null) {
            et_account.setText(userInfo.get("account"));
            et_password.setText(userInfo.get("password"));
        }
    }
    private void initView() {
        et_account =  findViewById(R.id.et_account);
        et_password =  findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);

        btn_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:

                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();

                boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this, account, password);
                if (isSaveSuccess) {
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}

6. Run the program

 After clicking Create device, select Nexus 4, and after next, select Oreo 27 or Oreo26, and finally click Run after next. (It has already been built and can be skipped)

After the program runs successfully, you can enter the account number and password in the interface. After clicking the "Login" button, "Successful login" and "Successful saving" will pop up, and the account number and password will still remain in the current input box after the program is reopened. , indicating that it has been stored successfully. The running results are as follows: You can see that the account I entered is: 123, and the password is: 123. After successfully saving, it still stays in the input box, and the password content is invisible.

7. Verify the result

After running successfully, you can find the data.xml file under the shared_prefs directory of the program in the Device File Explorer view (in the lower right corner of the Android studio interface), double-click to open it, and you can see that it has been stored successfully. Specific path (based on myself): Device File Explorer  -> data -> data -> com.example.saveqq -> shared_prefs -> data.xml (patience)

Under data.xml, we can see the previously entered account number: 123, password: 123, which have been stored successfully.


Staying up late to make, writing is not easy! ! !

Attention is the greatest support! ! !

If there are mistakes or omissions, pointers and suggestions are welcome!

It will be updated continuously.

Guess you like

Origin blog.csdn.net/qq_56017400/article/details/123899624