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.
65 lines
2.8 KiB
65 lines
2.8 KiB
/*
|
|
* 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.
|
|
*/
|
|
|
|
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; // 存储菜单对象
|
|
|
|
// 构造函数,接受上下文、按钮和菜单资源 ID
|
|
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); // 设置菜单项点击监听器
|
|
}
|
|
}
|
|
|
|
// 根据 ID 查找菜单项
|
|
public MenuItem findItem(int id) {
|
|
return mMenu.findItem(id); // 根据 ID 返回菜单项
|
|
}
|
|
|
|
// 设置按钮的标题
|
|
public void setTitle(CharSequence title) {
|
|
mButton.setText(title); // 更新按钮文本为给定的标题
|
|
}
|
|
} |