Android:单击上下文菜单

时间:2011-06-22 04:57:43

标签: android contextmenu

我有一个扩展ListActivity的Intent。在填充列表适配器后的onCreate方法中,我使用registerForContextMenu(getListView());注册上下文菜单。

现在它正在工作,上下文菜单有其原始功能;一旦我点击并按住某个项目,就会打开上下文菜单。

我可以一键打开上下文菜单(无需按住列表)吗?

感谢所有帮助。

4 个答案:

答案 0 :(得分:30)

调用activity.openContextMenu(l) onitem点击事件,以便在单击和onLongClick调用activity.closeContextMenu()

时打开上下文菜单

实施例

import android.app.Activity;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MyListView extends ListActivity implements OnItemLongClickListener {
    /** Called when the activity is first created. */
    Activity activity = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activity = this;


        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, PENS);
        setListAdapter(arrayAdapter);

        getListView().setTextFilterEnabled(true);
        ListView lv = getListView();
        this.registerForContextMenu(lv);

        lv.setOnItemLongClickListener(this);


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub

        super.onConfigurationChanged(newConfig);
        System.out.println("...11configuration is changed...");
    }

    static final String[] PENS = new String[]{
        "MONT Blanc",
        "Gucci",
        "Parker",
        "Sailor",
        "Porsche Design",
        "item1",
        "item2",
        "item3",
        "item4",
        "item5",
        "item6",
        "item7",
        "item8",
        "item9",
        "item10",
        "item11"



    };



    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        activity.openContextMenu(l);
        System.out.println("...context is called");

    }




    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        System.out.println("...on create context menu...");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        System.out.println("...on long click close context menu...");
        activity.closeContextMenu();
        // TODO Auto-generated method stub

        return false;
    }

答案 1 :(得分:28)

这是另一种在单击时显示上下文菜单的简单方法。

private void addOnClickListener()
{
    contactList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
              view.showContextMenu();
        }
    })
}

ListView替换contactList,并确保在初始化ListView后调用此方法。

答案 2 :(得分:14)

我不认为它运作顺利。调用openContextMenu(l)将导致item.getMenuInfo()为空(在方法onContextItemSelected(MenuItem item)内)。

您应该拨打l.showContextMenuForChild(v)而不是openContextMenu(l)

答案 3 :(得分:5)

这项工作很完美....

    listmp3 = (ListView) findViewById(R.id.results_mp3);
    registerForContextMenu(listmp3);

    listmp3.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            listmp3.showContextMenuForChild(view);
        }
    });