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.
123456/java/net/micode/notes/ui/DropdownMenu.java

86 lines
5.5 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.
* 总体分析
这段 Java 代码定义了一个名为DropdownMenu的类其主要功能是创建一个带有下拉菜单功能的组件。它基于安卓系统中的Button和PopupMenu来实现将Button作为触发下拉菜单显示的入口通过设置相关的监听器、加载菜单资源等操作方便在安卓应用界面中构建出一个能展示多种操作选项通过下拉菜单形式呈现的交互组件并且提供了查找菜单项、设置按钮标题等实用的方法增强了该组件在实际使用中的灵活性和可定制性。
函数分析
构造函数
DropdownMenu(Context context, Button button, int menuId)
所属类DropdownMenu
功能:
首先接收传入的Context用于获取系统资源等操作、Button作为触发下拉菜单显示的按钮控件以及menuId用于指定要加载的菜单资源的ID这三个参数。
将传入的Button实例赋值给类内部的mButton变量并通过setBackgroundResource方法为其设置背景资源这里使用R.drawable.dropdown_icon指定的图标资源应该是一个代表下拉箭头之类的图标用于在视觉上提示用户该按钮可展开下拉菜单
创建一个PopupMenu实例将传入的Context和Button作为参数传入使其与按钮关联起来方便后续基于按钮位置来显示下拉菜单然后获取这个PopupMenu的Menu对象并赋值给mMenu变量用于后续操作菜单中的具体菜单项等。
通过getMenuInflater方法获取菜单填充器并调用inflate方法按照传入的menuId将对应的菜单资源加载到mMenu中完成菜单内容的初始化。
最后为mButton设置点击监听器在监听器的onClick方法中当按钮被点击时调用mPopupMenu.show方法来显示关联的下拉菜单实现点击按钮弹出下拉菜单的核心功能。
设置监听器相关函数
setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener)
所属类DropdownMenu
功能接收一个实现了OnMenuItemClickListener接口的监听器对象作为参数用于设置下拉菜单中菜单项被点击时的监听器。在函数内部先判断mPopupMenu是否为null避免空指针异常如果不为null则通过mPopupMenu.setOnMenuItemClickListener方法将传入的监听器设置给PopupMenu这样当用户点击下拉菜单中的某个菜单项时对应的监听器中的回调方法就会被触发外部代码可以在回调方法中编写处理具体菜单项点击逻辑的代码例如执行不同的操作、跳转到不同的界面等。
查找菜单项函数
findItem(int id)
所属类DropdownMenu
功能接收一个表示菜单项ID的整数参数通过调用mMenu.findItem方法在内部的Menu对象之前在构造函数中加载了菜单资源的那个Menu中查找对应的菜单项返回找到的MenuItem对象。外部代码可以利用这个函数来获取特定ID的菜单项进而对其进行一些操作比如获取菜单项的文本、设置菜单项是否可用等方便根据具体的业务需求对菜单项进行定制化处理。
设置标题函数
setTitle(CharSequence title)
所属类DropdownMenu
功能接收一个CharSequence类型通常可以是字符串等文本内容的参数用于设置关联的Button的文本标题。在函数内部直接通过mButton.setText方法将传入的标题文本设置给按钮改变按钮上显示的文字内容从视觉上为用户提供更明确的提示信息比如可以根据当前菜单的功能或者所在的页面内容等设置合适的按钮标题增强界面的可读性和易用性。
*/
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 {
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);
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);
}
public void setTitle(CharSequence title) {
mButton.setText(title);
}
}