0

我有活动 a,我想使用操作栏徽标返回。通常这样做:

ActionBar actionBar= getActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);

并且要定义你通常必须重写活动 onOptionsItemSelected() 的事件,但是如果你想从帮助类重写这个事件怎么办,

所以我的代码是:

class Helper{
    public void init(Activity a) {
    ActionBar actionBar= a.getActionBar();
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(true);
    ?????? a.onOptionsItemSelected(?MenuItem?)
    }}

如何找到操作栏徽标单击的 MenuItem 对象

4

1 回答 1

1

You have to place this in your caller-class:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // DO SOMETHING WHEN BUTTON PRESSED!
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

The only way i know of to do it in the helper-class would be to have the helper as a fragment and then use setHasOptionsMenu(true); in that fragment.

于 2012-12-16T11:07:02.843 回答