Working with Context Menus

                                        Working with Context Menus

 

      Android supports the idea of context menus through an action called a long click.

context menu is represented as a ContextMenu class in the
Android menu architecture.  a context menu is owned by a view, the method to populate context menus  resides in the Activity class. This method is called activity.onCreateContextMenu(),
and its role resembles that of the activity.onCreateOptionsMenu() method.

If you want a particular view
to own a context menu, you must register that view with its activity specifically for the
purpose of owning a context menu. You do this through the
activity.registerForContextMenu(view) method,

 

the steps to implement a context menu:

1. Register a view for a context menu in an activity’s onCreate() method.
2. Populate the context menu using onCreateContextMenu(). You must
complete step 1 before this callback method is invoked by Android.
3. Respond to context-menu clicks.

 

A first step

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)this.findViewById(R.id.textViewId);
registerForContextMenu(this.getTextView());
}

 

B:Populating a Context Menu

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Sample Context Menu");
menu.add(200, 200, 200, "item1");
}

C:Responding to Context Menu Items

@Override
public boolean onContextItemSelected(MenuItem item)
{
if (item.itemId() = some-menu-item-id)
{
//handle this menu item
return true;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值