AndroidAnnotations——Handling options menu处理选项菜单

Handling options menu

Since AndroidAnnotations 2.2


You can easily add options menu support in your activities, with the  @OptionsMenu and @OptionsItem annotations.
你可以使用   @OptionsMenu   @OptionsItem   注解更方便地为你的activities添加选项菜单支持。

  • @OptionsMenu lets you specify the menu resource to use
  • @OptionsMenu  让你指定要使用的menu资源
  • @OptionsItem marks methods that receives menu selection events
  • @OptionsItem  标记接收菜单选择事件的方法。

Both annotations can be used independently.两个注解都可以独立使用。

Here is a simple example on how to use them:下面是一个简单的例子:

@EActivity
@OptionsMenu(R.menu.my_menu)
public class MyActivity extends Activity {

        @OptionMenuItem
        MenuItem menuSearch;

        @OptionsItem(R.id.menuShare)
        void myMethod() {
          // You can specify the ID in the annotation, or use the naming convention
        }

        @OptionsItem
        void homeSelected() {
          // home was selected in the action bar
          // The "Selected" keyword is optional
        }

        @OptionsItem
        boolean menuSearch() {
          menuSearch.setVisible(false);
          // menuSearch was selected
          // the return type may be void or boolean (false to allow normal menu processing to proceed, true to consume it here)
          return true;
        }

        @OptionsItem({ R.id.menu_search, R.id.menu_delete })
        void multipleMenuItems() {
          // You can specify multiple menu item IDs in @OptionsItem
        }

        @OptionsItem
        void menu_add(MenuItem item) {
          // You can add a MenuItem parameter to access it
        }
}

Injecting Menu items

Since AndroidAnnotations 3.0

@OptionMenuItem can be used to inject a MenuItem in an attribute.@OptionMenuItem 用来注入一个菜单项到属性中。

Be careful : Injected menu items can't be used in @AfterInject nor @AfterViews annotated methods, because of Android activity lifecycle. More information are available here注意:因为Android activity的生命周期,注入菜单项不可以使用或者注解方法。更多信息请看这里

Multiple Options Menu

Since AndroidAnnotations 2.7

You can combine multiple xml menus with @OptionsMenu:你使用 @OptionsMenu注解可以组合多个xml菜单:

@EActivity
@OptionsMenu({R.menu.my_menu1, R.menu.my_menu2})
public class MyActivity extends Activity {

}

Fragment support

Since AndroidAnnotations 2.7

You can also use @OptionsMenu and @OptionsItem in Fragments:你也可以在Fragments中使用@OptionsMenu 和@OptionsItem 

@EFragment
@OptionsMenu(R.menu.my_fragment_menu)
public class MyFragment extends Fragment {

        @OptionsItem
        void menuRefreshSelected() {
        }
        
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值