parent
66a2de1006
commit
ae65aeec34
Binary file not shown.
@ -0,0 +1,76 @@
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private TextView mSelectedDateTimeText;
|
||||
private Button mShowPickerButton;
|
||||
private SimpleDateFormat mDateFormat;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
mSelectedDateTimeText = findViewById(R.id.selected_date_time);
|
||||
mShowPickerButton = findViewById(R.id.show_picker_button);
|
||||
mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
|
||||
|
||||
mShowPickerButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showDateTimePicker();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showDateTimePicker() {
|
||||
// 获取当前时间作为初始值
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
int month = calendar.get(Calendar.MONTH);
|
||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
int minute = calendar.get(Calendar.MINUTE);
|
||||
|
||||
// 创建并配置DateTimePickerDialog
|
||||
DateTimePickerDialog dialog = new DateTimePickerDialog(
|
||||
this,
|
||||
(dateTimePickerDialog, year1, month1, day1, hourOfDay, minute1) -> {
|
||||
// 处理选择结果
|
||||
Calendar selectedCalendar = Calendar.getInstance();
|
||||
selectedCalendar.set(year1, month1, day1, hourOfDay, minute1);
|
||||
Date selectedDate = selectedCalendar.getTime();
|
||||
|
||||
// 显示选择结果
|
||||
mSelectedDateTimeText.setText("选择的日期时间: " + mDateFormat.format(selectedDate));
|
||||
Toast.makeText(MainActivity.this, "选择了: " + mDateFormat.format(selectedDate), Toast.LENGTH_SHORT).show();
|
||||
},
|
||||
year, month, day, hour, minute
|
||||
);
|
||||
|
||||
// 设置最小日期为当前日期
|
||||
dialog.setMinDate(new Date());
|
||||
|
||||
// 设置最大日期为30天后
|
||||
Calendar maxCalendar = Calendar.getInstance();
|
||||
maxCalendar.add(Calendar.DAY_OF_YEAR, 30);
|
||||
dialog.setMaxDate(maxCalendar.getTime());
|
||||
|
||||
// 设置为24小时制
|
||||
dialog.setIs24HourView(true);
|
||||
|
||||
// 显示对话框
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
<?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:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/selected_date_time"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="选择的日期时间: 未选择"
|
||||
android:textSize="18sp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:gravity="center" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/show_picker_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="显示日期时间选择器"
|
||||
android:textSize="16sp"
|
||||
android:padding="12dp" />
|
||||
|
||||
</LinearLayout>
|
||||
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/date_time_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<!-- 日期选择器 -->
|
||||
<CalendarView
|
||||
android:id="@+id/calendar_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
<!-- 时间选择器 -->
|
||||
<TimePicker
|
||||
android:id="@+id/time_picker"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="date_time_picker_title">选择日期时间</string>
|
||||
<string name="ok">确定</string>
|
||||
<string name="cancel">取消</string>
|
||||
<string name="date_out_of_range">所选日期超出范围</string>
|
||||
</resources>
|
||||
Loading…
Reference in new issue