菜单的操作
2023-01-24 09:16:15 最后更新
v3教程,v5观望即可
有没有发现menu实际用的挺多的
NavigationView, BottomNavigationView, Toolbar......
这将会在我后面发的教程用上
先定个菜单的基础操作
先创建事件.mjava
//这里用了残页代码
//导入类
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public static MenuItem 菜单点击事件(MenuItem 菜单项)
{

 MenuItem mi =菜单项.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
  {
   public boolean onMenuItemClick(MenuItem 项) {
    //菜单项被点击事件
    //tw(项.getTitle() + "被点击了");
    switch(项.getTitle())
    {
     //你每添加一个菜单就要在这加个case 标题
     case "1菜单":        
      tw(1);
      break;
     case "2菜单":
      tw(2)
      break;
    //以此类推
    }
    return true;
   }
 });
 return mi;
}
回到你的放代码的地方(推荐在载入完毕事件)
//得先弄到Menu对象,这里以NavigationView为例
//还得有个NavigationView,先不管它
java(menu,对象,"android.support.design.widget.NavigationView.getMenu")
//添加普通菜单
java(item,menu,"android.view.Menu.add","CharSequence","1菜单")
//设置图标,可以省去这步
sbp("图标路径",图像)
javanew(icon,"android.graphics.drawable.BitmapDrawable","android.graphics.Bitmap",图像)
java(item,item,"android.view.MenuItem.setIcon","android.graphics.drawable.Drawable",icon)
//设置单击事件,也可以省略(那菜单项就成摆设了)
call(item,"mjava","事件.菜单点击事件",item)
//设置是否可选,在mjava那设置判断开就关,关就开
java(item,item,"android.view.MenuItem.setCheckable","boolean",true)
//添加一个子菜单
java(subitems,menu,"android.view.Menu.addSubMenu","CharSequence","子菜单")
//子菜单内可以用正常方法添加菜单项
java(item,subitems,"android.view.Menu.add","CharSequence","2菜单")
//这就不多说了
sbp("路径",图像)
javanew(icon,"android.graphics.drawable.BitmapDrawable","android.graphics.Bitmap",图像)
java(item,item,"android.view.MenuItem.setIcon","android.graphics.drawable.Drawable",icon)
//设置是否可以点击,false就是灰色不可用java(item,item,"android.view.MenuItem.setEnabled","boolean",false)
call(null,"mjava","事件.菜单点击事件",item)
//以上代码大部分都可以省略
//设置菜单完就不用管了,不用再setMenu
.作者:MC的wither菌