-1

我想禁用ActionBar Menu Item但使其可点击。有什么办法可以做到这一点吗?

现在,如果我设置setEnabledfalse正常行为,它只是灰显,但它不可点击。

4

1 回答 1

0

这并不能真正回答问题,因为ActionBar Menu Item不能同时禁用和点击。我最终做的是启用Menu Item并将文本颜色设置为灰色以使其看起来是伪禁用的。如果满足条件,它将更改为默认颜色。

学分:尼古拉·布赫·安徒生

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    SpannableStringBuilder text = new SpannableStringBuilder();
    text.append(getString(R.string.menu_title));

    if (someCondition){
         text.setSpan(new ForegroundColorSpan(Color.BLACK), 
                 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    else {
        text.setSpan(new ForegroundColorSpan(Color.LTGRAY), 
                0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }


    MenuItem item = menu.findItem(R.id.some_menu_id);
    item.setTitle(text);

    return true;
}
于 2013-06-28T05:24:21.257 回答