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.
mi-note-project/doc/康江龙注释的代码/DropdownMenu.java

65 lines
2.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.
*/
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);//实例按钮设置view的背景
mPopupMenu = new PopupMenu(context, mButton);//弹出式菜单实例化实现
mMenu = mPopupMenu.getMenu();
//getMenuInflater用以实例化menu目录下的menu布局文件并根据ID确定menu内容
mPopupMenu.getMenuInflater().inflate(menuId, mMenu);//弹出式菜单视图
mButton.setOnClickListener(new OnClickListener() {
//设置监听
public void onClick(View v) {
mPopupMenu.show();
}//下拉菜单,显示
});
}
//首先是公有类DropdownMenu的私有数据成员申明,其中包括了对象按钮mButton,菜单对象mMenu和弹出菜单控件对象mPopupMenu
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);
}//设置标题
}