单击的项目的 ID - Android ContextMenu
你好,
我有一个 ListView 填充了各种文本值,我希望当您长按并打开上下文菜单时,您可以复制长按的 ListItem 中的文本。到目前为止,我已经弹出了带有“复制”选项的上下文菜单:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
//this was following another question but I don't know what to do with it
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
long selectedId = info.id;
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.copy:
//used to be in a function but wasn't sure about views
//yes I know it's depreciated but it works ;)
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
TextView clicked = (TextView)this.findViewById(???);
clipboard.setText(clicked.getText());
Context context = getApplicationContext();
Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG);
copied.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
谢谢
G'day,
I have a ListView populated with various text values, and I want to have it that when you long press and open the context menu, you can copy the text in the ListItem you long pressed. So far I've got the context menu to pop up with the "Copy" option:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
//this was following another question but I don't know what to do with it
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
long selectedId = info.id;
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.copy:
//used to be in a function but wasn't sure about views
//yes I know it's depreciated but it works ;)
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
TextView clicked = (TextView)this.findViewById(???);
clipboard.setText(clicked.getText());
Context context = getApplicationContext();
Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG);
copied.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置一个变量来保存被单击的视图:
然后在其上创建上下文菜单时为其分配一个值:
现在您可以在最终方法中使用它:
Set up a variable to hold the view that gets clicked:
Then assign a value to it when the context menu is created on it:
And now you can use it on your final method:
我想你已经回答了你自己的问题。 id 是:
I think you have answered your own question. The id is: