1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
public
class
MainActivity
extends
Activity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1 = (TextView) findViewById(R.id.textView1);
registerForContextMenu(textView1);
//注册上下文菜单
TextView textView2 = (TextView) findViewById(R.id.textView2);
registerForContextMenu(textView2);
//注册上下文菜单
}
@Override
public
boolean
onContextItemSelected(MenuItem item)
//上下文菜单点击事件
{
switch
(item.getItemId())
{
case
1
:
Toast.makeText(
this
,
"确定"
, Toast.LENGTH_LONG).show();
break
;
case
2
:
Toast.makeText(
this
,
"退出"
, Toast.LENGTH_LONG).show();
break
;
default
:
break
;
}
return
super
.onContextItemSelected(item);
}
@Override
//创建上下文菜单
public
void
onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
switch
(v.getId())
//根据控件id创建上下文菜单
{
case
R.id.textView1:
menu.add(
0
,
1
,
0
,
"确定"
);
menu.add(
0
,
2
,
0
,
"退出"
);
break
;
case
R.id.textView2:
menu.add(
0
,
3
,
0
,
"编辑"
);
menu.add(
0
,
4
,
0
,
"修改"
);
break
;
default
:
break
;
}
super
.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1200364,如需转载请自行联系原作者