gpt4 book ai didi

Android registerForContextMenu 退出一个 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 08:12:23 24 4
gpt4 key购买 nike

我想在用户单击适配器内的 View 时使用上下文菜单。问题是每个 View 的 OnClick 都在一个不是 Activity 的类中 [它是一个扩展 BaseAdapter 的类],在 public View getView(final int position, View convertView, ViewGroup parent) 中。

因此,当我尝试在仅扩展 BaseAdapter 的类中创建上下文菜单或注册 registerForContextMenu 时,这是不可能的。

所以问题是:onclick 在这个 Not Activity 类中。是否可以给这个类一个允许它启动上下文菜单的参数?

最佳答案

I want to use a Context Menu when a user click on a View inside an Adapter.

希望不会。当用户长按 AdapterView 中的项目时,您可以使用上下文菜单。 Here is a sample project证明这一点。

如@coder_For_Life22 所建议的那样,任何其他内容(例如,“当用户单击适配器内的 View 时”弹出的内容)都应该是 AlertDialog

So, when i try to create the context menu or to register with registerForContextMenu inside this class that only extends BaseAdapter, it is not possible.

您只需为 AdapterView 调用一次 registerForContextMenu()。在调用 setContentView() 并因此创建了 AdapterView 之后,您可以从 Activity 的 onCreate() 执行此操作。

任何其他内容都应该是 AlertDialog

Is it possible to give to this class a parameters that allows it to start an context menu?

你不需要“给这个类一个允许它启动上下文菜单的参数”,因为你只为 AdapterView 调用一次 registerForContextMenu() >.

任何其他内容都应该是 AlertDialog。您可以在 Adapter 上添加一个构造函数,该构造函数将 Activity 作为参数,您的 Adapter 保留并用于 AlertDialog 。或者,如果 AdapterActivity 的内部类,只需使用 MyActivityName.this 来引用 Activity .

关于Android registerForContextMenu 退出一个 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com