单击的项目的 ID - Android ContextMenu

发布于 2024-12-14 15:06:10 字数 1343 浏览 3 评论 0原文

你好,
我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2024-12-21 15:06:10

设置一个变量来保存被单击的视图:

View clicked;

然后在其上创建上下文菜单时为其分配一个值:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    clicked = v;

    //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);

        clipboard.setText(clicked.getText());
        // this should work now properly.

        Context context = getApplicationContext();
        Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG);
        copied.show();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

Set up a variable to hold the view that gets clicked:

View clicked;

Then assign a value to it when the context menu is created on it:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    clicked = v;

    //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);
}

And now you can use it on your final method:

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);

        clipboard.setText(clicked.getText());
        // this should work now properly.

        Context context = getApplicationContext();
        Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG);
        copied.show();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}
时光匆匆的小流年 2024-12-21 15:06:10

我想你已经回答了你自己的问题。 id 是:

long selectedId = info.id;

I think you have answered your own question. The id is:

long selectedId = info.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文