当前位置 : 主页 > 手机开发 > android >

android – Issue onClickListener应用于活动时调用上下文菜单

来源:互联网 收集:自由互联 发布时间:2021-06-11
请在下面找到代码示例: public class Abc extends Activity implements OnClickListener{ private ListView displayList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.l
请在下面找到代码示例:

public class Abc extends Activity implements OnClickListener{

    private ListView displayList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlayout);

        displayList =(ListView)findViewById(R.id.addressbooklistview);
        addressbookAdapter = new CustomListAdapter(this,addressbookList);
        displayList.setAdapter(addressbookAdapter);

        registerForContextMenu(displayList);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
    { 
         Log.e("", "Entered Context Menu");
    }

    public void onClick(View v) {

        Log.e("", "Click Detected");
    }

}

我无法长按调用上下文菜单.
请让我知道任何解决方案.

我有这个问题.最初“显示上下文菜单”功能正常工作,但当我添加正常的“点击”功能时,上下文菜单不再有效.

我的问题是我实际上在Listview项目中的TextView上有onClick()代码,而不是ListView本身.据推测,它正在窃取列表视图中的点击.为了解决这个问题,我删除了那段代码,在我的Activity的onCreate方法中,我为ListView调用了setOnItemClickListener().所以现在我有这个:

// This creates the context menu functionality.
registerForContextMenu(findViewById(R.id.list_item));

// This creates the click functionality for the listview item.
ListView listView = (ListView) findViewById(R.id.list_item);
listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // code here            
    }
});
网友评论