Android开发数据持久化储存(1)

MainActivity

package com.example.a92133.qqlogindemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import static android.R.attr.handle;
import static android.R.attr.logo;

public class MainActivity extends AppCompatActivity {

    private EditText maccount;
    private EditText mpassword;
    private Button mlogin;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        initListener();
    }

    /**
     * 这个方法就是给我们的按钮设置点击的监听
     */
       private void initListener(){
           mlogin.setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v){
                   Log.d(TAG,"点击了登录按钮。。。");
                   handlerLoginEvent(v);
               }
           });
       }
    //处理登录事件
    private void handlerLoginEvent(View v){
        //第三步,我们要拿到界面上的内容,包括账号和密码
        //账号
        String accountText = maccount.getText().toString();
        String passwordText = mpassword.getText().toString();

        //把账号和密码保存起来
        saveUserInfo(accountText,passwordText);
    }

    private void saveUserInfo(String accountText,String passwordText){
        Log.d(TAG,"保存用户信息。。。");
        File file = new   File("/data/data/com.example.a92133.qqlogindemo/info.text");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            //以特定的格式存储
            //我们的账号***我们的密码
            fos.write((accountText+"***"+passwordText).getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 这个方法,用来找到对应的控件
     */
    private void initView(){
        maccount = (EditText) this.findViewById(R.id.et_account);
        mpassword = (EditText) this.findViewById(R.id.et_password);
        mlogin = (Button) this.findViewById(R.id.bt_login);

    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@mipmap/ic_launcher"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_marginTop="70dp"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:text="qq"
            android:textSize="40sp"
            android:drawableLeft="@mipmap/ic_launcher"
            android:layout_marginLeft="30dp"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:hint="QQ号码/手机号码/邮箱"
            android:layout_height="match_parent"
            android:layout_marginLeft="20sp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="20dp"/>
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:hint="密码"
            android:layout_marginLeft="20sp"
            android:layout_marginRight="20dp"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/bt_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:textSize="20sp"/>
        <RelativeLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="忘记密码"
                android:layout_marginLeft="20dp"
                android:textColor="#00aaff"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:text="用户注册"
                android:layout_alignParentRight="true"
                android:textColor="#00aaff" />
        </RelativeLayout>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:text="登录即代表阅读并同意阅读条款"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:textColor="#222222"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

感想

1 刷root要用软件kingroot
2 修改Android Device Monitor 中data等权限的操作,需要先给模拟机或手机刷root,连接模拟机或者手机,再打开命令提示符(cmd),后输入在这里插入图片描述

类似于这种就ok。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值