|
|
/*
|
|
|
* 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类用于创建和管理一个下拉菜单。
|
|
|
* 该类封装了一个Button和一个PopupMenu,通过点击Button来显示下拉菜单。
|
|
|
*/
|
|
|
package net.micode.notes.ui; // 定义包名
|
|
|
|
|
|
import android.content.Context; // 导入Context类,用于获取上下文
|
|
|
import android.view.Menu; // 导入Menu类,用于管理菜单项
|
|
|
import android.view.MenuItem; // 导入MenuItem类,用于表示菜单项
|
|
|
import android.view.View; // 导入View类,用于处理视图
|
|
|
import android.view.View.OnClickListener; // 导入OnClickListener接口,用于处理点击事件
|
|
|
import android.widget.Button; // 导入Button类,用于创建按钮
|
|
|
import android.widget.PopupMenu; // 导入PopupMenu类,用于创建弹出菜单
|
|
|
import android.widget.PopupMenu.OnMenuItemClickListener; // 导入OnMenuItemClickListener接口,用于处理菜单项点击事件
|
|
|
|
|
|
import net.micode.notes.R; // 导入资源类,用于访问资源
|
|
|
|
|
|
public class DropdownMenu { // 定义DropdownMenu类
|
|
|
private Button mButton; // 弹出下拉菜单的按钮
|
|
|
private PopupMenu mPopupMenu; // 弹出的下拉菜单
|
|
|
private Menu mMenu; // 下拉菜单的项目集合
|
|
|
|
|
|
/**
|
|
|
* DropdownMenu的构造函数。
|
|
|
*
|
|
|
* @param context 上下文对象,通常是指Activity。
|
|
|
* @param button 用于触发下拉菜单显示的按钮。
|
|
|
* @param menuId 菜单资源ID,用于加载下拉菜单的项目。
|
|
|
*/
|
|
|
public DropdownMenu(Context context, Button button, int menuId) {
|
|
|
mButton = button; // 初始化按钮
|
|
|
mButton.setBackgroundResource(R.drawable.dropdown_icon); // 设置按钮背景为下拉图标
|
|
|
mPopupMenu = new PopupMenu(context, mButton); // 创建PopupMenu实例,锚点为按钮
|
|
|
mMenu = mPopupMenu.getMenu(); // 获取菜单项的集合
|
|
|
mPopupMenu.getMenuInflater().inflate(menuId, mMenu); // 加载菜单项
|
|
|
// 设置按钮点击事件,点击后显示下拉菜单
|
|
|
mButton.setOnClickListener(new OnClickListener() {
|
|
|
public void onClick(View v) {
|
|
|
mPopupMenu.show(); // 显示下拉菜单
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置下拉菜单项的点击事件监听器。
|
|
|
*
|
|
|
* @param listener PopupMenu的OnMenuItemClickListener,用于监听菜单项的点击事件。
|
|
|
*/
|
|
|
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
|
|
|
if (mPopupMenu != null) {
|
|
|
mPopupMenu.setOnMenuItemClickListener(listener); // 设置菜单项点击监听器
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据ID查找菜单项。
|
|
|
*
|
|
|
* @param id 菜单项的ID。
|
|
|
* @return 返回找到的MenuItem对象,如果未找到则返回null。
|
|
|
*/
|
|
|
public MenuItem findItem(int id) {
|
|
|
return mMenu.findItem(id); // 根据ID查找菜单项
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置按钮的标题。
|
|
|
*
|
|
|
* @param title 按钮要显示的标题。
|
|
|
*/
|
|
|
public void setTitle(CharSequence title) {
|
|
|
mButton.setText(title); // 设置按钮显示的文本
|
|
|
}
|
|
|
}
|