Android 上下文菜单 ActionMode
程序员文章站
2022-11-06 19:53:26
public class MainActivity extends Activity { private Button button; private ActionMode actionMode; @Override protected void onCreate(Bundle savedInsta... ......
public class mainactivity extends activity { private button button; private actionmode actionmode; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button) findviewbyid(r.id.button); button.setonlongclicklistener(new onlongclicklistener() { @override public boolean onlongclick(view v) { // todo auto-generated method stub if (actionmode != null) { return false; } else { actionmode = startactionmode(actioncallback); v.setselected(true); return true; } } }); } private actionmode.callback actioncallback = new actionmode.callback() { @override public boolean onprepareactionmode(actionmode mode, menu menu) { // todo auto-generated method stub return false; } @override public void ondestroyactionmode(actionmode mode) { // todo auto-generated method stub } // 从xml菜单中加载菜单 @override public boolean oncreateactionmode(actionmode mode, menu menu) { // todo auto-generated method stub menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); return true; } // 菜单被点击 @override public boolean onactionitemclicked(actionmode mode, menuitem item) { // todo auto-generated method stub switch (item.getitemid()) { case r.id.action_add: toast.maketext(mainactivity.this, "添加被点击", 1).show(); break; case r.id.action_delete: toast.maketext(mainactivity.this, "删除被点击", 1).show(); break; default: break; } return false; } }; }
长按显示菜单
选择菜单
推荐阅读
-
Android组件之DrawerLayout实现抽屉菜单
-
Android App中DrawerLayout抽屉效果的菜单编写实例,drawerlayout上下抽屉_PHP教程
-
详解Xamarin.Android 利用Fragment实现底部菜单
-
Android中选项菜单(OptionMenu)的创建方法
-
Android利用WindowManager生成悬浮按钮及悬浮菜单
-
Android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能
-
Android自定义控件实现底部菜单(下)
-
Android自定义控件实现底部菜单(上)
-
Android底部菜单简单应用
-
Android中软键盘弹出时底部菜单上移问题