2023-12-20部分代码优化,后面跟着的一篇中代码我没有改,大家随意看看
本人刚学android,如果里面有哪些垃圾写法请担待。
简单的记录一下
标红的是我认为比较有难点的。
要求:
创建两个活动
1、ActivityMain有一个Button和一个ListView控件,其中ListView控件来输出学生信息,Button控件是点击添加可以跳转到录入页面来继续录入别的学生信息。
2、ActivityStudent实现学生信息的录入。
姓名、学号(编辑框);
性别(两个RadioButton);
学院(下拉框,可选,例如:计算机学院、电气学院),专业(下拉框,与学院实现二级联动,计算机学院只能选他里面的专业,不能选择电气学院的专业);
爱好(多个复选框);
录入按钮(Button控件)
(注:这个算是首页,如果不是这个作为首页想要修改的,可以打开蓝色选中的目录
打开这个文件,然后将下图中蓝色选中的移到如图所示的位置
)
运行效果:
废话不多说,上代码。
源码:
Java文件有三个。
Student.java
package com.example.studentmgr;
import java.io.Serializable;
public class Student implements Serializable {
private String name ;
private String number ;
private String sex ;
private String college;
private String subject;
private String hobby ;
Student(){}
public Student(String name, String number, String sex, String college, String subject, String hobby) {
this.name = name;
this.number = number;
this.sex = sex;
this.college = college;
this.subject = subject;
this.hobby = hobby;
}
public String getName(){return name;}
public void setName(String name){this.name = name;}
public String getNumber(){return number;}
public void setNumber(String number){this.number = number;}
public String getSex(){return sex;}
public void setSex(String sex){this.sex = sex;}
public String getCollege(){return college;}
public void setCollege(String college){this.college = college;}
public String getSubject() {return subject;}
public void setSubject(String subject){this.subject=subject;}
public String getHobby() {return hobby;}
public void setHobby(String hobby) {this.hobby = hobby;}
}
ActivityStudent.java
package com.example.studentmgr;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
public class ActivityStudent extends Activity {
RadioButton s1, s2;
Student student=new Student();
Spinner spcollege, spsubject;
ArrayAdapter<String> collegeAdapter;
ArrayAdapter<String> subjectAdapter;
String name;
String number;
String sex;
String college;
String subject;
String checked;
String[] collegeList = new String[]{"计算机学院", "电气学院"};
String[] jisuanji = new String[]{"软件工程", "信息安全"};
String[] dianqi = new String[]{"电气工程", "电机工程"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
s1 = (RadioButton) findViewById(R.id.man);
s2 = (RadioButton) findViewById(R.id.woman);
spcollege = (Spinner) findViewById(R.id.spcollege);
spsubject = (Spinner) findViewById(R.id.spsubject);
setSpinner();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
final CheckBox ID1 = (CheckBox) findViewById(R.id.ID1);
final CheckBox ID2 = (CheckBox) findViewById(R.id.ID2);
final CheckBox ID3 = (CheckBox) findViewById(R.id.ID3);
final CheckBox ID4 = (CheckBox) findViewById(R.id.ID4);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = ((EditText) findViewById(R.id.name)).getText().toString();
number = ((EditText) findViewById(R.id.number)).getText().toString();
if (s1.isChecked()) {
sex = ((RadioButton) findViewById(R.id.man)).getText().toString();
} else {
sex = ((RadioButton) findViewById(R.id.woman)).getText().toString();
}
if (ID1.isChecked()) {
checked += ID1.getText().toString();
}
if (ID2.isChecked()) {
checked += ID2.getText().toString();
}
if (ID3.isChecked()) {
checked += ID3.getText().toString();
}
if (ID4.isChecked()) {
checked += ID4.getText().toString();
}
if (!"".equals(name) && !"".equals(number)) {
student.setName(name);
student.setNumber(number);
student.setSex(sex);
student.setCollege(college);
student.setSubject(subject);
student.setHobby(checked);
ArrayList<Student> list= (ArrayList<Student>) getIntent().getSerializableExtra("list");
if(list==null){
list=new ArrayList<>();
}
list.add(student);
Intent intent = new Intent(ActivityStudent.this, ActivityMain.class);
intent.putExtra("list", (Serializable) list);
startActivity(intent);
} else {
Toast.makeText(ActivityStudent.this, "请填写完整!!!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void setSpinner() {
//绑定适配器和值
collegeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, collegeList);
spcollege.setAdapter(collegeAdapter);
spcollege.setSelection(0);//设置初始默认值
//绑定适配器和值
// subjectAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, jisuanji);
// spsubject.setAdapter(subjectAdapter);
// spsubject.setSelection(0);//设置初始默认值
//设置列表项选中监听
spcollege.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//获取选中项的值
college = adapterView.getItemAtPosition(i).toString();
//根据选中的不同的值绑定不同的适配器
if (college.equals("计算机学院")) {
subjectAdapter = new ArrayAdapter<String>(ActivityStudent.this, android.R.layout.simple_spinner_item,jisuanji);
spsubject.setAdapter(subjectAdapter);
} else if (college.equals("电气学院")) {
subjectAdapter = new ArrayAdapter<String>(ActivityStudent.this, android.R.layout.simple_spinner_item,dianqi);
spsubject.setAdapter(subjectAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//设置列表项选中监听
spsubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//获取列表项的值
subject = adapterView.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
ActivityMain.java
package com.example.studentmgr;
import com.example.myapplication.Student;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.String;
public class ActivityMain extends Activity {
ArrayList<Student> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ArrayList<Student>) getIntent().getSerializableExtra("list");
ListView listview = (ListView) findViewById(R.id.listview); // 获取列表视图
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();// 创建一个list集合
if(list!=null){
for(int i=0;i<list.size();i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("名字", (String) list.get(i).getName());
map.put("学号", (String) list.get(i).getNumber());
map.put("性别", (String) list.get(i).getSex());
map.put("学院", (String) list.get(i).getCollege());
map.put("专业", (String) list.get(i).getSubject());
map.put("爱好", (String) list.get(i).getHobby());
listItems.add(map);
}
}
SimpleAdapter adapter = new SimpleAdapter(this, listItems,
R.layout.item, new String[]{"名字", "学号", "性别", "学院", "专业", "爱好"}, new int[]{
R.id.name, R.id.number, R.id.sex, R.id.college, R.id.subject, R.id.checked});
// 创建SimpleAdapter
listview.setAdapter(adapter);// 将适配器与ListView关联
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent();
intent1.setClass(ActivityMain.this, ActivityStudent.class);
intent1.putExtra("list", (Serializable) list);
startActivity(intent1);
}
});
}
}
3个xml文件
activity_student.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:orientation="vertical"
tools:context="com.example.studentmgr.ActivityStudent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20dp"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:"
android:textSize="20dp" />
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textColor="#000000"
android:textSize="20sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学院:"
android:textColor="#000000"
android:textSize="20sp"/>
<Spinner
android:id="@+id/spcollege"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专业:"
android:textColor="#000000"
android:textSize="20sp"/>
<Spinner
android:id="@+id/spsubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:textColor="#000000"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/ID1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文学"
/>
<CheckBox
android:id="@+id/ID2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育"
/>
<CheckBox
android:id="@+id/ID3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"
/>
<CheckBox
android:id="@+id/ID4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美术"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.studentmgr.ActivityMain">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="信息显示"
android:textSize="30dp"/>
<Button
android:layout_weight="1"
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=" 添加"
android:layout_centerInParent="true"/>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
item.xml
<?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">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"></ImageView>
<TextView
android:id="@+id/xingming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/image"
android:layout_toRightOf="@+id/image"
android:text="姓名:"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/xingming"/>
<TextView
android:id="@+id/xuehao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/xingming"
android:layout_toRightOf="@+id/image"
android:text="学号:"/>
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/xuehao"
android:layout_below="@+id/name"/>
<TextView
android:id="@+id/xingbie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/xuehao"
android:layout_toRightOf="@+id/image"
android:text="性别:"/>
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/xuehao"
android:layout_toRightOf="@+id/xingbie"/>
<TextView
android:id="@+id/xueyuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/xingbie"
android:layout_toRightOf="@+id/image"
android:text="学院:"/>
<TextView
android:id="@+id/college"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/xueyuan"
android:layout_below="@+id/sex"/>
<TextView
android:id="@+id/zhuanye"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/xueyuan"
android:layout_toRightOf="@+id/image"
android:text="专业:"/>
<TextView
android:id="@+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/zhuanye"
android:layout_below="@+id/college" />
<TextView
android:id="@+id/aihao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/zhuanye"
android:layout_toRightOf="@+id/image"
android:text="爱好:"/>
<TextView
android:id="@+id/checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/aihao"
android:layout_below="@+id/subject"/>
</RelativeLayout>