一、代码设计
(一)主页面
1.Student2Activity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.helloworld.R;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Student2Activity extends AppCompatActivity {
Button bt_submit;
Button bt_save;
EditText et_sname;
EditText et_smajor;
List<Student> students=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student2);
et_sname= findViewById(R.id.et_sname);
et_smajor=findViewById(R.id.et_smajor);
bt_save=findViewById(R.id.bt_save);
bt_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addStu(view);
}
});
bt_submit=findViewById(R.id.bt_submit);
bt_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setClass(Student2Activity.this,StudentInfo2Activity.class);
intent.putExtra("students", (Serializable) students);
startActivity(intent);
}
});
}
public void addStu(View view){
String name=et_sname.getText().toString();
String major=et_smajor.getText().toString();
Student student=new Student(name,major);
students.add(student);
Toast.makeText(this,"已经添加"+students.size()+"条记录",Toast.LENGTH_SHORT).show();
et_sname.setText(" ");
et_smajor.setText(" ");
et_sname.setFocusable(true);
et_sname.setFocusableInTouchMode(true);
et_sname.requestFocus();
et_smajor.setFocusable(true);
et_smajor.setFocusableInTouchMode(true);
et_smajor.requestFocus();
}
}
2.activity_student2.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:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:paddingTop="20dp"
tools:context="com.example.helloworld1.Student2Activity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="380dp"
android:layout_height="529dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学生信息管理" />
<EditText
android:id="@+id/et_sname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名" />
<EditText
android:id="@+id/et_smajor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="专业" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="380dp"
android:layout_height="450dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<Button
android:id="@+id/bt_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="45dp"
android:paddingRight="75dp"
android:text="添加信息" />
<Button
android:id="@+id/bt_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="55dp"
android:paddingRight="75dp"
android:text="发送信息" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
(二)跳转页面
1.StudentInfo2Activity
package com.example.helloworld1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.helloworld.R;
import java.util.ArrayList;
public class StudentInfo2Activity extends AppCompatActivity {
TextView info_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_info2);
info_show=findViewById(R.id.show);
Bundle bundle=getIntent().getExtras();
ArrayList<Student> students=new ArrayList<Student>();
students=(ArrayList<Student>) bundle.get("students");
for(Student s:students){
String stu_info="姓名:"+s.getName()+",专业:"+s.getMajor()+"\n";
info_show.append(stu_info);
}
}
}
2.activity_studentinfo2.xml
<?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="com.example.helloworld1.StudentInfo2Activity">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/teal_200"
android:textSize="25sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
(三)学生类
Student
package com.example.helloworld1;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable {
private String name;
private String major;
Student(String name, String major){
this.name=name;
this.major=major;
}
public String getName(){ return name; }
public void setName(String name){
this.name=name;
}
public String getMajor(){
return major;
}
public void setMajor(String major){ this.major=major; }
}
二、页面演示
1.点击学生管理进入
2.添加一个学生信息
3.点击添加信息,添加成功
4.以同样的方式再添加一个
5.点击发送