From 939cb57001582d82f7f77a7596b8fbe7c2c433ca Mon Sep 17 00:00:00 2001 From: pf45q8f3g <850847787@qq.com> Date: Sun, 12 Jan 2025 13:56:51 +0800 Subject: [PATCH] Update DropdownMenu.java --- .../net/micode/notes/ui/DropdownMenu.java | 82 +++++++++++-------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/src/java/net/micode/notes/ui/DropdownMenu.java b/src/java/net/micode/notes/ui/DropdownMenu.java index a5520b2..25d55fb 100644 --- a/src/java/net/micode/notes/ui/DropdownMenu.java +++ b/src/java/net/micode/notes/ui/DropdownMenu.java @@ -1,21 +1,19 @@ /* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权所有 (c) 2010-2011, MiCode 开源社区 (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 + * 本文件授权使用 Apache License, Version 2.0(以下简称“许可证”); + * 除非符合许可证规定,否则不得使用此文件。 + * 您可以从以下网址获取许可证副本: * * 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; @@ -24,42 +22,62 @@ import android.view.View.OnClickListener; import android.widget.Button; import android.widget.PopupMenu; import android.widget.PopupMenu.OnMenuItemClickListener; - + import net.micode.notes.R; - + +/** + * DropdownMenu 类用于创建一个下拉菜单。 + * 它提供了一个按钮,点击按钮时显示一个弹出菜单。 + */ public class DropdownMenu { - private Button mButton; - private PopupMenu mPopupMenu; - //声明一个下拉菜单 - private Menu mMenu; - + private Button mButton; // 按钮控件 + private PopupMenu mPopupMenu; // 弹出菜单 + private Menu mMenu; // 菜单对象 + + /** + * 构造函数,初始化 DropdownMenu。 + * @param context 应用上下文。 + * @param button 按钮控件。 + * @param menuId 菜单资源 ID。 + */ public DropdownMenu(Context context, Button button, int menuId) { mButton = button; - mButton.setBackgroundResource(R.drawable.dropdown_icon); - //设置这个view的背景 + mButton.setBackgroundResource(R.drawable.dropdown_icon); // 设置按钮背景 mPopupMenu = new PopupMenu(context, mButton); mMenu = mPopupMenu.getMenu(); - mPopupMenu.getMenuInflater().inflate(menuId, mMenu); - //MenuInflater是用来实例化Menu目录下的Menu布局文件 - //根据ID来确认menu的内容选项 + mPopupMenu.getMenuInflater().inflate(menuId, mMenu); // 加载菜单资源 mButton.setOnClickListener(new OnClickListener() { + @Override public void onClick(View v) { - mPopupMenu.show(); + mPopupMenu.show(); // 显示弹出菜单 } }); } - + + /** + * 设置菜单项点击监听器。 + * @param listener 菜单项点击监听器。 + */ public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) { if (mPopupMenu != null) { - mPopupMenu.setOnMenuItemClickListener(listener); - }//设置菜单的监听 + mPopupMenu.setOnMenuItemClickListener(listener); // 设置菜单项点击监听器 + } } - + + /** + * 查找菜单项。 + * @param id 菜单项 ID。 + * @return 菜单项。 + */ public MenuItem findItem(int id) { - return mMenu.findItem(id); - }//对于菜单选项的初始化,根据索引搜索菜单需要的选项 - + return mMenu.findItem(id); // 查找菜单项 + } + + /** + * 设置按钮标题。 + * @param title 标题文本。 + */ public void setTitle(CharSequence title) { - mButton.setText(title); - }//布局文件,设置标题 + mButton.setText(title); // 设置按钮标题 + } } \ No newline at end of file