简单仿QQ登录界面,保存信息到sd卡

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

猜你喜欢

转载自blog.csdn.net/qq_38189195/article/details/82492696