如何获得由此引发了上下文菜单的按钮来查看? [英] How to get the Button view which triggered a Context Menu?

查看:138
本文介绍了如何获得由此引发了上下文菜单的按钮来查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个快捷菜单按钮动态的看法。当选择了上下文菜单项,我想以检索由此引发的上下文菜单的按钮来查看。

I'm creating dynamically Button views with a context menu. When a context menu item is selected, I would like to retrive the Button view which triggered the context menu.

这是我如何创建按钮:

// Create a new button
Button buttonView = new Button(this);
// Set button text
buttonView.setText("MyButton");
// Set on click listener
buttonView.setOnClickListener( new ButtonClickHandler() );
// Register for context menu
registerForContextMenu(buttonView);

这是我如何创建上下文菜单:

This is how I create the context menu :

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

这就是我如何处理所选项目:

And this is how I handle selected items :

public boolean onContextItemSelected(MenuItem item) {

    // Get extra menu information about the item selected   
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    // 
    switch (item.getItemId()) {
    case R.id.delete:

        // Retrieve selected button text
        String btnText = ((Button) info.targetView).getText().toString();
        // etc...
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

问题是,(AdapterContextMenuInfo)item.getMenuInfo()返回空值,即有关于项目的任何额外信息。我期待得到info.targetView的按钮来查看。显然,这仅适用于ListView中,因为适配器视图需要填充此额外信息的照顾。

The issue is that "(AdapterContextMenuInfo) item.getMenuInfo()" returns null, i.e. there is no extra information about the item. I was expecting to get the Button view in info.targetView. Apparently this works only for ListView, because AdapterView takes care of populating this extra info.

我想我应该做一些onCreateContextMenu这个信息连接。样本code附上这个信息将是非常欢迎的。

I guess I should do something in "onCreateContextMenu" to attach this information. A sample code to attach this information would be very much welcomed.

感谢

推荐答案

这用于创建上下文菜单中的按钮查看参数传递到 onCreateContextMenu

The Button that was used to create the context menu is the View parameter passed into onCreateContextMenu.

这篇关于如何获得由此引发了上下文菜单的按钮来查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆