2024/3/20

所花时间:4小时

代码行:230行

博客量:1篇

了解到的知识点:进行安卓第一次作业的第二个页面的编写

package com.example.enroll;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class learningrecord extends AppCompatActivity {
private Button mButton_start_date;
private Button mButton_end_date;

private EditText mEditText_record_text;
private Button mButton_add_day_record;
private MySQLConnector mySQLConnector;
private TextView tvStartDate;
private TextView tvEndDate;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learningrecord);
mButton_start_date = findViewById(R.id.btn_start_date);
mButton_end_date = findViewById(R.id.btn_end_date);
tvStartDate = findViewById(R.id.text_start_date);
tvEndDate = findViewById(R.id.text_end_date);

mButton_add_day_record=findViewById(R.id.btn_add_day_record);
mEditText_record_text=findViewById(R.id.Edit_record_text);

mySQLConnector=new MySQLConnector();

mButton_add_day_record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertData3();
}
});
}

public void selectStartDate(View view) {
showDateTimePicker("start");
}

public void selectEndDate(View view) {
showDateTimePicker("end");
}

private void showDateTimePicker(final String type) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(learningrecord.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// Handle date selection
// Show TimePicker dialog for selecting time
showTimePicker(type, year, month, dayOfMonth);
}
}, year, month, day);

datePickerDialog.show();
}

private void showTimePicker(final String type, final int year, final int month, final int day) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(learningrecord.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Handle time selection
// Here you can set the selected start or end date/time
String selectedDateTime = year + "-" + (month + 1) + "-" + day + " " + hourOfDay + ":" + minute;
if (type.equals("start")) {
// Set selected start date/time
//startDateTextView.setText(selectedDateTime);
updateSelectedDate(selectedDateTime,type);
} else if (type.equals("end")) {
// Set selected end date/time
// endDateTextView.setText(selectedDateTime);
updateSelectedDate(selectedDateTime,type);
}
}
}, hour, minute, true);

timePickerDialog.show();
}
private void updateSelectedDate(String selectedDate, String type) {
if (type.equals("start")) {
tvStartDate.setText( selectedDate);
} else if (type.equals("end")) {
tvEndDate.setText( selectedDate);
}
}

private void insertData3(){
String str_start_date=tvStartDate.getText().toString();
String str_end_date=tvEndDate.getText().toString();
String str_record_text= mEditText_record_text.getText().toString();

try{
mySQLConnector.insertData3(str_start_date,str_end_date,str_record_text);
Toast.makeText(learningrecord.this, "今日学习记录添加成功", Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(learningrecord.this, "添加失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".learningrecord"
android:orientation="vertical">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:gravity="center">

<TextView
android:layout_width="400dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/day_record"
android:textColor="#000000"
android:textSize="40sp"
android:textStyle="bold">

</TextView>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">

<Button
android:id="@+id/btn_start_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selectStartDate"
android:text="@string/start_date" />

<TextView
android:id="@+id/text_start_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">

<Button
android:id="@+id/btn_end_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selectEndDate"
android:text="@string/end_date" />

<TextView
android:id="@+id/text_end_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />

</LinearLayout>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="50dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">

<EditText
android:id="@+id/Edit_record_text"
android:layout_width="match_parent"
android:layout_height="300dp"
android:inputType="textMultiLine"
android:gravity="top|start"
android:hint="@string/record_text"
android:scrollbars="vertical"
android:scrollbarStyle="insideInset"
android:scrollbarDefaultDelayBeforeFade="1000"
android:fadeScrollbars="false"
android:scrollbarFadeDuration="0"
android:scrollbarSize="4dp"
android:textSize="25sp"
android:background="@null">

</EditText>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">

<Button
android:id="@+id/btn_add_day_record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_day_record"
tools:ignore="ButtonStyle">
</Button>
</LinearLayout>


</LinearLayout>
posted @ 2024-03-20 19:14  为20岁努力  阅读(2)  评论(0编辑  收藏  举报