0

ユーザーがスピナーのアイテムを短くクリックした場合、コンテキストメニューを表示しません:

    uSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id)
        {
            Toast.makeText(getBaseContext(), "Position = " + position, Toast.LENGTH_SHORT).show();
            pos = position;
            registerForContextMenu(view); 
            openContextMenu(view);
            unregisterForContextMenu(view);
        }
    });
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo info)
    {
        if(pos != -1)
        {
            menu.setHeaderTitle("Admin menu");
            menu.add("Kick");
        }       
    }
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        if(item.getTitle()=="Kick")
        {
            Toast.makeText(getBaseContext(), "Kick: " + usrStack.get(pos), Toast.LENGTH_SHORT).show();      
        }
        return super.onContextItemSelected(item);  
    }

すべて問題ありませんが、アプリを起動すると、最初の要素にこのコンテキスト メニューが表示されます。(覚えていると思いますが、匿名のように、最初の要素はスピナーに表示されます。この最初の要素を選択して、コンテキスト メニューを呼び出すと思います)

4

1 に答える 1

1

The easiest way to do this is to add a boolean flag to your onItemSelected callback.

if (!isfirst){
    openContextMenu(spinner);                   
} else {
    isfirst = false;
}

You will also encounter a second problem with your spinner, where because the first item is selected by default, clicking it won't 'trigger' the callback method. The way around this is to insert a blank item at the head of your list and check for this in your callback too.

 if (position != 0){
    openContextMenu(spinner);                   
} else {
    // Do nothing
}

edit: I also noticed you are registering each view for the context menu every time you click an item. You only need to register the spinner once, after you've defined it, then pass the spinner as the view when you call openContextMenu(spinner)

于 2012-11-18T20:41:45.283 に答える