Android上下文菜单

作者: 孤风卓影 | 来源:发表于2017-08-26 01:05 被阅读0次

    GridView  gv = (GridView) findViewById(R.id.gv);

    // 添加长按点击弹出选择菜单

    gv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

    public void onCreateContextMenu(ContextMenu menu, View v,

    ContextMenuInfo menuinfo) {

    AdapterContextMenuInfo am=(AdapterContextMenuInfo)menuinfo;

    //获得每个listview中的item

    View item=am.targetView;

    TextView tv_id=(TextView) item.findViewById(R.id.tv_id);

    TextView tv_title=(TextView)item.findViewById(R.id.tv_title);

    menu.setHeaderTitle(tv_title.getText().toString());

    //获得要删除的记录id

    id=tv_id.getText().toString();

    menu.add(0, 0, 0, "更新该条");

    menu.add(0, 1, 0, "删除该条");

    }

    });

    上下文菜单被选择了

    @Override

    public boolean onContextItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case 0:

    Toast.makeText(MainActivity.this, "你点击了上下文菜单0",

    Toast.LENGTH_SHORT).show();

    break;

    case 1:

    Toast.makeText(MainActivity.this, "你点击了上下文菜单1",

    Toast.LENGTH_SHORT).show();

    break;

    default:

    break;

    }

    return super.onContextItemSelected(item);

    }

    相关文章

      网友评论

        本文标题:Android上下文菜单

        本文链接:https://www.haomeiwen.com/subject/oxxmdxtx.html