Android存储方式之内部存储

登录案例

  • 设置UI布局
  • 获取控件ID
  • 保存数据
    • 设置点击事件
    • 通过上下文获取系统私有路径
    • 将数据写入到文件(write)
  • 读取数据
    • 通过上下文获取文件的路径
    • 解析文本内容

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ml.no1.MainActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入账号"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入密码"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/cb_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码" />

    <Button
        android:id="@+id/bt_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆" />
</LinearLayout>

Java代码

package ml.no1;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_pass;
    private CheckBox cb_data;
    private Button bt_login;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取资源ID
        setContentView(R.layout.activity_main);
        mContext = this;
        et_name = (EditText) findViewById(R.id.et_name);
        et_pass = (EditText) findViewById(R.id.et_pass);
        cb_data = (CheckBox) findViewById(R.id.cb_data);
        bt_login = (Button) findViewById(R.id.bt_login);
        //设置按钮的点击事件
        bt_login.setOnClickListener(this);

        //回显帐号密码
        HashMap<String, String> hashMap = (HashMap<String, String>) DataInfo.getDataInfo(mContext);
        if (hashMap != null) {
            //获取map中的数据
            String username = hashMap.get("username");
            String password = hashMap.get("password");
            et_name.setText(username);
            et_pass.setText(password);
            cb_data.setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                login();
        }
    }

    private void login() {
        //获取用户输入的数据
        String username = et_name.getText().toString();
        String password = et_pass.getText().toString();
        boolean checked = cb_data.isChecked();
        //判断用户名密码不能为空
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "帐号密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //判断用户是否记住密码,如果记住,就保存数据
        if (checked) {

            boolean info = DataInfo.setDataInfo(mContext, username, password);
            if (info) {
                Toast.makeText(mContext, "数据保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "数据保存失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Java工具类

package ml.no1;

import android.content.Context;

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

/**
 * Created by Administrator on 2012/1/1.
 */
public class DataInfo {
    /**
     * 存储数据
     *
     * @param mContext 上下文
     * @param username 帐号
     * @param password 密码
     * @return
     */
    public static boolean setDataInfo(Context mContext, String username, String password) {
        //封装用户输入的数据
        String info = username + "##" + password;
        try {
            //获取私有目录,开启文件写入流:参1:写入到本地的文件名称,参2:文件操作模式:私有,追加,读写等.
            FileOutputStream stream = mContext.openFileOutput("info.txt", Context.MODE_PRIVATE);
            //将文件写入到本地
            stream.write(info.getBytes());
            stream.close();//关闭流
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 读取文件
     *
     * @param context
     * @return
     */
    public static Map<String, String> getDataInfo(Context context) {
        try {
            //通过context对象获取私有目录
            FileInputStream stream = context.openFileInput("info.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            //读取数据,解析数据
            String line = reader.readLine();
            String[] split = line.split("##");
            //以键值对关系保存数据到集合
            HashMap hashMap = new HashMap<>();
            hashMap.put("username", split[0]);
            hashMap.put("password", split[1]);
            //关闭流
            stream.close();
            reader.close();
            return hashMap;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值