You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git/java/net/micode/notes/ui/DropdownMenu.java

172 lines
5.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
// DropdownMenu.java - 下拉菜单包装类
// 主要功能将Button和PopupMenu组合封装提供简洁的下拉菜单实现
package net.micode.notes.ui;
// ======================= 导入区域 =======================
// Android上下文
import android.content.Context; // 上下文
// Android菜单相关
import android.view.Menu; // 菜单接口
import android.view.MenuItem; // 菜单项
// Android视图相关
import android.view.View; // 视图基类
import android.view.View.OnClickListener; // 点击监听器
// Android控件
import android.widget.Button; // 按钮控件
import android.widget.PopupMenu; // 弹出菜单控件
import android.widget.PopupMenu.OnMenuItemClickListener; // 弹出菜单项点击监听器
// 应用内部资源
import net.micode.notes.R; // 资源文件R类
// ======================= 下拉菜单包装类 =======================
/**
* DropdownMenu - 下拉菜单包装类
* 功能将Button控件与PopupMenu组合创建下拉菜单效果
* 使用方式将Button转换为下拉菜单按钮点击显示菜单
* 设计模式外观模式Facade简化PopupMenu的使用
*/
public class DropdownMenu {
// ======================= 成员变量 =======================
/** 按钮控件 - 显示为下拉菜单的触发器按钮 */
private Button mButton;
/** 弹出菜单 - 实际的PopupMenu控件包含菜单项 */
private PopupMenu mPopupMenu;
/** 菜单对象 - 对应PopupMenu中的菜单用于查找菜单项 */
private Menu mMenu;
// ======================= 构造函数 =======================
/**
* 构造函数
* 初始化下拉菜单将普通Button转换为下拉菜单按钮
*
* 工作流程:
* 1. 保存Button引用
* 2. 设置Button背景为下拉箭头图标
* 3. 创建PopupMenu并关联到Button
* 4. 获取Menu对象
* 5. 加载菜单布局
* 6. 设置Button点击事件显示菜单
*
* @param context 上下文用于创建PopupMenu
* @param button 按钮控件,将转换为下拉菜单触发器
* @param menuId 菜单资源ID对应res/menu目录下的XML文件
*
* 示例:
* new DropdownMenu(context, button, R.menu.note_operation_menu);
*/
public DropdownMenu(Context context, Button button, int menuId) {
// 1. 保存Button引用
mButton = button;
// 2. 设置Button背景为下拉箭头图标
// R.drawable.dropdown_icon: 下拉箭头图标
// 使Button看起来像下拉菜单按钮
mButton.setBackgroundResource(R.drawable.dropdown_icon);
// 3. 创建PopupMenu并关联到Button
// 第二个参数是锚点视图菜单会显示在Button下方
mPopupMenu = new PopupMenu(context, mButton);
// 4. 获取PopupMenu的Menu对象
mMenu = mPopupMenu.getMenu();
// 5. 加载菜单布局文件
// 从XML资源文件加载菜单项到Menu对象
mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
// 6. 设置Button点击事件
mButton.setOnClickListener(new OnClickListener() {
/**
* Button点击事件处理
* 点击Button时显示弹出菜单
* @param v 被点击的Button视图
*/
public void onClick(View v) {
mPopupMenu.show(); // 显示弹出菜单
}
});
}
// ======================= 菜单项点击监听器设置 =======================
/**
* 设置下拉菜单项点击监听器
* 当用户点击菜单项时触发
*
* @param listener 菜单项点击监听器
* 实现OnMenuItemClickListener接口
*
* 使用示例:
* dropdownMenu.setOnDropdownMenuItemClickListener(new OnMenuItemClickListener() {
* @Override
* public boolean onMenuItemClick(MenuItem item) {
* // 处理菜单项点击
* return true;
* }
* });
*/
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
// 安全检查确保PopupMenu已创建
if (mPopupMenu != null) {
mPopupMenu.setOnMenuItemClickListener(listener);
}
}
// ======================= 菜单项查找 =======================
/**
* 根据ID查找菜单项
* 用于动态修改菜单项属性(如启用状态、标题、图标等)
*
* @param id 菜单项资源ID
* @return 找到的MenuItem对象未找到返回null
*
* 使用示例:
* MenuItem item = dropdownMenu.findItem(R.id.menu_delete);
* if (item != null) {
* item.setEnabled(false); // 禁用删除菜单项
* }
*/
public MenuItem findItem(int id) {
return mMenu.findItem(id);
}
// ======================= 按钮标题设置 =======================
/**
* 设置按钮标题文本
* 用于动态更新下拉按钮显示的文字
*
* @param title 按钮标题文本
*
* 使用示例:
* dropdownMenu.setTitle("操作菜单");
* 或
* dropdownMenu.setTitle(getString(R.string.menu_title));
*/
public void setTitle(CharSequence title) {
mButton.setText(title);
}
}