簡體   English   中英

單擊菜單后打開上下文菜單欄

[英]Open a contextual menu bar upon menu click

我想知道是否有一種方法可以通過在Android中單擊電話菜單按鈕來打開上下文菜單欄。 我根據此基礎教程創建了一個上下文菜單欄: 教程上下文菜單欄

現在,我的活動中包含以下內容:

mCallback = new Callback(){

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch(item.getItemId()){
        case 1:
            Toast.makeText(getBaseContext(), "Selected Action1 " + postId, Toast.LENGTH_SHORT).show();
            //mode.finish();    // Automatically exists the action mode, when the user selects this action

            break;
        case 2:
            Toast.makeText(getBaseContext(), "Selected Action2 "  + postId, Toast.LENGTH_SHORT).show();
            //mode.finish();    // Automatically exists the action mode, when the user selects this action
            break;
        case 3:
            Toast.makeText(getBaseContext(), "Selected Action3 " + postId, Toast.LENGTH_SHORT).show();
            //mode.finish();    // Automatically exists the action mode, when the user selects this action
            break;

        }
        return false;

    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle("Actions for Post");
        getMenuInflater().inflate(R.menu.context_menu_posts, menu);

        menu.add(1, 1, 1, "Option 1");
        menu.add(1, 2, 2, "Option 2");
        menu.add(1, 3, 3, "Option 3");



        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mMode = null;

    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

};

但是現在我希望它在用戶單擊菜單按鈕時“顯示”。 在LongclickListener中使用此代碼之前:

if(mMode!=null)
        return false;
    else
        mMode = startActionMode(mCallback);

這對於“活動”中的控件非常有用,但是是否可以通過菜單單擊來做到這一點? 我嘗試將startActionMode的代碼放入@Overriden菜單方法中,但單擊菜單按鈕后無法打開它。

提前致謝!

對不起,我很笨,我一定比我想的還要累。

因為我以前沒有使用過,所以沒有想到只是覆蓋menubutton並在其中添加代碼。

這篇文章為我解決了: 攔截menuclick

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM