实战演练--保存QQ账号与密码

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="#E6E6E6">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/head"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal">

    </ImageView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="账户:"
        android:textColor="@color/black"
        android:textSize="20dp" />

        <EditText
            android:id="@+id/account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:padding="10dp"
            android:background="@null"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码:"
            android:textColor="@color/black"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:padding="10dp"
            android:background="@null"/>

    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="登录"
        android:textSize="20dp"
        android:id="@+id/btn"/>

</LinearLayout>

 

2.工具类文件

FileSaveQQ.xml

package cn.itcast.saveqq;

import android.content.Context;

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

public class FileSaveQQ {

    public  static boolean info(Context context , String accout , String passwords) throws IOException {
        FileOutputStream os = null;
        try {
            os = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            os.write((accout + ":" +passwords).getBytes());
            return true;

        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        finally {
            os.close();
        }
    }



    public static Map<String,String> getinfo(Context context) throws IOException {
        String content= "";
        FileInputStream rs = null;
        try {
            rs = context.openFileInput("data.txt");
            byte[] buff = new byte[rs.available()];
            rs.read(buff);
            content = new String(buff);

            Map<String,String> s = new HashMap<String, String>();
            String[] infos = content.split(":");
            s.put("account",infos[0]);
            s.put("password",infos[1]);
            return s;


        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        finally {
            try {
                if (rs != null) {
                    rs.close();

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


}

}

3.MainAcitivity.java

package cn.itcast.saveqq;

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.io.IOException;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText account;
    private EditText password;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jianting();
        try {
            Map<String , String> s = FileSaveQQ.getinfo(this);
            if (s!= null){
                account.setText(s.get("account"));
                password.setText(s.get("password"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void jianting() {
        account = findViewById(R.id.account);
        password  = findViewById(R.id.password);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btn:
                String accout = account.getText().toString().trim();
                String passwords = password.getText().toString();

                if (TextUtils.isEmpty(accout)){
                    Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(passwords)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                try {
                    boolean isSaveSuccess = FileSaveQQ.info(this,accout,passwords);
                    if (isSaveSuccess){
                        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}

 

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喷喷炸洋芋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值