android – 单击上下文菜单

前端之家收集整理的这篇文章主要介绍了android – 单击上下文菜单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
抱歉愚蠢的问题,但我应该更改/添加代码 – 通过短暂单击列表视图中的项目来显示上下文菜单
public class MyActivity extends ListActivity implements AdapterView.OnItemClickListener {
    static final String[] COUNTRIES = new String[]{
            "Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica"
    };

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item,COUNTRIES));

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

        lv.setOnItemClickListener(this);
    }


    public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo) {
        Log.e("sdklfjsdkljfl"," <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ");
        menu.setHeaderTitle("HELLO");

    }

    public void onItemClick(AdapterView<?> adapterView,View view,int i,long l) {
        Log.e("kjhasjkdhkas","sdkhjkhskaf");
        this.openContextMenu(view);
    }
}

解决方法

这里发布的其他解决方案对我不起作用,因为我使用的是ListFragment.但是,以下解决方案似乎对ListFragment和ListActivity(或者通常只是任何旧的listview)都很有效:
public void onListItemClick(ListView l,int position,long id){
  l.showContextMenuForChild(v);   
}

它更加简单和优雅.实际上,这实际上是ListView类本身如何通过长按来启动上下文菜单.

原文链接:https://www.f2er.com/android/316391.html

猜你在找的Android相关文章