布局文件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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:gravity="center"/>
<Button
android:id="@+id/btn_native_r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取本地文件"/>
<Button
android:id="@+id/btn_native_w"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入本地文件"/>
</LinearLayout>
MainActivity.java
package com.example.ch07filedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity {
private EditText edtMsg;
private Button btnNativeR;
private Button btnNativeW;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
showUserInfo();
setListener();
}
private void setListener() {
Mylistener mylistener = new Mylistener();
btnNativeR.setOnClickListener(mylistener);
btnNativeW.setOnClickListener(mylistener);
}
private void showUserInfo() {
SharedPreferences userSp = getSharedPreferences(
"UserInfo",
MODE_PRIVATE
);
String name = userSp.getString("name", "无名");
String pwd = userSp.getString("pwd", "******");
edtMsg.setText(name + " :" + pwd);
}
private void findViews() {
edtMsg = findViewById(R.id.edt_msg);
btnNativeR = findViewById(R.id.btn_native_r);
btnNativeW = findViewById(R.id.btn_native_w);
}
class Mylistener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_native_r:
showNativeTxtMsg();
break;
case R.id.btn_native_w:
appenNateiveTxtMsg();
break;
}
}
}
private void appenNateiveTxtMsg() {
try {
String path = getFilesDir().getAbsolutePath();
String file = path + "/test.txt";
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file, true), "GB2312"
)
);
String str = edtMsg.getText().toString().trim();
writer.write(str);
writer.flush();
writer.close();
Toast.makeText(getApplicationContext(),
"数据追加完成",
Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showNativeTxtMsg() {
try {
String path = getFilesDir().getAbsolutePath();
Log.i("lww", "本地路径:" + path);
String file = path + "/test.txt";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(file), "GB2312"
)
);
String str = null;
StringBuffer buffer = new StringBuffer();
while (null != (str = reader.readLine())) {
buffer.append(str);
buffer.append("\n");
}
reader.close();
Toast.makeText(getApplicationContext(),
"文件读取完成",
Toast.LENGTH_SHORT).show();
edtMsg.setText(buffer.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
布局文件 activity_login.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:orientation="vertical"
android:layout_margin="15dp"
android:gravity="center"
tools:context=".LoginActivity">
<EditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/edt_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="登录"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp">
<TextView
android:id="@+id/tv_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="自动登录"/>
<CheckBox
android:id="@+id/chk_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/tv_auto"
android:layout_alignBaseline="@id/tv_auto"/>
<TextView
android:id="@+id/tv_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/chk_auto"
android:text="保存密码"/>
<CheckBox
android:id="@+id/chk_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/tv_save"
android:layout_alignBaseline="@id/tv_save"/>
</RelativeLayout>
</LinearLayout>
LoginActivity.java
package com.example.ch07filedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
private Button btnLogin;
private EditText edtName;
private EditText edtPwd;
private TextView tvAuto;
private TextView tvSave;
private CheckBox chkAuto;
private CheckBox chkSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViews();
setListener();
}
private void setListener() {
Mylistener mylistener = new Mylistener();
btnLogin.setOnClickListener(mylistener);
chkAuto.setOnClickListener(mylistener);
}
private void findViews() {
btnLogin = findViewById(R.id.btn_login);
edtName = findViewById(R.id.edt_name);
edtPwd = findViewById(R.id.edt_pwd);
tvAuto = findViewById(R.id.tv_auto);
tvSave = findViewById(R.id.tv_save);
chkAuto = findViewById(R.id.chk_auto);
chkSave = findViewById(R.id.chk_save);
}
class Mylistener implements View.OnClickListener{
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_login:
boolean flagSave = isChecked(R.id.chk_save);
if(flagSave) {
saveUser();
}else {
deleteUser();
}
Intent intent = new Intent();
intent.setClass(
LoginActivity.this,
MainActivity.class);
startActivity(intent);
break;
case R.id.chk_auto:
boolean flagAuto = isChecked(R.id.chk_auto);
if(flagAuto){
Toast.makeText(
getApplicationContext(),
"您已选择自动登录",
Toast.LENGTH_SHORT
).show();
}else{
Toast.makeText(
getApplicationContext(),
"您已取消自动登录",
Toast.LENGTH_SHORT
).show();
}
break;
}
}
}
private void deleteUser() {
SharedPreferences userSp = getSharedPreferences(
"UserInfo",
0
);
SharedPreferences.Editor editor = userSp.edit();
editor.remove("name");
editor.clear();
editor.commit();
}
private void saveUser() {
SharedPreferences userSp = getSharedPreferences(
"UserInfo", MODE_PRIVATE);
SharedPreferences.Editor editor = userSp.edit();
String name = edtName.getText().toString().trim();
String pwd = edtPwd.getText().toString().trim();
editor.putString("name", name);
editor.putString("pwd", pwd);
editor.commit();
}
private boolean isChecked(int id) {
switch(id){
case R.id.chk_auto:
return chkAuto.isChecked();
case R.id.chk_save:
return chkSave.isChecked();
}
return false;
}
}