Android开发之学生管理系统(一)

版权声明:转载请标明链接, https://blog.csdn.net/qq_43433255/article/details/88375076

学生管理系统设计的几点要求:
1)实现多个学生信息的添加和显示
2)用EditText实现姓名、年龄的输入,并有输入校验(空校验,数字校验)
3)用AutoCompleteTextView的实现专业的输入,并有提示功能。
4)通过CheckBox实现多门课程选择
5)通过Radio实现性别
6)用下拉框选择学院
7)用日期控件实现入学时间的输入
8)点击提交按钮后,将输入的信息,用列表控件ListView显示在另一个Activity。
9)实现列表中学生信息的编辑和删除。

代码的设计:
在这里插入图片描述
在这里插入图片描述

下面就介绍代码:

shape.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >

            <solid android:color="#ffffff" />
            <!-- 边框 -->
            <stroke
                android:width="1dip"
                android:color="#ffffff" />
            <!-- 内填充颜色 -->
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
            <!-- 圆角 -->
            <corners android:radius="6dp" />
        </shape>

    </item>
</selector>

shape_button.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            >
            <!-- 填充的颜色 -->
            <solid android:color="#87CEFA"/>
            <!-- 设置按钮的四个角为弧形 -->
            <!-- android:radius 弧形的半径 -->
            <corners android:radius="105dip"/>

            <!-- padding: Button 里面的文字与Button边界的间隔 -->
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp"
                />
        </shape>
    </item>
</selector>

activity_chose_coures2.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".ChoseCouresActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="225dp"
        android:layout_height="52dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        android:text="学生选课系统"
        android:textColor="#f47983"
        android:textSize="25dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.219"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <AutoCompleteTextView
        android:id="@+id/stu_name"
        android:layout_width="240dp"
        android:layout_height="47dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape"
        android:hint="name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/stu_name"
        android:layout_marginStart="56dp"
        android:layout_marginTop="16dp"
        android:text="选修课程:"
        android:textColor="#f47983"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/stu_name" />

    <LinearLayout
        android:id="@+id/check_project"
        android:layout_width="242dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/stu_name"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/shape"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2">

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="C++"
            android:textSize="15sp" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Java"
            android:textSize="15sp" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Python"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/check_project"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/check_project">

        <Button
            android:id="@+id/button_add"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:background="@drawable/shape_button"
            android:text="添加选课"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/button_send"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_marginLeft="50dp"
            android:background="@drawable/shape_button"
            android:text="查看选课"
            android:textColor="#ffffff" />
    </LinearLayout>



</android.support.constraint.ConstraintLayout>

activit_other.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".otherActivity">

    <TextView
        android:id="@+id/stu_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="学生信息"
        android:textColor="#ff9999"
        android:textSize="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.029"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!-- 显示学生信息列表 -->
    <ListView
        android:id="@+id/studentlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textColor="#5e9cff"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/stu_title" />

</android.support.constraint.ConstraintLayout>

activity_other2.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".other2Activity">
    <TextView
        android:id="@+id/stu_show2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#5e9cff" />

</android.support.constraint.ConstraintLayout>

activity_stu.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".stuActivity">

    <Button
        android:id="@+id/button_1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="76dp"
        android:layout_marginTop="112dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/shape_button"
        android:onClick="onClick"
        android:text="注册"
        android:textColor="#ffffff"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_1"
        android:layout_marginTop="52dp"
        android:background="@drawable/shape_button"
        android:onClick="onClick"
        android:text="选课"
        android:textColor="#ffffff"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.413"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_1" />


</android.support.constraint.ConstraintLayout>

activity_stu_registration.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".stuRegistrationActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="182dp"
        android:layout_height="52dp"
        android:layout_marginStart="96dp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text="学生注册"
        android:textColor="#f47983"
        android:textSize="25dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <AutoCompleteTextView
        android:id="@+id/stu_name"
        android:layout_width="200dp"
        android:layout_height="53dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/shape"
        android:hint="name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.445"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <RadioGroup
        android:id="@+id/rg_gender"
        android:layout_width="197dp"
        android:layout_height="50dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/stu_major"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.443"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/stu_name"
        app:layout_constraintVertical_bias="0.0">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="男"
            android:textColor="#ff9999"
            android:textSize="18sp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="女"
            android:textColor="#ff9999"
            android:textSize="18sp" />
    </RadioGroup>

    <Spinner
        android:id="@+id/stu_major"
        android:layout_width="203dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:hint="major"
        app:layout_constraintBottom_toTopOf="@+id/stu_age"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">

    </Spinner>

    <EditText
        android:id="@+id/stu_age"
        android:layout_width="156dp"
        android:layout_height="46dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="296dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/shape"
        android:hint="age"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TableRow
        android:layout_width="319dp"
        android:layout_height="60dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/stu_age">

        <Button
            android:id="@+id/button_add"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:background="@drawable/shape_button"
            android:text="添加信息"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/button_send"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:layout_marginLeft="40dp"
            android:background="@drawable/shape_button"
            android:text="查看信息"
            android:textColor="#ffffff" />
    </TableRow>
</android.support.constraint.ConstraintLayout>

au_checkboxlist.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="java"
        android:textSize="24sp">
    </CheckBox>
</android.support.constraint.ConstraintLayout>

au_textview.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="18sp"
        android:textColor="#ff9999">
    </TextView>

</android.support.constraint.ConstraintLayout>

stu_list.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="易烊千玺"
        android:textColor="#404040"
        android:textSize="24sp"
        tools:layout_editor_absoluteX="25dp"
        tools:layout_editor_absoluteY="65dp" />

    <TextView
        android:id="@+id/major"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:text="物联网工程"
        android:textColor="#003472"
        android:textSize="15sp"
        tools:layout_editor_absoluteX="25dp"
        tools:layout_editor_absoluteY="106dp" />

    <ImageView
        android:id="@+id/edit"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_toLeftOf="@+id/delete"
        android:src="@drawable/edit"
        tools:layout_editor_absoluteX="251dp"
        tools:layout_editor_absoluteY="77dp" />

    <ImageView
        android:id="@+id/delete"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="16dp"
        android:src="@drawable/delete"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="77dp" />

</android.support.constraint.ConstraintLayout>

然后,就是剩下的代码了。
ChoseCouresActivity的代码:

package com.example.wuluo.student;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class ChoseCouresActivity extends AppCompatActivity implements View.OnClickListener{
    Button button_add;
    Button button_send;
    AutoCompleteTextView stu_name;
    LinearLayout check_project;
    ArrayList<CouresInfo> couresList=new ArrayList<CouresInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_coures);
        button_add = (Button) findViewById(R.id.button_add);
        button_send = (Button) findViewById(R.id.button_send);
        stu_name  = (AutoCompleteTextView) findViewById(R.id.stu_name);
        check_project=(LinearLayout)findViewById(R.id.check_project);
        button_add.setOnClickListener(this);
        button_send.setOnClickListener(this);
        //输入时自动提示
        String[] arr={"张三","李四","wang"};
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter(this,R.layout.au_textview,arr);
        stu_name.setAdapter(arrayAdapter);
    }
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.button_add:
                if(stu_name.length() == 0){
                    stu_name.setError("不能为空!");
                    //Toast.makeText(stuActivity.this,"输入不能为空!",Toast.LENGTH_SHORT).show(); //弹出一个自动消失的提示框
                    //return;
                }else {
                    String name=stu_name.getText().toString() ;
                    String couresName=getCheckedBoxInfo(check_project);
                    CouresInfo coures=new CouresInfo(name,couresName);
                    couresList.add(coures);
                    if(coures!=null){
                        Toast.makeText(this,"添加完成!",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                break;
            case R.id.button_send:
                if(couresList.isEmpty()) return;
                intent.putExtra("coures",couresList);
                intent.setClass(this, other2Activity.class);
                startActivity(intent);
                break;
        }
    }
    //
    private String getCheckedBoxInfo(LinearLayout check_project) {
        String couresName="";
        int num=check_project.getChildCount();
        for(int i=0;i<num;i++){
            CheckBox checkBox=(CheckBox)check_project.getChildAt(i);
            if(checkBox.isChecked()){
                couresName+=checkBox.getText().toString()+"\n";
            }
        }
        return couresName;
    }
    //自定义动态checkbox的内容
    public void initcheckbox(String[] array) {
        for(String s:array){
            CheckBox checkBox=(CheckBox) View.inflate(this,R.layout.au_checkboxlist,null);
            checkBox.setText(s);
            check_project.addView(checkBox);
        }
    }
}


CouresInfo的代码:

package com.example.wuluo.student;

import java.io.Serializable;

/**
 * Created by wuluo on 2018/12/17
 */
public class CouresInfo implements Serializable {
    private String name;
    private String couresName;

    public CouresInfo(String n,String m)
    {
        name = n;
        couresName = m;
    }

    public String getCouresName() {
        return couresName;
    }

    public String getName() {
        return name;
    }

    public void setCouresName(String couresName) {
        this.couresName = couresName;
    }

    public void setName(String name) {
        this.name = name;
    }
}


other2Activity的代码:

package com.example.wuluo.student;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.SuperscriptSpan;
import android.widget.TextView;

import java.util.ArrayList;

public class other2Activity extends AppCompatActivity {
    TextView stu_show2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other2);
        stu_show2 = (TextView) findViewById(R.id.stu_show2);

        Bundle bundle=getIntent().getExtras();//获得传值
        ArrayList<CouresInfo> coures=(ArrayList<CouresInfo>)bundle.get("coures");

        for (CouresInfo c: coures) {
            String list1 = c.getName()+" 已选课程有: " + "\n";
            String couresInfo = c.getCouresName() + "\n";
            //=========================字体样式设置============================================
            SpannableString text1=new SpannableString(list1);
            //设置前景色
            ForegroundColorSpan forcolor=new ForegroundColorSpan(Color.parseColor("#ef7a82"));
            text1.setSpan(forcolor,0,text1.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//定位
            //设置背景色
            BackgroundColorSpan back=new BackgroundColorSpan(Color.parseColor("#80ffffff"));
            text1.setSpan(back,0,text1.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//定位
            //设置文字相对大小
            RelativeSizeSpan relativeSizeSpan=new RelativeSizeSpan(1.5f);
            text1.setSpan(relativeSizeSpan,0,c.getName().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //设置上标
            SuperscriptSpan superscriptSpan=new SuperscriptSpan();
            text1.setSpan(superscriptSpan,0,c.getName().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //设置粗体和斜体
            StyleSpan styleSpan=new StyleSpan(Typeface.BOLD);
            StyleSpan styleSpan1=new StyleSpan(Typeface.ITALIC);
            text1.setSpan(styleSpan,0,c.getName().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text1.setSpan(styleSpan1,0,c.getName().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //================================================================================
            stu_show2.append(text1);
            stu_show2.append(couresInfo);
        }
    }
}

otherActivity的代码:

package com.example.wuluo.student;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class otherActivity extends AppCompatActivity {
    List<Map<String, Object>> stuMap = new ArrayList<Map<String, Object>>();
    List <String>names = new ArrayList<String>();
    List <String>majors = new ArrayList<String>();
    ListView studentlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        studentlist = (ListView) findViewById(R.id.studentlist);
        initlistView();//声明数组
        //获取传值
        Bundle bundle = getIntent().getExtras();
        ArrayList<StuInfo> students = (ArrayList<StuInfo>) bundle.get("students");

        for (int i = 0; i < students.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            String n = students.get(i).getName();
            names.add(n);
            map.put("name", names.get(i));
            String m = students.get(i).getMajor();
            majors.add(m);
            map.put("major", majors.get(i));
            stuMap.add(map);
        }

        String[] key = {"name", "major"};
        int[] value = new int[]{R.id.name, R.id.major};
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, stuMap, R.layout.stu_list, key, value);
        studentlist.setAdapter(simpleAdapter);
    }
    private void initlistView() {
        List<Map<String, Object>> stuMap = new ArrayList<Map<String, Object>>();
        String[] names = new String[100];
        String[] majors = new String[100];
    }
}


stuActivity的代码:

package com.example.wuluo.student;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class stuActivity extends AppCompatActivity implements View.OnClickListener{
    Button button_1;
    Button button_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu);
        button_1 = (Button) findViewById(R.id.button_1);
        button_2 = (Button) findViewById(R.id.button_2);
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.button_1:
                intent.setClass(this,stuRegistrationActivity.class);
                startActivity(intent);
                break;
            case R.id.button_2:
                intent.setClass(this,ChoseCouresActivity.class);
                startActivity(intent);
                break;
        }

    }
}

StuInfo的代码:

package com.example.wuluo.student;

import java.io.Serializable;

/**
 * Created by wuluo on 2018/12/17
 */
public class StuInfo implements Serializable {
    private String name;
    private String major;

    public StuInfo(String n, String m)
    {
        name=n;
        major=m;
    }

    public String getMajor() {
        return major;
    }
    public String getName() {
        return name;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public void setName(String name) {
        this.name = name;
    }
}


stuRegistrationActivity的代码:

package com.example.wuluo.student;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class stuRegistrationActivity extends AppCompatActivity implements View.OnClickListener{
    Button button_add;
    Button button_send;
    AutoCompleteTextView stu_name;
    RadioGroup rg_gender;
    Spinner stu_major;
    EditText stu_age;
    ArrayList<StuInfo> studentList=new ArrayList<StuInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_registration);
        button_add = (Button) findViewById(R.id.button_add);
        button_send = (Button) findViewById(R.id.button_send);
        stu_name  = (AutoCompleteTextView) findViewById(R.id.stu_name);
        stu_major = (Spinner) findViewById(R.id.stu_major);
        stu_age=(EditText) findViewById(R.id.stu_age);
        rg_gender=(RadioGroup)findViewById(R.id.rg_gender);
        button_add.setOnClickListener(this);
        button_send.setOnClickListener(this);
        //输入时自动提示
        String[] name={"张三","李四","wang"};
        ArrayAdapter<String> arrayAdapter2=new ArrayAdapter(this,R.layout.au_textview,name);
        stu_name.setAdapter(arrayAdapter2);

        String[] arr={"物联网工程","计算机科学","电信"};
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter(this,R.layout.au_textview,arr);
        stu_major.setAdapter(arrayAdapter);


    }
    private  boolean isDigtal(String num){
        return num.matches("[0-9]{1,}");
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.button_add:
                if(stu_name.length() == 0){
                    stu_name.setError("不能为空!");
                }

                if(stu_age.length()==0){
                    stu_age.setError("不能为空!");
                    //Toast.makeText(stuActivity.this,"输入不能为空!",Toast.LENGTH_SHORT).show(); //弹出一个自动消失的提示框
                    //return;
                }else {
                    String name=stu_name.getText().toString() ;//
                    String sex=getCheckedRadioInfo(rg_gender);//返回性别
                    String major=stu_major.getSelectedItem().toString();
                    String age=stu_age.getText().toString();
                    if (!isDigtal(age.toString())){
                        stu_age.setError("只能为数字!");
                    }
                    StuInfo stu=new StuInfo(name,sex,major,age);//字符串
                    studentList.add(stu);//studentList添加学生
                    if(stu!=null&&isDigtal(age.toString())){
                        Toast.makeText(this,"添加完成!",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                break;
            case R.id.button_send:
                if(studentList.isEmpty()) return;
                intent.putExtra("students",studentList);//传值
                intent.setClass(this, otherActivity.class);
                startActivity(intent);
                break;
        }
    }
    //获得RadioGroup中返回的sex性别值(String类型)
    private String getCheckedRadioInfo(RadioGroup radioGroup) {
        String sex="";
        int num=radioGroup.getChildCount();
        for(int i=0;i<num;i++){
            RadioButton rd=(RadioButton)radioGroup.getChildAt(i);
            if(rd.isChecked()){
                sex=rd.getText().toString();
                break;
            }
        }
        return sex;
    }

}

代码有点多,但重点是思路的一个理解,代码的发布,更多是让人学习并理解,能够进行一个思考。

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43433255/article/details/88375076