十一,十二周作业(数据存储,QQ实现记住密码)

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hp.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入你想写入的内容"/> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入"/> <EditText android:id="@+id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="显示读取的内容" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取"/> </LinearLayout>
逻辑代码
package com.example.hp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.EditText; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.util.logging.Handler; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt1).setOnClickListener(this); findViewById(R.id.bt2).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.bt1: String fileName="data.txt"; String content=((EditText)findViewById(R.id.et1)).getText().toString(); FileOutputStream fos=null; try{ fos=openFileOutput(fileName,MODE_PRIVATE); fos.write(content.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } break; case R.id.bt2: content = ""; FileInputStream fis=null; try{ fis=openFileInput("data.txt"); byte[] buffer=new byte[fis.available()]; fis.read(buffer); content=new String(buffer); ((EditText)findViewById(R.id.et2)).setText(content); } catch (Exception e) { e.printStackTrace(); } finally { try{ if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } break; } } }

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.saveqq.MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:src="@drawable/head"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="18sp"
android:textColor="@android:color/black"
android:padding="10dp"/>
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black"
android:hint="请输入用户名"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="18sp"
android:textColor="@android:color/black"
android:padding="10dp"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black"
android:inputType="textPassword"
android:hint="请输入密码"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_margin="10dp">
<CheckBox
android:id="@+id/cb1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
<CheckBox
android
