数据存储
实例:实现一个登录界面
要求:
1.用户名
2.密码
3.记住密码
4.登录
5.方法一:把用户,密码,选择存储起来,读出来
6.方法二:用文件把用户,密码,选择存储起来,读出来
activity_main.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"
android:background="@drawable/b"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="90dp"
app:srcCompat="@drawable/a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="用户名:"
android:textColor="#000000" />
<EditText
android:id="@+id/edt_UserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="密 码:"
android:textColor="#000000" />
<EditText
android:id="@+id/edt_PassWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:textColor="#000000" />
</LinearLayout>
<CheckBox
android:id="@+id/cek_Save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="记住密码" />
<Button
android:id="@+id/btn_Save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#5267E6"
android:text="登录(保存)SharedPreferences" />
<Button
android:id="@+id/btnSaveTxtFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#5267E6"
android:text="登录(保存)TXTFIle" />
<Button
android:id="@+id/btn_ReadTxtFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#5267E6"
android:text="读取TXTFIle" />
</LinearLayout>
MainActivity.java
package com.example.mysixapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
//1.定义变量
private EditText edtUserName, edtPassWord;
private CheckBox chkSave;
private Button btnSave,btnSaveTxtFile,btnReadTxtFile;
private Boolean checked; //定义布尔变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2.初始化变量
edtUserName = findViewById(R.id.edt_UserName);
edtPassWord = findViewById(R.id.edt_PassWord);
chkSave = findViewById(R.id.cek_Save);
btnSave = findViewById(R.id.btn_Save);
checked = false;
btnSaveTxtFile = findViewById(R.id.btnSaveTxtFile);
btnReadTxtFile = findViewById(R.id.btn_ReadTxtFile);
chkSave.setOnCheckedChangeListener(this);
btnSave.setOnClickListener(this);
btnSaveTxtFile.setOnClickListener(this);
btnReadTxtFile.setOnClickListener(this);
readSharedPreferences();
}
public void readSharedPreferences()
{
SharedPreferences sp = getSharedPreferences("mydata",MODE_PRIVATE);
Boolean b=sp.getBoolean("checked",false);
if(b)
{
edtUserName.setText(sp.getString("username",null));
edtPassWord.setText(sp.getString("password",null));
chkSave.setChecked(true);
}
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_Save)
{
//edtUserName.getText().toString()+edtPassWord.getText().toString()+checked
SharedPreferences sp =getSharedPreferences("mydata",MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putString("username",edtUserName.getText().toString());
ed.putString("password",edtPassWord.getText().toString());
ed.putBoolean("checked", checked);
ed.commit(); //提交
Toast.makeText(this,"保存成功!",Toast.LENGTH_LONG).show();
}
//实现方法
if(v.getId()==R.id.btnSaveTxtFile)
{
try { //抛出异常!!!
FileOutputStream fos = openFileOutput("myfile.txt",MODE_PRIVATE);
String str = edtUserName.getText().toString()+":"+edtPassWord.getText().toString()+":"+checked;
fos.write(str.getBytes());
fos.close();
Toast.makeText(this,"保存TXT成功!",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(v.getId() == R.id.btn_ReadTxtFile)
{
try {
FileInputStream fis = openFileInput("myfile.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String str =new String(buffer);
fis.close();
Toast.makeText(this,str,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(chkSave.isChecked())
{
checked =true;
}
else
{
checked = false;
}
}
}