Android数据存储

高斋晓开卷,独共圣人语。这篇文章主要讲述Android数据存储相关的知识,希望能为你提供帮助。
layout.java

< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/background1"> < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:padding="30dp" android:orientation="vertical"> < TextView android:layout_width="wrap_content" android:layout_marginLeft="30dp" android:drawableLeft="@mipmap/ic_launcher_round" android:text="家庭记账本" android:textSize="40sp" android:layout_height="wrap_content"/> < EditText android:layout_width="match_parent" android:layout_marginTop="30dp" android:id="@+id/et_username" android:layout_height="wrap_content" android:hint="用户名" /> < EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_password" android:hint="密码" /> < Button android:layout_width="match_parent" android:text="登录" android:textSize="20sp" android:id="@+id/bt_login" android:layout_height="wrap_content"/> < RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="没有账号,立即去注册" android:textColor="#00ffff" android:textSize="16sp" /> < /RelativeLayout> < /LinearLayout> < /RelativeLayout>

MainActivity.java
package com.example.logindemo; import androidx.appcompat.app.AppCompatActivity; import android.nfc.Tag; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity {private static final String TAG ="MainActivity"; private TextView mUsername; private TextView mPassword; private ButtonmLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logindemo_layout); //第一步,找到控件 initViews(); //第二步,给我们的登录按钮设置监听事件 initListener(); }/** * 这个方法,我们用来找对应的控件 */ private void initViews(){ mUsername=(TextView)this.findViewById(R.id.et_username); mPassword=(TextView)this.findViewById(R.id.et_password); mLogin=(Button)this.findViewById(R.id.bt_login); } /** * 这个方法就是给登录按钮设置点击的监听 */ privatevoidinitListener(){ mLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG,"点击了登录按钮"); handlerLoginEvent(v); } }); }/** * 处理登录事件 * @param v */ private void handlerLoginEvent(View v) { //第三部,我们要拿到界面上的信息,包括账号和密码 //账号 String usernameText =mUsername.getText().toString(); //密码 String passwordText =mPassword.getText().toString(); //把账号和密码保存起来 saveUserInfo(usernameText,passwordText); } private void saveUserInfo(String usernameText,String passwordText){ Log.d(TAG,"保存用户信息"); File file =new File("info.txt"); try{ FileOutputStream fileOutputStream = new FileOutputStream(file); //以特定的格式存储 fileOutputStream.write((usernameText+"***"+passwordText).getBytes()); fileOutputStream.close(); }catch (Exception e){ e.printStackTrace(); } } }

【Android数据存储】 

    推荐阅读