From 88c1f835d8360ff034659cfadcf900d1210e5319 Mon Sep 17 00:00:00 2001 From: LHY <3170597338@qq.com> Date: Tue, 19 Nov 2024 10:29:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DateTimePickerDialog.java | 90 +++++++++++++++++++++++++++++++++++++++ DropdownMenu.java | 70 ++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 DateTimePickerDialog.java create mode 100644 DropdownMenu.java diff --git a/DateTimePickerDialog.java b/DateTimePickerDialog.java new file mode 100644 index 0000000..66dddf4 --- /dev/null +++ b/DateTimePickerDialog.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /*版权和许可证声明,说明了该文件的版权信息以及遵循的许可协议*/ + +package net.micode.notes.ui; + +import java.util.Calendar; + +import net.micode.notes.R; +import net.micode.notes.ui.DateTimePicker; +import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.text.format.DateFormat; +import android.text.format.DateUtils; /*设置了代码的包名,并导入了所需的类和接口*/ + +public class DateTimePickerDialog extends AlertDialog implements OnClickListener { /*定义了一个名为 DateTimePickerDialog 的公共类,它扩展了 AlertDialog 并实现了 OnClickListener 接口。*/ + + private Calendar mDate = Calendar.getInstance(); + private boolean mIs24HourView; + private OnDateTimeSetListener mOnDateTimeSetListener; + private DateTimePicker mDateTimePicker; /*声明了一些私有变量,用于存储日期时间信息、24小时格式标志、监听器和日期时间选择器。 */ + + public interface OnDateTimeSetListener { + void OnDateTimeSet(AlertDialog dialog, long date); + } /*定义了一个内部接口 OnDateTimeSetListener,用于接收日期时间设置事件的回调。 */ + + public DateTimePickerDialog(Context context, long date) { + super(context); /*构造函数初始化父类 AlertDialog */ + mDateTimePicker = new DateTimePicker(context); + setView(mDateTimePicker);/*创建一个 DateTimePicker 实例并设置为对话框的内容视图*/ + mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { + public void onDateTimeChanged(DateTimePicker view, int year, int month, + int dayOfMonth, int hourOfDay, int minute) { + mDate.set(Calendar.YEAR, year); + mDate.set(Calendar.MONTH, month); + mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); + mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); + mDate.set(Calendar.MINUTE, minute); + updateTitle(mDate.getTimeInMillis()); + } + });/*为 DateTimePicker 设置监听器,在日期时间改变时更新内部的日历对象,并调用 updateTitle 方法更新对话框标题 */ + mDate.setTimeInMillis(date); + mDate.set(Calendar.SECOND, 0); + mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); + setButton(context.getString(R.string.datetime_dialog_ok), this); + setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); + set24HourView(DateFormat.is24HourFormat(this.getContext())); + updateTitle(mDate.getTimeInMillis()); + } /*初始化日期时间对象,并设置到 DateTimePicker 中。设置对话框的确认和取消按钮,并根据系统设置决定是否使用24小时制显示时间。最后更新对话框标题。 */ + + public void set24HourView(boolean is24HourView) { + mIs24HourView = is24HourView; + } /*设置是否使用24小时制显示时间的方法*/ + + public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { + mOnDateTimeSetListener = callBack; + } /*设置监听器的方法*/ + + private void updateTitle(long date) { + int flag = + DateUtils.FORMAT_SHOW_YEAR | + DateUtils.FORMAT_SHOW_DATE | + DateUtils.FORMAT_SHOW_TIME; + flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR; + setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); + } /*更新对话框标题的方法,根据当前日期时间和格式化标志来显示日期时间。*/ + + public void onClick(DialogInterface arg0, int arg1) { + if (mOnDateTimeSetListener != null) { + mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); + } + }/*实现 OnClickListener 接口的 onClick 方法,当点击对话框上的按钮时,如果设置了监听器,则调用其 OnDateTimeSet 方法*/ + +}/*类定义结束*/ \ No newline at end of file diff --git a/DropdownMenu.java b/DropdownMenu.java new file mode 100644 index 0000000..e0db7db --- /dev/null +++ b/DropdownMenu.java @@ -0,0 +1,70 @@ + + +/*这段代码展示了如何在 Android 应用程序中实现一个带有弹出菜单的下拉按钮。 +这个 DropdownMenu 类封装了创建一个带有菜单的按钮所需的所有步骤。 */ + + + +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.ui; + +import android.content.Context; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.PopupMenu; +import android.widget.PopupMenu.OnMenuItemClickListener; + +import net.micode.notes.R; + +public class DropdownMenu { /*定义了一个名为 DropdownMenu 的公共类*/ + private Button mButton; + private PopupMenu mPopupMenu; + private Menu mMenu; /*声明了一些私有变量,分别代表按钮、弹出菜单和菜单对象。*/ + + public DropdownMenu(Context context, Button button, int menuId) { + mButton = button; + mButton.setBackgroundResource(R.drawable.dropdown_icon); /*构造函数初始化成员变量,设置按钮的背景图片*/ + mPopupMenu = new PopupMenu(context, mButton); + mMenu = mPopupMenu.getMenu(); + mPopupMenu.getMenuInflater().inflate(menuId, mMenu);/*创建 PopupMenu 实例,获取菜单对象,并通过提供的菜单资源 ID 充气菜单*/ + mButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + mPopupMenu.show(); + } + }); + } /*为按钮设置点击监听器,点击时显示弹出菜单*/ + + public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) { + if (mPopupMenu != null) { + mPopupMenu.setOnMenuItemClickListener(listener); + } + } /*设置弹出菜单项点击监听器的方法,允许外部设置监听器以便处理菜单项点击事件*/ + + public MenuItem findItem(int id) { + return mMenu.findItem(id); + } /*提供一个方法用于根据资源 ID 查找菜单项*/ + + public void setTitle(CharSequence title) { + mButton.setText(title); + }/*设置按钮文本的方法*/ +} + + /*这个 DropdownMenu 类使得在应用程序中添加带有弹出菜单的按钮变得更加容易,通过构造函数传入上下文、按钮和菜单资源 ID,就可以轻松地创建和使用带有菜单的按钮。*/ \ No newline at end of file