ContextMenu 是用户手指长按某个View触发的菜单
步骤:
1. 注册上下文菜单:在onCreate() 中对View进行注册
registerForContextMenu();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTV = (TextView)findViewById(R.id.tv_Text);
registerForContextMenu(myTV);
}2. 创建上下文菜单
onContextItemSelected(MenuItem item, View v, ContextMenuInfo menuInfo)
public boolean onContextItemSelected(MenuItem item, View v, ContextMenuInfo menuInfo)
{
switch(item.getItemId()){
case ITEM1:
myTV.setBackgroundColor(Color.RED);
break;
case ITEM2:
myTV.setBackgroundColor(Color.GREEN);
break;
case ITEM3:
myTV.setBackgroundColor(Color.WHITE);
break;
}
return true;
}
本文详细介绍了如何在Android中为View注册上下文菜单,并通过onContextItemSelected方法处理菜单项选择事件,实现视图背景颜色的改变。
673

被折叠的 条评论
为什么被折叠?



