package com.example.innerfilesave;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.b1);
findViewById(R.id.b2);
EditText editText1=findViewById(R.id.et_in);
EditText editText2=findViewById(R.id.et_out);
}
public void click1(View view){
EditText editText1=findViewById(R.id.et_in);
EditText editText2=findViewById(R.id.et_out);
String filename="data.txt";
String context=editText1.getText().toString();
FileOutputStream fos=null;
try {
fos=openFileOutput(filename,MODE_PRIVATE);
fos.write(context.getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}
public void click2(View view){
EditText editText2=findViewById(R.id.et_out);
String context="";
FileInputStream fis=null;
try {
fis=openFileInput("data.txt");
byte[]buffer=new byte[fis.available()];
fis.read(buffer);
context=new String(buffer);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
editText2.setText(context);
}
}
<?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入你想要写入的内容"
android:layout_margin="10dp"
android:id="@+id/et_in"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入"
android:layout_margin="10dp"
android:id="@+id/b1"
android:onClick="click1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="显示读取的内容"
android:layout_margin="10dp"
android:id="@+id/et_out"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
android:layout_margin="10dp"
android:id="@+id/b2"
android:onClick="click2"/>
</LinearLayout>
package com.example.myapplication;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
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 androidx.annotation.CheckResult;
public class MainActivity extends Activity implements android.view.View.OnClickListener , CompoundButton.OnCheckedChangeListener {
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();
Map<String, String> userInfo=qq.getUserInfo(this);
if(userInfo!=null){
et_account.setText(userInfo.get("account"));
et_password.setText(userInfo.get("password"));
}
}
private void initView() {
// TODO Auto-generated method stub
et_account=(EditText)findViewById(R.id.user);
et_password=(EditText)findViewById(R.id.password);
btn_login=(Button)findViewById(R.id.b1);
btn_login.setOnClickListener(this);
CheckBox cb1=findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(this);
CheckBox cb2=findViewById(R.id.cb2);
cb2.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.b1:
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();
}
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()){
case R.id.cb1 :
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString();
boolean isSaveSuccess=qq.saveUserInfo(this, account, password);
if(isSaveSuccess&&b){
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
et_account.setText(null);
et_password.setText(null);
}
break;
case R.id.cb2: account=et_account.getText().toString().trim();
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;
}
if(b)
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
else{
Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/zhanghu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.081" />
<EditText
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginLeft="52dp"
android:layout_toRightOf="@id/zhanghu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.064" />
<TextView
android:id="@+id/mima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/zhanghu"
android:text="密码"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.186" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/user"
android:layout_marginStart="52dp"
android:layout_marginLeft="52dp"
android:layout_toRightOf="@id/mima"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.193" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cb2"
android:layout_alignRight="@+id/password"
android:layout_alignBottom="@+id/cb2"
android:text="登录"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.894"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mima"
android:layout_alignLeft="@+id/mima"
android:text="记住密码"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.27" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cb1"
android:layout_alignBottom="@+id/cb1"
android:layout_toRightOf="@+id/cb1"
android:text="自动登录"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.427"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.27" />