How to programatically change an item in the options menu?

≡放荡痞女 提交于 2019-12-11 06:29:25

问题


When the user clicks on the Menu button I present a few menu items. One of them is a Sign Out button. I want to be able to change the text between "Sign In" and "Sign Out" depending on a boolean value I am tracking. How can I programmatically change the text in the menu?


回答1:


You want to override the onPrepareOptionsMenu function in the Activity. There is a menu parameter passed, you can simply find the menuitem that you want to show/hide (or change the image and text) and return true.

onPrepareOptionsMenu




回答2:


Override/Implement the onPrepareOptionsMenu(...) callback. Change what you add based on your sign-in state.

Alternatively, I haven't tested this but in my mind it works: When you create your menu items, keep a handle to the Sign In/Sign Out item in a class variable, and just update it by calling setTitle(...) as needed.



来源:https://stackoverflow.com/questions/5876109/how-to-programatically-change-an-item-in-the-options-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!