简体   繁体   English

动态禁用ActionBar按钮

[英]Disable ActionBar button dynamically

I want to disable the "attachment" button when there is no attachment is the page user loads. 当页面没有加载时,我想禁用“附件”按钮。 I searched every stack overflow suggestion but I think I'm getting something wrong. 我搜索了每个堆栈溢出建议,但我认为我做错了什么。 What I did is: 我所做的是:

  • set a boolean to find if there are attachments. 设置一个布尔值以查找是否有附件。
  • call invalidateOptionsMenu(); 调用invalidateOptionsMenu();
  • try to disable the action bar button 尝试禁用操作栏按钮

     @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.inpostmenu, menu); return super.onCreateOptionsMenu(menu); if (attachments) { menu.findItem(R.id.attach).setEnabled(!attachments); } } 

I get an Unreachable Statement error on 我在收到“ 无法到达的语句”错误

menu.findItem(R.id.attach).setEnabled(!attachments); menu.findItem(R.id.attach).setEnabled(附件!);

What is wrong with my code? 我的代码有什么问题?

The Unreachable Statement is because you're trying to do stuff after the function already returned. Unreachable Statement是因为您正在尝试在函数已返回之后做一些事情。 That code can never be reached. 该代码永远无法到达。 Try: 尝试:

menu.findItem(R.id.attach).setEnabled(attachments);
return super.onCreateOptionsMenu(menu);

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

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