Android 课程表App

来源:互联网 发布:网络电视成人频道源码 编辑:程序博客网 时间:2024/06/16 21:21


最近写了个简单的Android 课程表App,我是个初学者,这个App里使用了SQLite数据库储存课程数据,课程的视图用CardView卡片视图.

新建课程的方式是代码动态加入的,动态添加View的好处是可以很灵活,如果靠xml的话就有点难扩展了,因为你不知道学生一天总共有多少节课.

                                                  


下面的xml代码是课程表的布局了,一个LinearLayout表示课程表的左侧节数视图,七个Relative表示从星期一到星期天,然后根据用户的输入在正确的地方添加课程视图.代码有点长我没有全部贴出来,整个代码上面还有个父类ScrollView布局的,因为课程表可能有许多节课,是需要下拉的.

<LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <!--工具条-->            <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                android:background="#7fab96c5"                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"                app:popupTheme="@style/AlertDialog.AppCompat.Light"/>            <!--周-->            <LinearLayout                android:layout_width="match_parent"                android:layout_height="30dp"                android:background="#7fab96c5">                <TextView                    android:layout_width="110px"                    android:layout_height="match_parent"                    android:gravity="center"                    android:text="节/周"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周一"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周二"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周三"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周四"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周五"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周六"/>                <TextView                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="周日"/>            </LinearLayout>            <!--课程表-->            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content">                <LinearLayout                    android:id="@+id/class_number_layout"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:orientation="vertical"/>                <RelativeLayout                    android:id="@+id/monday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_margin="1dp"/>                <RelativeLayout                    android:id="@+id/tuesday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_margin="1dp"/>                <RelativeLayout                    android:id="@+id/wednesday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_margin="1dp"/>                <RelativeLayout                    android:id="@+id/thursday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_margin="1dp"/>                <RelativeLayout                    android:id="@+id/friday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_margin="1dp"/>                <RelativeLayout                    android:id="@+id/saturday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_margin="1dp"/>                <RelativeLayout                    android:id="@+id/weekday"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:orientation="vertical"                    android:layout_marginTop="1dp"                    android:layout_marginLeft="1dp"                    android:layout_marginBottom="1dp"/>            </LinearLayout>        </LinearLayout>


下面是课程的布局文件

<android.support.v7.widget.CardView    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="45dp"    android:layout_height="70dp"    app:cardBackgroundColor="#7feacdd1"    app:cardCornerRadius="6dp">    <TextView        android:id="@+id/text_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"/></android.support.v7.widget.CardView>

这里的宽和高都是预览用的,因为实际的宽和高都是代码控制的.


下面是创造视图的java代码

private void createLeftView(Course course) {        //动态生成课程表左侧的节数视图        int len = course.getEnd();        if (len > maxClassNumber) {            LinearLayout classNumberLayout = (LinearLayout) findViewById(R.id.class_number_layout);            View view;            TextView text;            for (int i = 0; i < len-maxClassNumber; i++) {                view = LayoutInflater.from(this).inflate(R.layout.class_number, null);                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(110,180);                view.setLayoutParams(params);                text = view.findViewById(R.id.class_number_text);                text.setText("" + number++);                classNumberLayout.addView(view);            }            maxClassNumber = len;        }    }    //创建卡片课程视图    private void createView(final Course course) {        int integer = course.getDay();        if ((integer < 1 && integer > 7) || course.getStart() > course.getEnd()) {            Toast.makeText(this, "星期几没写对,或课程结束时间比开始时间还早~~", Toast.LENGTH_LONG).show();        } else {            switch (integer) {                case 1: day = (RelativeLayout) findViewById(R.id.monday);break;                case 2: day = (RelativeLayout) findViewById(R.id.tuesday);break;                case 3: day = (RelativeLayout) findViewById(R.id.wednesday);break;                case 4: day = (RelativeLayout) findViewById(R.id.thursday);break;                case 5: day = (RelativeLayout) findViewById(R.id.friday);break;                case 6: day = (RelativeLayout) findViewById(R.id.saturday);break;                case 7: day = (RelativeLayout) findViewById(R.id.weekday);break;            }            final View view = LayoutInflater.from(this).inflate(R.layout.course_card, null); //加载单个课程布局            view.setY(180 * (course.getStart()-1)); //设置开始高度,即第几节课开始            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams                    (ViewGroup.LayoutParams.MATCH_PARENT,(course.getEnd()-course.getStart()+1)*180-2); //设置布局高度,即跨多少节课            view.setLayoutParams(params);            TextView text = view.findViewById(R.id.text_view);            text.setText(course.getCourseName() + "\n" + course.getTeacher() + "\n" + course.getClassRoom()); //显示课程名            day.addView(view);            //长按删除课程            view.setOnLongClickListener(new View.OnLongClickListener() {                @Override                public boolean onLongClick(View v) {                    view.setVisibility(View.GONE);//先隐藏                    day.removeView(view);//再移除课程视图                    SQLiteDatabase sqLiteDatabase =  databaseHelper.getWritableDatabase();                    sqLiteDatabase.execSQL("delete from course where course_name = ?", new String[] {course.getCourseName()});                    return true;                }            });        }    }

我觉得核心的代码就这些了.

完整的代码在我的github里:https://github.com/izcp/Kcb