2024/03/28(2024春季)

今日学习时长:4小时

代码行数:400多行

博客数量:1篇

今天主要完成了个人作业中的打卡记录的部分,可以通过点击开始学习按钮记录开始学习时间,点击结束学习按钮记录结束学习的时间,只有都点击过后并且填写了学习内容才能进行提交打卡。

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".RecordActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/goals"
        android:textSize="40sp" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="10dp" />


    <TextView
        android:id="@+id/start_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="25sp" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="10dp" />

    <TextView
        android:id="@+id/end_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="25sp" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="10dp" />

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="25sp" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <Button
                android:id="@+id/s_sty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/s_study"
                android:textSize="30sp" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="10dp" />

            <TextView
                android:id="@+id/is_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="20sp" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="10dp" />

            <Button
                android:id="@+id/e_sty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/e_study"
                android:textSize="30sp" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="10dp" />

            <TextView
                android:id="@+id/is_end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="20sp" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="10dp" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:gravity="center"
            android:text="@string/record"
            android:textSize="23sp" />
        <EditText
            android:id="@+id/record_mes"
            android:layout_width="match_parent"
            android:layout_height="100sp"
            android:background="@drawable/edit_bg"
            android:gravity="top"
            android:maxLines="6"
            android:textSize="21sp" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="10dp" />
        <EditText
            android:id="@+id/week_anl"
            android:visibility="gone"
            android:hint="目标进行能力达成度分析"
            android:layout_width="match_parent"
            android:layout_height="100sp"
            android:background="@drawable/edit_bg"
            android:gravity="top"
            android:maxLines="6"
            android:textSize="21sp" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="10dp" />

        <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/submit"
            android:textSize="30sp" />
    </LinearLayout>
</LinearLayout>
package com.example.myapplication;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.dphelper.GoalsDbHelper;
import com.example.myapplication.dphelper.RecordDbHelper;
import com.example.myapplication.pojo.Goals;
import com.example.myapplication.pojo.Record;
import com.example.myapplication.pojo.User;
import com.example.myapplication.utils.JDBCUtils;
import com.example.myapplication.utils.WeekDayUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.transform.Result;

public class RecordActivity extends AppCompatActivity {

    private TextView start_time;
    private TextView end_time;
    private TextView message_v;
    private Button s_sty;
    private TextView is_start;
    private TextView is_end;
    private Button e_sty;
    private SharedPreferences preferences;
    private String username;
    private LocalDate today;
    private Button submit;
    private EditText record_mes;
    private GoalsDbHelper goalsDbHelper;
    private RecordDbHelper recordDbHelper;
    private LocalDateTime s;
    private LocalDateTime e;
    private EditText week_anl;
    private List<Goals> goal;

    @Override
    protected void onStart() {
        super.onStart();
        recordDbHelper =RecordDbHelper.getInstance(this);
        goalsDbHelper = GoalsDbHelper.getInstance(this);
        recordDbHelper.openReadLink();
        recordDbHelper.openWriteLink();
        goalsDbHelper.openReadLink();
        goalsDbHelper.openWriteLink();
    }

    @Override
    protected void onStop() {
        recordDbHelper.close();
        goalsDbHelper.close();
        super.onStop();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_record);
        preferences = getSharedPreferences("user2", Context.MODE_PRIVATE);
        username=preferences.getString("username",null);
        Log.i("username",username);
        init();
        LocalDate firstDay = today.with(DayOfWeek.MONDAY);
        System.out.println(firstDay);



        new Thread(new Runnable() {
            @Override
            public void run() {
                Connection connection=JDBCUtils.getConn();
                String sql = "select * from goals where username=? and start_date=?";
                try {
                    PreparedStatement ps=connection.prepareStatement(sql);
                    ps.setString(1,username);
                    ps.setString(2, String.valueOf(firstDay));
                    ResultSet resultSet=ps.executeQuery();
                    while (resultSet.next())
                    {
                        Goals goals=new Goals();
                        goals.setStart_date(LocalDate.parse(resultSet.getString(3)));
                        goals.setEnd_date(LocalDate.parse(resultSet.getString(4)));
                        goals.setMessage(resultSet.getString(5));
                        goal.add(goals);
                    }
                    RecordActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(!goal.isEmpty())
                            {
                                start_time.setText("本周计划开始日期:"+String.valueOf(goal.get(0).getStart_date()));
                                end_time.setText("本周计划结束日期:"+String.valueOf(goal.get(0).getEnd_date()));
                                message_v.setText("学习目标:每日打卡"+goal.get(0).getMessage()+"小时");
                            }
                            else{
                                message_v.setText("您未在本周设置学习计划");
                            }
                        }
                    });
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }).start();


        s_sty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocalDateTime s_time=LocalDateTime.now();
                s=s_time;
                DateTimeFormatter of = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String now=s_time.format(of);
                is_start.setText("您这次学习的开始时间为"+now);
                s_sty.setEnabled(false);
            }
        });


        e_sty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println(is_start.getText().toString().trim()+"9999");
                if(is_start.getText().toString().trim().isEmpty())
                {
                    Toast toast = Toast.makeText(RecordActivity.this, "您尚未开始学习", Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }
                LocalDateTime e_time=LocalDateTime.now();
                e=e_time;
                DateTimeFormatter of = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String now=e_time.format(of);
                Duration dur= Duration.between(s, e );
                long minute = dur.toMinutes();
                is_end.setText("您这次学习的结束时间为"+now+"\n\n"+"本次打卡时长为"+minute+"分钟,\n完成学习记录的填写后可提交");
            }
        });


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(is_start.getText().toString().trim().isEmpty() || is_end.getText().toString().trim().isEmpty())
                {
                    Toast toast = Toast.makeText(RecordActivity.this, "您尚未结束学习", Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }
                if(record_mes.getText().toString().isEmpty())
                {
                    Toast toast = Toast.makeText(RecordActivity.this, "学习记录不能为空", Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }
                Record record=new Record();
                record.setUsername(username);
                record.setStart_time(s);
                record.setEnd_time(e);
                record.setAnly(week_anl.getText().toString());
                record.setMessage(record_mes.getText().toString());
                long f=recordDbHelper.insert(record);
                mysqlRecord(record);
                if(f>0)
                {
                    Toast toast = Toast.makeText(RecordActivity.this, "学习记录存入成功", Toast.LENGTH_SHORT);
                    toast.show();
                    finish();
                }
                else {
                    Toast toast = Toast.makeText(RecordActivity.this, "学习记录存入失败", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
    }

    private void mysqlRecord(Record record) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Connection coon = null;
                coon = JDBCUtils.getConn();
                String sql = "insert into record(username,start_time,end_time,week_anl,message) value (?,?,?,?,?)";
                try {
                    PreparedStatement ps = coon.prepareStatement(sql);
                    ps.setString(1,record.getUsername());
                    ps.setString(2, String.valueOf(record.getStart_time()));
                    ps.setString(3, String.valueOf(record.getEnd_time()));
                    ps.setString(4,record.getAnly());
                    ps.setString(5,record.getMessage());
                    ps.executeUpdate();
                    Log.d("daily", "打卡成功");
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    private void init()
    {
        record_mes=findViewById(R.id.record_mes);
        submit =findViewById(R.id.submit);
        is_start=findViewById(R.id.is_start);
        is_end=findViewById(R.id.is_end);
        start_time=findViewById(R.id.start_time);
        end_time=findViewById(R.id.end_time);
        message_v=findViewById(R.id.message);
        s_sty=findViewById(R.id.s_sty);
        e_sty=findViewById(R.id.e_sty);
        week_anl=findViewById(R.id.week_anl);
        today=LocalDate.now();
        Date date=new Date();
        String weekday=new WeekDayUtils().getWeekOfDate(date);
        if(weekday.equals("星期五"))
        {
            week_anl.setVisibility(View.VISIBLE);
        }
        goal=new ArrayList<>();
    }
}

 

posted @ 2024-03-28 21:40  伐木工熊大  阅读(8)  评论(0编辑  收藏  举报