簡體   English   中英

如何判斷哪個按鈕訪問了上下文菜單

[英]How can I tell which button has accessed the context menu

在我的音板應用程序中,我使用此代碼創建了一個上下文菜單。

public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Choose an option");
        menu.add(0, v.getId(), 0, "Save as ringtone");
        menu.add(0, v.getId(), 0, "Save as Notification");
        menu.add(0, v.getId(), 0, "Save as Alarm");
        menu.add(0, v.getId(), 0, "Exit Menu");
}


   @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    //    Global.currentsound = info.id;
        if(item.getTitle()=="Save as ringtone"){function1(item.getItemId());}
        else if(item.getTitle()=="Save as Notification"){function2(item.getItemId());}
        else if(item.getTitle()=="Save as Alarm"){function3(item.getItemId());}
        else {return false;}
    return true;
    }

並調用這樣的按鈕

按鈕cmenu =(按鈕)findViewById(R.id.s1sound1);
registerForContextMenu(c菜單);

現在我想將每個按鈕的信息傳遞給代碼中的函數,以根據按下的按鈕設置聲音。 如果不為每個按鈕創建一個瘋狂的單獨的上下文菜單,我該如何做到這一點。

謝謝

我認為它存儲在“View v”中,Button擴展了View,所以你必須將其強制轉換。 我不確定,但您可以通過設置斷點來檢查

super.onCreateContextMenu(menu, v, menuInfo);

行,並檢查調試器。

暫無
暫無

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

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