1.布局文件
<?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=".MainActivity"
android:orientation="vertical"
android:background="#E6E6E6">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/head"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="账户:"
android:textColor="@color/black"
android:textSize="20dp" />
<EditText
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="10dp"
android:background="@null"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="密码:"
android:textColor="@color/black"
android:textSize="20dp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="10dp"
android:background="@null"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="登录"
android:textSize="20dp"
android:id="@+id/btn"/>
</LinearLayout>
2.工具类文件
FileSaveQQ.xml
package cn.itcast.saveqq;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class FileSaveQQ {
public static boolean info(Context context , String accout , String passwords) throws IOException {
FileOutputStream os = null;
try {
os = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
os.write((accout + ":" +passwords).getBytes());
return true;
}
catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
finally {
os.close();
}
}
public static Map<String,String> getinfo(Context context) throws IOException {
String content= "";
FileInputStream rs = null;
try {
rs = context.openFileInput("data.txt");
byte[] buff = new byte[rs.available()];
rs.read(buff);
content = new String(buff);
Map<String,String> s = new HashMap<String, String>();
String[] infos = content.split(":");
s.put("account",infos[0]);
s.put("password",infos[1]);
return s;
}
catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3.MainAcitivity.java
package cn.itcast.saveqq;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText account;
private EditText password;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jianting();
try {
Map<String , String> s = FileSaveQQ.getinfo(this);
if (s!= null){
account.setText(s.get("account"));
password.setText(s.get("password"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void jianting() {
account = findViewById(R.id.account);
password = findViewById(R.id.password);
btn = findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btn:
String accout = account.getText().toString().trim();
String passwords = password.getText().toString();
if (TextUtils.isEmpty(accout)){
Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(passwords)){
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
try {
boolean isSaveSuccess = FileSaveQQ.info(this,accout,passwords);
if (isSaveSuccess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}