简体   繁体   English

片段xamarin android中未调用OnOptionsItemSelected

[英]OnOptionsItemSelected is not called in fragment xamarin android

In OnCreate: SetHasOptionsMenu (true); 在OnCreate中:SetHasOptionsMenu(true);

public override bool OnOptionsItemSelected(IMenuItem menu)
        {
            menu.SetChecked (true);
            switch (menu.ItemId) {
            case Resource.Id.selecta:
                Toast.MakeText (Application.Context, "Top", ToastLength.Long);

                return true;
            case Resource.Id.selectb:
                Toast.MakeText (Application.Context, "New", ToastLength.Long);

                return true;
            }
            return base.OnOptionsItemSelected (menu);

        }
        public override void OnCreateOptionsMenu(Android.Views.IMenu menu,MenuInflater inflater)
        {
            //MenuInflater.Inflate (Resource.Menu.Action_menu, menu);
            //menu.Clear();
            inflater.Inflate(Resource.Menu.Action_menu,menu);

            base.OnCreateOptionsMenu (menu,inflater);
        }

When the item of checkbox is checked . 选中复选框的项目时。 OnOptionsItemSelected is not called. 不调用OnOptionsItemSelected。

I guess you have a implementation of OnOptionsItemSelected in your Activity too. 我想您在Activity中也有OnOptionsItemSelected的实现。

Either remove it or return false if you want to give Fragment a chance to handle this event. 如果要给Fragment处理该事件的机会,请删除它或返回false

The Activity gets a chance to handle this event first. 该活动有机会首先处理此事件。 So onOptionsItemSelected() is called first on the Activity before the method is called for the fragment. 因此,在为片段调用方法之前,先在Activity上调用onOptionsItemSelected() Only if it is not handled by the Activity then the fragment has a chance to handle it. 仅当Activity未处理该片段时,该片段才有机会处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM