gpt4 book ai didi

android - 动态上下文菜单选项

转载 作者:行者123 更新时间:2023-11-29 14:57:20 26 4
gpt4 key购买 nike

是否可以在 android 中创建动态上下文菜单?

我的意思是,例如,如果我有一个项目列表,它可以处于两种状态:“已读”和“未读”。我希望能够调出一个上下文菜单,其中包含未读项目的“标记为已读”选项和已读项目的“标记为未读”选项。

所以点击:

> read
> unread <-- click
> read

将显示上下文菜单“标记为已读”,导致:

> read
> read
> read <-- click

将显示菜单“标记为未读”。

是否有一些功能允许我在显示之前自定义上下文菜单的创建?

欢迎任何帮助!

最佳答案

由于您不提供代码,这是基本方法:

@Override
public void onCreateContextMenu(ContextMenu contextMenu,
View v,
ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;

String actionString = "Mark as Read";

// if it's alredy read
if ( ((TextView) info.targetView).getText().toString().equals("read") )
actionString = "Mark as Unread";

menu.setHeaderTitle("Action");
menu.add(0, 0, 0, actionString);
}

在这种情况下,我假设列表中填充了 TextView s 中可以包含字符串“read”或“unread”。正如我已经说过的,这是一种非常基本的方法。这里重要的是要注意 ContextMenu.ContextMenuInfo正在使用对象。

此外,要读取所选项目的状态,您可以使用 item.getMenuInfo() onContextItemSelected(MenuItem item) 中的方法方法。

关于android - 动态上下文菜单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3064637/

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