按物理上的菜单按钮

时间:2013-05-14 13:22:52

标签: android menu

我这里有一个代码here

的例子
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时。 我想制作,就是只有当用户点击物理菜单按钮时才会调用菜单

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        mQuickAction.show(this.btn1);
        return true;
    }
    return super.onKeyUp(keyCode, event);
}