使用 .setEnabled() 启用/禁用选项菜单按钮?
我试图做这样的事情:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//multiListener = false;
menu.add(0,START_DELETE,0, "Delete selected..").setEnabled(multiListener);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.multiselect:
if(multiselect == 0) { multiselect = 1;
multiListener = true;
Log.d("DH", "index="+multiListener);
}
else if(multiselect == 1) { multiselect = 0; multiListener = false;
Log.d("DH", "index="+multiListener);
}
fillData();
return true;
case START_DELETE:
Toast.makeText(Notepadv3.this, "Pressed delete", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
基本上,如果 multiListener = true;使“删除选定的..”可按下,否则将其灰显...
这个简单的事情..不想和我一起解决,
因为某种原因...按钮总是灰显的,尽管Log说,它改变了是真的...
有人知道吗?
Im trying to do something like this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//multiListener = false;
menu.add(0,START_DELETE,0, "Delete selected..").setEnabled(multiListener);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.multiselect:
if(multiselect == 0) { multiselect = 1;
multiListener = true;
Log.d("DH", "index="+multiListener);
}
else if(multiselect == 1) { multiselect = 0; multiListener = false;
Log.d("DH", "index="+multiListener);
}
fillData();
return true;
case START_DELETE:
Toast.makeText(Notepadv3.this, "Pressed delete", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Basically, if multiListener = true; make "Delete selected.." pressable otherwise gray it out...
This simple , thing.. doesn't want to work out with me,
for somehow... the button is always greyed out, although Log says that, it changes to true...
Anyone, know something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该再次调用
setEnabled()
来更改项目的状态。它的状态不绑定到变量。应在onPrepareOptionsMenu()
中调用此方法。You should call
setEnabled()
again to change item's state. Its state doesn't bind to a variable. This method should be called inonPrepareOptionsMenu()
.