2024/3/19

所花时间:4小时

代码行:200行

博客量: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 setgoal extends AppCompatActivity {

private TextView tvmdate;
private EditText mEditText_goaltext;
private Button mButton_addgoal;

private MySQLConnector mySQLConnector;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setgoal);
mEditText_goaltext=findViewById(R.id.Edit_goal_text);
tvmdate=findViewById(R.id.text_mdate);
mButton_addgoal=findViewById(R.id.btn_add_goal);

mySQLConnector=new MySQLConnector();

mButton_addgoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertData1();
/*Toast.makeText(setgoal.this, "添加本周目标成功", Toast.LENGTH_LONG).show();*/
}
});
}

public void selectmDate(View view) {
showDateTimePicker("mdate");
}

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(setgoal.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(setgoal.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("mdate")) {
// Set selected start date/time
//startDateTextView.setText(selectedDateTime);
updateSelectedDate(selectedDateTime,type);
}
}
}, hour, minute, true);

timePickerDialog.show();
}
private void updateSelectedDate(String selectedDate, String type) {
if (type.equals("mdate")) {
tvmdate.setText( selectedDate);
}
}
private void insertData1(){
String str_mdate=tvmdate.getText().toString();
String str_goal_text=mEditText_goaltext.getText().toString();

try{
mySQLConnector.insertData1(str_goal_text,str_mdate);
Toast.makeText(setgoal.this, "添加本周目标成功", Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(setgoal.this, "添加本周目标失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
posted @ 2024-03-19 17:11  为20岁努力  阅读(2)  评论(0编辑  收藏  举报