概要
设计一个APP,完成能够添加学生的姓名、专业、年龄、分数等功能的实现。这里对项目的创建等过程就不细说了,主要靠图片和代码解释。这里为大家附上项目代码的网盘地址,有需求的可以自行下载。
https://pan.baidu.com/s/1qJ84gxQ44oj08g6TrYKorA .
提取码:ouun
一、界面布局
activity_main_xml文件
<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文件
<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"/>
效果图
二、主代码
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");
}
}
}
三、效果演示
添加信息
点击发送信息,信息栏清空
点击添加信息,可以看到之前添加的信息