1、layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<ImageView
android:id="@+id/iv_head"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/img12"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20dp"
android:layout_marginTop="200dp"/>
<EditText
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/id"
android:hint="请输入用户名"
android:layout_marginTop="190dp"/>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20dp"
android:layout_marginTop="260dp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/password"
android:hint="请输入密码"
android:inputType="textPassword"
android:layout_marginTop="250dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_centerHorizontal="true"
android:layout_marginTop="310dp"
android:textColor="#FFFFFF"
android:background="#21B8F3"/>
<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Hello World!"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintLeft_toLeftOf="parent"-->
<!-- app:layout_constraintRight_toRightOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent" />-->
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="登录账号:"/>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录密码:"
android:layout_marginTop="25dp"
android:textSize="15dp"/>
<EditText
android:id="@+id/newid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入新用户名"
android:layout_marginTop="60dp"/>
<EditText
android:id="@+id/newpw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入新密码"
android:layout_marginTop="110dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置"
android:paddingLeft="80dp"
android:paddingRight="80dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="180dp"
android:textColor="#FFFFFF"
android:background="#21B8F3"/>
</RelativeLayout>
2、java
package com.example.zuoye;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
String id="123456";
String password="123456";
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if((requestCode==1)&&(resultCode==1)){
id=data.getStringExtra("newid");
password=data.getStringExtra("newpw");
Log.v("a",id);
Log.v("a",password);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
//读取id
String content1="";
FileInputStream fis;
try{
fis=openFileInput("data.text");
byte[] buffer=new byte[fis.available()];
fis.read(buffer);
content1=new String(buffer);
Toast.makeText(MainActivity.this,content1,Toast.LENGTH_SHORT).show();
fis.close();
}catch (Exception e){
e.printStackTrace();
}
//读取pw
String content2="";
FileInputStream fis2;
try{
fis2=openFileInput("data2.text");
byte[] buffer=new byte[fis2.available()];
fis2.read(buffer);
content2=new String(buffer);
Toast.makeText(MainActivity.this,content2,Toast.LENGTH_SHORT).show();
fis2.close();
}catch (Exception e){
e.printStackTrace();
}
EditText et_id1=findViewById(R.id.et_id);
EditText et_password1=findViewById(R.id.et_password);
et_id1.setText(content1);
et_password1.setText(content2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//保存输入的用户名和密码
String et_id=((EditText)findViewById(R.id.et_id)).getText().toString();
String et_password=((EditText)findViewById(R.id.et_password)).getText().toString();
//启动passwordActivity
Intent intent=new Intent(MainActivity.this,passwordActivity.class);
//判断用户名和密码是否正确
if(et_id.equals(id)&&et_password.equals(password))
{
intent.putExtra("id",id);
intent.putExtra("password",password);
//startActivity(intent);
startActivityForResult(intent,1);
}
//文件存储*************************
//存取id
String fileName ="data.text";
String content=et_id;
FileOutputStream fos;
try{
fos=openFileOutput(fileName,MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
}catch (Exception e){
e.printStackTrace();
}
//存取pw
String fileName2 ="data2.text";
String content2=et_password;
FileOutputStream fos2;
try{
fos2=openFileOutput(fileName2,MODE_PRIVATE);
fos2.write(content.getBytes());
fos2.close();
}catch (Exception e){
e.printStackTrace();
}
//文件存储******************************************
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)){
Log.i("abc", "设备可用");
File path = Environment.getExternalStorageDirectory();
File file = new File(path,"dataa.txt");
String data = et_id;
//动态获取权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
try {
fos = new FileOutputStream(file);
fos.write(data.getBytes());
Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
package com.example.zuoye;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
public class passwordActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
Intent intent=getIntent();
String id=intent.getStringExtra("id");
String password=intent.getStringExtra("password");
TextView id2=findViewById(R.id.id);
TextView password2=findViewById(R.id.password);
id2.setText("登录账号:"+id);
password2.setText("登录密码:"+password);
//
//
Button button2=findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//保存新用户名和密码
String newid=((EditText)findViewById(R.id.newid)).getText().toString();
String newpw=((EditText)findViewById(R.id.newpw)).getText().toString();
Intent intent=new Intent();
intent.putExtra("newid",newid);
intent.putExtra("newpw",newpw);
setResult(1,intent);
finish();
}
});
}
}