|
|
/*
|
|
|
* 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;//导入ui包
|
|
|
|
|
|
import android.content.Context;//引入需要使用的Context、Menu等包
|
|
|
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() {//当我们点击mButton时,会弹出菜单
|
|
|
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);
|
|
|
}//根据索引搜索菜单需要的选项
|
|
|
}
|