|
|
|
@ -27,35 +27,36 @@ 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);
|
|
|
|
|
public class DropdownMenu {// 按钮,用于触发下拉菜单的显示
|
|
|
|
|
private Button mButton;; // 弹出菜单,包含下拉菜单项
|
|
|
|
|
private PopupMenu mPopupMenu;// 菜单,用于管理下拉菜单项
|
|
|
|
|
private Menu mMenu;//构造方法,初始化下拉菜单
|
|
|
|
|
|
|
|
|
|
public DropdownMenu(Context context, Button button, int menuId) {// 上下文,用于创建弹出菜单; 按钮,用于触发下拉菜单的显示 ;菜单资源ID,定义下拉菜单项的布局
|
|
|
|
|
mButton = button;// 设置按钮的背景为下拉菜单图标
|
|
|
|
|
mButton.setBackgroundResource(R.drawable.dropdown_icon);// 创建弹出菜单
|
|
|
|
|
mPopupMenu = new PopupMenu(context, mButton); // 获取弹出菜单的菜单对象
|
|
|
|
|
mMenu = mPopupMenu.getMenu();// 从资源文件中 inflate 菜单项到弹出菜单
|
|
|
|
|
mPopupMenu.getMenuInflater().inflate(menuId, mMenu);// 设置按钮点击监听器,当按钮被点击时显示下拉菜单
|
|
|
|
|
mButton.setOnClickListener(new OnClickListener() {
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
mPopupMenu.show();
|
|
|
|
|
mPopupMenu.show();// 设置下拉菜单项点击监听器 * * @param listener 下拉菜单项点击监听器,当菜单项被点击时回调 */
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
|
|
|
|
|
if (mPopupMenu != null) {
|
|
|
|
|
mPopupMenu.setOnMenuItemClickListener(listener);
|
|
|
|
|
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {//确保弹出菜单不为空
|
|
|
|
|
if (mPopupMenu != null) {// 设置弹出菜单的菜单项点击监听器
|
|
|
|
|
mPopupMenu.setOnMenuItemClickListener(listener);//查找菜单项 * * @param id 菜单项的ID * @return 找到的菜单项,如果没有找到则返回null */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MenuItem findItem(int id) {
|
|
|
|
|
public MenuItem findItem(int id) {// 在菜单中查找指定ID的菜单项 return mMenu.findItem(id); } /** * 设置按钮的标题 * * @param title 按钮的标题
|
|
|
|
|
public MenuItem findItem(int id) {// 在菜单中查找指定ID的菜单项 return mMenu.findItem(id); } /** * 设置按钮的标题 * * @param title 按钮的标题
|
|
|
|
|
return mMenu.findItem(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTitle(CharSequence title) {
|
|
|
|
|
public void setTitle(CharSequence title) {// 设置按钮的文本内容
|
|
|
|
|
mButton.setText(title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|