Android studio case-basic student information (parameter transfer)
Overview
Design an APP to complete the realization of functions that can add students' names, majors, ages, and scores. I won’t elaborate on the process of project creation and so on here, mainly relying on pictures and code explanations. Here is the network disk address of the project code for everyone, if you need it, you can download it yourself.
https://pan.baidu.com/s/1qJ84gxQ44oj08g6TrYKorA .
Extraction code: ouun
1. Interface layout
activity_main_xml file
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生信息"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.309"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.28" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:id="@+id/name_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="专业"
android:id="@+id/major_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄"
android:inputType="number"
android:id="@+id/age_edit"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="成绩"
android:inputType="numberDecimal"
android:id="@+id/score_edit"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送信息"
android:id="@+id/btn_save"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加信息"
android:layout_weight="1"/>
</LinearLayout>
info_xml file
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:textColor="@color/teal_200"
android:id="@+id/infoshow"/>
Effect picture
Second, the main code
mainactivity.java
public class MainActivity extends AppCompatActivity {
Button btn_submit;
Button btn_save;
EditText name_edit;
EditText major_edit;
EditText age_edit;
EditText score_edit;
ArrayList<student> students=new ArrayList<student>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name_edit=(EditText) findViewById(R.id.name_edit);
major_edit=(EditText) findViewById(R.id.major_edit);
age_edit=(EditText) findViewById(R.id.age_edit);
score_edit=(EditText) findViewById(R.id.score_edit);
btn_save=(Button) findViewById(R.id.btn_save);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=name_edit.getText().toString();
String major=major_edit.getText().toString();
int age=Integer.parseInt(age_edit.getText().toString());
double score=Double.parseDouble(score_edit.getText().toString());
student s=new student(name,major,age,score);
students.add(s);
name_edit.setText(" ");
major_edit.setText(" ");
age_edit.setText(" ");
score_edit.setText(" ");
}
});
btn_submit=(Button) findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this,info.class);
intent.putExtra("students",students);
startActivity(intent);
}
});
}
}
student.java
public class student implements Serializable{
private String name;
private String major;
private int age;
private double score;
student(String name,String major,int age,double score){
this.name=name;
this.major=major;
this.age=age;
this.score=score;
}
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
info.java
public class info extends AppCompatActivity {
TextView infoshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
infoshow=(TextView) findViewById(R.id.infoshow);
Bundle bundle=getIntent().getExtras();
ArrayList<student> students=( ArrayList<student>) bundle.get("students");
for(student s:students){
infoshow.append("姓名:"+s.getName()+";专业:"+s.getMajor()+";年龄:"+s.getAge()+";成绩:"+s.getScore()+"\n");
}
}
}
Three, effect demonstration
Add information
Click to send information, the information bar is cleared,
click Add information, you can see the previously added information