2024/03/25(2024春季)
今日学习时长3小时
代码行数:331行
博客数量:1篇
今天主要完成了每周目标制定和每日打卡的操作,可以根据今天日期来判断今日是否可以制定目标。然后打卡完成后会存入数据库。
还完成在每周五能对这周的任务完成进行自我评价分析。
package com.example.myapplication; import android.content.Context; import android.content.Intent; 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.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.UserDbHelper; import com.example.myapplication.pojo.Goals; import com.example.myapplication.pojo.Record; import com.example.myapplication.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class GoalsActivity extends AppCompatActivity { private EditText start_time_v; private EditText end_time_v; private EditText message_v; private Button save_message_v; private LocalDate startDate; private LocalDate endDate; private String username; private SharedPreferences preferences; private GoalsDbHelper goalsDbHelper; @Override protected void onStart() { super.onStart(); goalsDbHelper = GoalsDbHelper.getInstance(this); goalsDbHelper.openReadLink(); goalsDbHelper.openWriteLink(); } @Override protected void onStop() { goalsDbHelper.close(); super.onStop(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_goals); init(); save_message_v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(message_v.getText().toString().isEmpty()) { Toast toast = Toast.makeText(GoalsActivity.this, "学习目标不可为空", Toast.LENGTH_SHORT); toast.show(); return; } newGoals(); } }); } private void newGoals() { Goals goals=new Goals(); goals.setUsername(username); goals.setStart_date(startDate); goals.setEnd_date(endDate); goals.setMessage(message_v.getText().toString()); long is_saved=goalsDbHelper.insert(goals); mysqlGoals(goals); if(is_saved>0) { Toast toast = Toast.makeText(GoalsActivity.this, "插入成功", Toast.LENGTH_SHORT); toast.show(); finish(); } else { Toast toast = Toast.makeText(GoalsActivity.this, "插入失败,一周内不可重复制定学习计划", Toast.LENGTH_SHORT); toast.show(); } } private void mysqlGoals(Goals goals) { new Thread(new Runnable() { @Override public void run() { Connection coon = null; coon = JDBCUtils.getConn(); String sql = "insert into goals(username,start_date,end_date,message) value (?,?,?,?)"; try { PreparedStatement ps = coon.prepareStatement(sql); ps.setString(1,goals.getUsername()); ps.setString(2, String.valueOf(goals.getStart_date())); ps.setString(3, String.valueOf(goals.getEnd_date())); ps.setString(4,goals.getMessage()); ps.executeUpdate(); Log.d("goals set", "目标创建成功"); } catch (SQLException e) { throw new RuntimeException(e); } } }).start(); } private void init() { start_time_v=findViewById(R.id.start_time); end_time_v=findViewById(R.id.end_time); message_v=findViewById(R.id.message); save_message_v=findViewById(R.id.save_message); startDate= LocalDate.now(); endDate= startDate.plusDays(6); preferences = getSharedPreferences("user2", Context.MODE_PRIVATE); username=preferences.getString("username",null); DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd"); start_time_v.setFocusableInTouchMode(false);//设置为不可编辑 end_time_v.setFocusableInTouchMode(false); start_time_v.setText(startDate.format(fmt)); end_time_v.setText(endDate.format(fmt)); } }