avtivity_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"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="账号:"
android:textColor="#000000"
android:textSize="28sp" />
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="密码:"
android:textSize="28sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:inputType="textPassword"
android:padding="10dp"/>
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="登录"/>
</LinearLayout>
FileSaveQQ.java
package cn.itcast.saveqq;
import android.content.Context;
import android.os.Environment;
import com.example.myapplication7.MainActivity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 state = Environment.getExternalStorageState(); //获取SD卡状态
if (state.equals(Environment.MEDIA_MOUNTED)) { //获取SD卡是否可用可用
File SDPath = Environment.getExternalStorageDirectory(); //获取SD卡路径
File file = new File(SDPath, "date.txt");
String data = "";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
MainActivity,java
package com.example.myapplication7;
import android.app.Activity;
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;
import cn.itcast.saveqq.FileSaveQQ;
public class MainActivity extends Activity implements View.OnClickListener {
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
try{
Map<String, String> userInfo = FileSaveQQ.getinfo(this);
if (userInfo != null) {
et_account.setText(userInfo.get("account"));
et_password.setText(userInfo.get("password"));
}
}catch(IOException e){
e.printStackTrace();
}
}
private void initView() {
et_account = findViewById(R.id.et_account);
et_password = findViewById(R.id.et_password);
btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
String account = et_account.getText().toString().trim();
String password = et_password.getText().toString();
if (TextUtils.isEmpty(account)) {
Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
try {
boolean isSaveSuccess = FileSaveQQ.info(this, account, password);
if (isSaveSuccess) {
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}