簡單仿QQ登入介面,儲存資訊到sd卡

白浩傑發表於2018-09-07

1.java類

MainActivity
package com.example.bhj.qqlogin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etNumber;
    private EditText etPassword;
    private CheckBox cbRememberPwd;
    private CheckBox cbAutoLogin;
    private Button btnLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);//不顯示標題,必須寫在setContentView前面
        setContentView(R.layout.activity_main);

        //  /data/data/包名/files
        this.getFilesDir();

        etNumber = findViewById(R.id.et_number);
        etPassword = findViewById(R.id.et_password);
        cbRememberPwd = findViewById(R.id.cb_remember_pwd);
        cbAutoLogin = findViewById(R.id.cb_auto_login);
        btnLogin = findViewById(R.id.btn_login);

        btnLogin.setOnClickListener(this);

        //回顯示資料
        Map<String, String> userInfoMap = UtilsOfSDCard.getUserInfo(this);

        if (userInfoMap != null) {
            etNumber.setText(userInfoMap.get("number"));
            etPassword.setText(userInfoMap.get("password"));
        }


    }

    @Override
    public void onClick(View v) {//執行登入的操作
        //1.取出賬號和密碼
        String number = etNumber.getText().toString();
        String password = etPassword.getText().toString();

        if (TextUtils.isEmpty(number) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "請正確輸入", Toast.LENGTH_SHORT).show();
            return;
        }

        //2.判斷是否記住密碼,並存起來
        if (cbRememberPwd.isChecked()) {

            Log.d("bai", "記住密碼");

            boolean isSuccess = UtilsOfSDCard.saveUserInfo(this, number, password);

            if (isSuccess) {
                Log.d("bai", "儲存成功");
                Toast.makeText(this, "儲存成功", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "儲存失敗", Toast.LENGTH_SHORT).show();
            }

        }

        // 3.登入
        Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT).show();

    }
}

Utils

package com.example.bhj.qqlogin;

import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by bhj on 2018/9/6.
 */


public class Utils {


    /**
     * 儲存使用者資訊
     *
     * @param number   qq號
     * @param password 密碼
     * @return true 成功
     */
    public static boolean saveUserInfo(String number, String password) {

        try {
            String path = "/data/data/com.example.bhj.myapplication/bhjqq.txt";

            FileOutputStream fos = new FileOutputStream(path);

            //3966554#12154684
            String data = number + "##" + password;
            fos.write(data.getBytes());
            fos.flush();
            fos.close();

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }


    /**
     * 儲存使用者資訊到本地
     *
     * @param number   qq號
     * @param password 密碼
     * @return true 成功
     */
    public static boolean saveUserInfo(Context context, String number, String password) {

        try {
            //String path = "/data/data/com.example.bhj.myapplication/bhjqq.txt"

            File filesDir = context.getFilesDir();

            File f = new File(filesDir, "bhjqq.txt");

            FileOutputStream fos = new FileOutputStream(f);

            //3966554#12154684
            String data = number + "##" + password;
            fos.write(data.getBytes());
            fos.flush();
            fos.close();

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }


    public static Map<String, String> getUserInfo() {

        try {
            String path = "/data/data/com.example.bhj.myapplication/bhjqq.txt";

            FileInputStream fis = new FileInputStream(path);
            //字元流物件
            BufferedReader buf = new BufferedReader(new InputStreamReader(fis));

            String text = buf.readLine();

            if (!TextUtils.isEmpty(text)) {
                String[] split = text.split("##");

                Map<String, String> userInfoMap = new HashMap<String, String>();

                userInfoMap.put("number", split[0]);
                userInfoMap.put("password", split[1]);
                return userInfoMap;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public static Map<String, String> getUserInfo(Context context) {

        try {
            //String path = "/data/data/com.example.bhj.myapplication/bhjqq.txt";

            File filesDir = context.getFilesDir();

            File f = new File(filesDir, "bhjqq.txt");

            FileInputStream fis = new FileInputStream(f);
            //字元流物件
            BufferedReader buf = new BufferedReader(new InputStreamReader(fis));

            String text = buf.readLine();

            if (!TextUtils.isEmpty(text)) {
                String[] split = text.split("##");

                Map<String, String> userInfoMap = new HashMap<String, String>();

                userInfoMap.put("number", split[0]);
                userInfoMap.put("password", split[1]);
                return userInfoMap;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}

UtilsOfSDCard

package com.example.bhj.qqlogin;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by bhj on 2018/9/8.
 */

public class UtilsOfSDCard {


    /**
     * 儲存使用者資訊到SD卡
     *
     * @param number   qq號
     * @param password 密碼
     * @return true 成功
     */
    public static boolean saveUserInfo(Context context, String number, String password) {

        try {

            //判斷有沒sd卡
            String state = Environment.getExternalStorageState();

            if (!Environment.MEDIA_MOUNTED.equals(state)){
                //沒有sd卡
                return false;
            }

            File sdCardFile = Environment.getExternalStorageDirectory();

            File f = new File(sdCardFile, "bhjqq.txt");

            FileOutputStream fos = new FileOutputStream(f);

            //3966554#12154684
            String data = number + "##" + password;
            fos.write(data.getBytes());
            fos.flush();
            fos.close();

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }


    /**
     * 到sd卡獲取使用者資訊
     * @param context
     * @return
     */
    public static Map<String, String> getUserInfo(Context context) {

        try {

            //判斷有沒sd卡
            String state = Environment.getExternalStorageState();

            if (!Environment.MEDIA_MOUNTED.equals(state)){
                //沒有sd卡
                return null;
            }

            File sdCardFile = Environment.getExternalStorageDirectory();

            File f = new File(sdCardFile, "bhjqq.txt");

            FileInputStream fis = new FileInputStream(f);
            //字元流物件
            BufferedReader buf = new BufferedReader(new InputStreamReader(fis));

            String text = buf.readLine();

            buf.close();

            if (!TextUtils.isEmpty(text)) {
                String[] split = text.split("##");

                Map<String, String> userInfoMap = new HashMap<String, String>();

                userInfoMap.put("number", split[0]);
                userInfoMap.put("password", split[1]);
                return userInfoMap;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}

layout檔案 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="@string/qq_number"  />
    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="請輸入密碼"  />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <CheckBox
            android:id="@+id/cb_remember_pwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="記住密碼"/>
        <CheckBox
            android:id="@+id/cb_auto_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/cb_remember_pwd"
            android:text="自動登入"/>

    </RelativeLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/shape_drawable_2"
        android:text="登       錄"/>

</LinearLayout>

drawable檔案  shape_drawable_2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 設定透明背景色 -->
    <solid android:color="#87CEEB"/>

    <!-- 設定一個黑色邊框 -->
    <stroke android:color="#000000"
            android:width="2px"/>

    <!-- 設定四個圓角的半徑 -->
    <corners android:radius="100px"/>

    <!-- 設定一下邊距,讓空間大一點 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

原始碼下載地址:

https://github.com/bai390426262/QQLogIn.git

 

相關文章