0

我在这里有一个这样的代码 示例

public class Example1Activity extends Activity {
private static final int ID_ADD = 1;
private static final int ID_ACCEPT = 2;
private static final int ID_UPLOAD = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.example1);

    ActionItem addItem      = new ActionItem(ID_ADD, "Add", getResources().getDrawable(R.drawable.ic_add));
    ActionItem acceptItem   = new ActionItem(ID_ACCEPT, "Accept", getResources().getDrawable(R.drawable.ic_accept));
    ActionItem uploadItem   = new ActionItem(ID_UPLOAD, "Upload", getResources().getDrawable(R.drawable.ic_up));

    //use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked
    uploadItem.setSticky(true);

    final QuickAction mQuickAction  = new QuickAction(this);

    mQuickAction.addActionItem(addItem);
    mQuickAction.addActionItem(acceptItem);
    mQuickAction.addActionItem(uploadItem);

    //setup the action item click listener
    mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
        @Override
        public void onItemClick(QuickAction quickAction, int pos, int actionId) {
            ActionItem actionItem = quickAction.getActionItem(pos);

            if (actionId == ID_ADD) {
                Toast.makeText(getApplicationContext(), "Add item selected", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
            }
        }
    });

    mQuickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
        @Override
        public void onDismiss() {
            Toast.makeText(getApplicationContext(), "Ups..dismissed", Toast.LENGTH_SHORT).show();
        }
    });

    Button btn1 = (Button) this.findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuickAction.show(v);
        }
    })

    Button btn2 = (Button) this.findViewById(R.id.btn2);
    btn2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuickAction.show(v);
            mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
        }
    });
}

但我想做

mQuickAction.show(v);

当用户点击物理菜单按钮时btn1。我想做的是,只有当用户点击物理菜单按钮时才会调用菜单

4

2 回答 2

1

如果要在用户按下物理菜单按钮时触发事件,则必须执行以下操作:

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        mQuickAction.show(this.getCurrentFocus());//or the view param that you want
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
于 2013-05-14T13:37:51.743 回答
1
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        mQuickAction.show(this.btn1);
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
于 2013-05-15T04:53:00.107 回答