从 oncontextitemselected 获取视图 id

发布于 2024-11-08 00:28:10 字数 1530 浏览 0 评论 0原文

我为上下文菜单注册了多个按钮,

我如何知道单击了哪个按钮以显示菜单?

下面是我将使用的伪代码。我需要做一些与单击哪个按钮相关的事情(我还有几个按钮需要声明),我如何知道单击哪个按钮会激活上下文菜单。

编辑:我想我没有说清楚,我想知道单击了哪个按钮来显示菜单。不是单击了哪个菜单项。无论如何,我有一个解决方案,我很快就会添加。

谢谢

private static final int SEND_AS_TEXT = Menu.FIRST;
private static final int SEND_AS_IMAGE = Menu.FIRST + 1;

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

        sendAllBtn = (Button)findViewById(R.id.sendAllBtn);
        sendAllBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        registerForContextMenu(v);
        openContextMenu(v);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case SEND_AS_TEXT:
            //do sth related to the button clicked
            break;

        }
        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
        menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    }

I've several buttons registered for context menu

how do I know which button was clicked for the menu to appear?

below is the pseudocode that i'll be using. I need to do something related to which button clicked (I have few more buttons to be declared), how do I know that the context menu is activated from which button click.

EDIT: I think i didn't make myself clear, I wanted to know which button was clicked for the menu to appear. Not which menu item is clicked. Anyways, I've a solution which I'll add in pretty soon.

thanks

private static final int SEND_AS_TEXT = Menu.FIRST;
private static final int SEND_AS_IMAGE = Menu.FIRST + 1;

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

        sendAllBtn = (Button)findViewById(R.id.sendAllBtn);
        sendAllBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        registerForContextMenu(v);
        openContextMenu(v);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case SEND_AS_TEXT:
            //do sth related to the button clicked
            break;

        }
        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
        menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

我只土不豪 2024-11-15 00:28:10

好的,非常感谢其他人的帮助,这消除了我对 getItemId 的疑虑,因为它返回我分配给菜单项的 ID。
就我而言,我想知道在创建上下文菜单之前单击了哪个按钮。

为此,我只需创建一个长变量来存储单击的按钮。按钮的 ID 可以通过以下方式获取:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Send As..");
    menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
    menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    btnId = v.getId(); //this is where I get the id of my clicked button
}

稍后我只需要引用这个 btnId 就可以做我想做的事情了。

Ok, thanks alot for the help from the others which clear my doubts on the getItemId since it returns the ID that I assigned to the menu item.
In my case, I wanted to know which button was clicked before the contextmenu was created.

To do this, I simply create a long variable to store the button that was clicked. The ID of the button can be obtained as in the following:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Send As..");
    menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
    menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    btnId = v.getId(); //this is where I get the id of my clicked button
}

and later on I'll only need to refer to this btnId to do whatever I want.

逐鹿 2024-11-15 00:28:10

我认为使用特定视图的 ID 更有意义。假设您有一个 ListView,其中填充了包含您的数据的项目,但在您创建的分隔符/标题的一些项目之间。您不希望分隔符处理点击/长按。

在某些情况下,仅引用“position”或 MenuInfo.id 是完全可以的,但根据您的数据结构,您可能需要更多控制。

您可以做的是为 ListView 中的项目/视图设置 ID(view.setId(x),其中 x 代表数据结构/对象的 ID/位置。然后,在创建 ContextMenu 并处理其中的选择时,读取该 ID 的命令如下:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}

I think it makes more sense to use the ID of the specific view. Say you've got an ListView populated of items containing your data, but in-between some of the items you've created separators/headers. You don't want the separators to handle clicks/long clicks.

In some cases it's totally fine to just refer to "position" or MenuInfo.id, but depending on your data structure you might need more control.

What you can do is to set ID's for the items/views within your ListView (view.setId(x), where x represents the ID/position for your data structure/object. Then, when creating a ContextMenu and handling selections within it do the following to read that ID out:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}
坦然微笑 2024-11-15 00:28:10

如果您正在查找基础数据的 ID(由适配器的 getItemId(int) 提供),则只需在 onContextItemSelected 方法中添加以下行:

final AdapterView.AdapterContextMenuInfo info = 
  (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final long datasId = info.id    // get datas id

If you are looking for the ID of your underlying data (provided by the adapter's getItemId(int)), then just add the following lines in the onContextItemSelected method:

final AdapterView.AdapterContextMenuInfo info = 
  (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final long datasId = info.id    // get datas id
旧人哭 2024-11-15 00:28:10
@Override
public boolean onContextItemSelected(MenuItem item) {
    item.getItemId();
    return super.onContextItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    item.getItemId();
    return super.onContextItemSelected(item);
}
椵侞 2024-11-15 00:28:10

试试这个...

@Override
public boolean onContextItemSelected(MenuItem item)
 {
    if(item.getItemId()==SEND_AS_TEXT)
    {
        //code for send text
    } 
    else if(item.getItemId()==SEND_AS_IMAGE)
    {
       //code for send image
    }
    return super.onContextItemSelected(item);
}

try this...

@Override
public boolean onContextItemSelected(MenuItem item)
 {
    if(item.getItemId()==SEND_AS_TEXT)
    {
        //code for send text
    } 
    else if(item.getItemId()==SEND_AS_IMAGE)
    {
       //code for send image
    }
    return super.onContextItemSelected(item);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文