开发者

Enable/Disable Options Menu button with .setEnabled()?

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);

    }
}

Ba开发者_运维知识库sically, 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?


You should call setEnabled() again to change item's state. Its state doesn't bind to a variable. This method should be called in onPrepareOptionsMenu().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜