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.

114 lines
4.4 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.

java
/*
* 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
*
* <url id="cu1jo4prqpm9v23mk6p0" type="url" status="parsed" title="Apache License, Version 2.0" wc="10467">http://www.apache.org/licenses/LICENSE-2.0</url>
*
* 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;
// 导入资源文件,方便访问项目中的资源
/**
* DropdownMenu 类用于创建和管理一个下拉菜单。
* 它封装了下拉菜单的创建、显示和事件处理逻辑,提供了一个简单的接口来使用下拉菜单。
*/
public class DropdownMenu {
// 定义一个名为 DropdownMenu 的公共类
private Button mButton;
// 定义一个 Button 类型的私有成员变量 mButton用于存储下拉菜单按钮
private PopupMenu mPopupMenu;
// 定义一个 PopupMenu 类型的私有成员变量 mPopupMenu用于存储下拉菜单
private Menu mMenu;
// 定义一个 Menu 类型的私有成员变量 mMenu用于存储下拉菜单中的菜单项
/**
* 构造方法,用于创建 DropdownMenu 对象。
*
* @param context 上下文对象,用于创建 PopupMenu
* @param button 按钮对象,用于触发下拉菜单的显示
* @param menuId 菜单资源 ID用于填充下拉菜单的项
*/
public DropdownMenu(Context context, Button button, int menuId) {
// 定义一个构造方法,用于创建 DropdownMenu 对象
mButton = button;
// 将传入的按钮赋值给 mButton
mButton.setBackgroundResource(R.drawable.dropdown_icon);
// 为按钮设置背景资源,使用 R.drawable.dropdown_icon
mPopupMenu = new PopupMenu(context, mButton);
// 创建一个 PopupMenu 对象,并传入上下文和按钮作为参数
mMenu = mPopupMenu.getMenu();
// 获取 PopupMenu 对象的菜单,并赋值给 mMenu
mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
// 使用菜单填充器将传入的菜单资源 ID 填充到菜单中
mButton.setOnClickListener(new OnClickListener() {
// 为按钮设置点击事件监听器
public void onClick(View v) {
// 当按钮被点击时
mPopupMenu.show();
// 显示下拉菜单
}
});
}
/**
* 设置下拉菜单项点击事件监听器。
*
* @param listener 菜单项点击事件监听器
*/
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
// 定义一个方法,用于设置下拉菜单项点击事件监听器
if (mPopupMenu != null) {
// 如果 mPopupMenu 不为空
mPopupMenu.setOnMenuItemClickListener(listener);
// 为 mPopupMenu 设置点击事件监听器
}
}
/**
* 查找菜单项。
*
* @param id 菜单项的 ID
* @return 找到的菜单项,如果未找到则返回 null
*/
public MenuItem findItem(int id) {
// 定义一个方法,用于查找菜单项
return mMenu.findItem(id);
// 调用 mMenu 的 findItem 方法查找并返回指定 ID 的菜单项
}
/**
* 设置按钮的标题。
*
* @param title 按钮的标题文本
*/
public void setTitle(CharSequence title) {
// 定义一个方法,用于设置按钮的标题
mButton.setText(title);
// 调用 mButton 的 setText 方法设置按钮的文本内容
}
}
// DropdownMenu 类的结束