Android之模仿QQ保存账号密码

先写个布局文件activity_main.xml





    

    

    
    



    

        
        
    
    

然后创建一个FileSaveQQ的类,用来实现QQ的账号密码保存

package com.example.mrlee.login;

import android.content.Context;

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

public class FileSaveQQ {
    //把账号密码保存在data.txt文件中
    public static boolean saveUserInfo(Context context,String number,String password){
        try{
            FileOutputStream fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            fos.write((number + ":" + password).getBytes());
            fos.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    //从data.txt中去获取刚刚保存的账号密码
    public static Map getUserInfo(Context context) {
        String content = "";
        try {
            FileInputStream fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);//读取
            content = new String(buffer);
            Map userMap = new HashMap();
            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;
        }

    }
}

saveUserInfo()方法呢是将数据保存在data.txt文件中,而getUserInfo()方法则是将数据从data.txt中读取出来
接下来就编写主方法MainActivity.java

package com.example.mrlee.login;

        import android.content.Intent;
        import android.support.v7.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.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etNumber;
    private EditText etPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.btn_login);
        initView();
        Map userInfo = FileSaveQQ.getUserInfo(this);
        if (userInfo != null){
            etNumber.setText(userInfo.get("number"));
            etPassword.setText(userInfo.get("password"));
        }
    }

    private void initView() {
        etNumber = (EditText) findViewById(R.id.et_number);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
    }
    @Override
    public void onClick (View v){
        //单击事件,获取账号密码
        String number = etNumber.getText().toString().trim();
        String password = etPassword.getText().toString();
        //检查账号密码是否正确
        if (TextUtils.isEmpty(number)){
            Toast.makeText(this,"请输入账号",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 = FileSaveQQ.saveUserInfo(this,number,password);
        if (isSaveSuccess){
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
        }
    }
}

initView()是初始化控件,单击时调用FileSaveQQ类中的saveUserInfo()方法进行数据的保存
运行成功后,可以通过单击DDMS视图右上方的导出图标按钮将文件导出,就可以看淡刚刚保存的数据啦

你可能感兴趣的:(Android)