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

编程入门 行业动态 更新时间:2024-10-18 12:30:13

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/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。

更多推荐

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

本文发布于:2023-06-14 07:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持久   数据   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!