Add Switch widget to ActionBar and respond to change event

前端 未结 2 2048
忘掉有多难
忘掉有多难 2021-02-06 09:02

Can I know how to add Switch widget in ActionBar and handle the click event or toggle change event.

For now I can inflate the Switch in ActionBar but unable to respond t

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 09:46

    For those of you using Xamarin. This is the translated version of adneal's answer:

    private Switch _actionViewSwitch;
    
    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.main_activity_actions, menu);
    
        var menuItem = menu.FindItem(Resource.Id.toggleservice);
        _actionViewSwitch = (Switch) menuItem.ActionView;
        _actionViewSwitch.CheckedChange += ActionViewOnCheckedChange;
    
        return base.OnCreateOptionsMenu(menu);
    }
    
    private void ActionViewOnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs checkedChangeEventArgs)
    {
        // ToDo: stuff that happens when switch gets checked.
    }
    

提交回复
热议问题