再添加一个选项到android消息长按上下文菜单

时间:2014-08-09 07:15:43

标签: android messages

我想写一个可以存储一些消息的应用程序。目前通过长时间按下Android消息,一些选项(如"转发消息","删除消息"等)到达。我想为此添加一个选项(例如"存储此消息")。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

试试这段代码:

ListView nameList;    
nameList = (ListView) findViewById(R.id.list);
nameList.setLongClickable(true);
registerForContextMenu(nameList);

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {   
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle("Delete / Miss Call Contact");
    menu.add(menu.NONE,1,menu.NONE,"Delete");
    menu.add(menu.NONE,2,menu.NONE,"call");
}

@Override
public boolean onContextItemSelected(MenuItem item) {       
  final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  position = (int)info.id;          
  switch(item.getItemId()) {        
    case DELETE:
       // do something
    case MISSCALL:
       // do something
  }
  return super.onContextItemSelected(item);
}