阅读之后简单写其中的某些功能,先贴图:

android Menu 菜单使用总结_androidandroid Menu 菜单使用总结_android_02

android Menu 菜单使用总结_android_03android Menu 菜单使用总结_android_04

android Menu 菜单使用总结_android_05


第一步:

android Menu 菜单使用总结_android_06

相关代码:


?
1

第二步:


代码中的实现:


?
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
packagecom.jiaruihuademo.testmenu;
importjava.util.ArrayList;
importjava.util.List;
importandroid.os.Bundle;
importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.view.ContextMenu;
importandroid.view.ContextMenu.ContextMenuInfo;
importandroid.view.Menu;
importandroid.view.MenuInflater;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.View.OnCreateContextMenuListener;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.AdapterContextMenuInfo;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.ListView;
importandroid.widget.PopupMenu;
importandroid.widget.PopupMenu.OnMenuItemClickListener;
importandroid.widget.Toast;
/**
*
* menu 的简单使用
*
* @author JiaRH
*
* @date 2013-12-30 上午9:55:29
*/
@SuppressLint(NewApi)
publicclassMainActivity extendsActivity implementsOnMenuItemClickListener,
OnCreateContextMenuListener, OnItemClickListener {
privateListView listview;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView1);
List<string> list = newArrayList<string>();
for(inti = 0; i < 20; i++) {
list.add(i + );
}
listview.setAdapter(newArrayAdapter<string>(this,
android.R.layout.simple_list_item_1, list));
listview.setOnItemClickListener(this);
}
/**
* 创建popviewMenu
*
* @param v
*/
@SuppressLint(NewApi)
publicvoidshowpop(View v) {
PopupMenu popup = newPopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.main, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
/*
* (non-Javadoc)
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
publicbooleanonOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
caseR.id.item1:
Toast.makeText(this, you have clicked 1, 0).show();
break;
caseR.id.item2:
Toast.makeText(this, you have clicked 2, 0).show();
break;
caseR.id.item3:
Toast.makeText(this, you have clicked 3, 0).show();
break;
caseR.id.item4:
Toast.makeText(this, you have clicked 4, 0).show();
break;
default:
break;
}
returnsuper.onOptionsItemSelected(item);
}
/**
* 响应popmenu的点击事件
*/
@Override
publicbooleanonMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
caseR.id.item1:
Toast.makeText(this, you have clicked 1, 0).show();
break;
caseR.id.item2:
Toast.makeText(this, you have clicked 2, 0).show();
break;
caseR.id.item3:
Toast.makeText(this, you have clicked 3, 0).show();
break;
caseR.id.item4:
Toast.makeText(this, you have clicked 4, 0).show();
break;
default:
break;
}
returnfalse;
}
/**
* listview响应事件
*/
@Override
publicvoidonItemClick(AdapterView<!--?--> parent, View view, intposition,
longid) {
// TODO Auto-generated method stub
registerForContextMenu(view);
}
@Override
publicvoidonCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
@Override
publicbooleanonContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch(item.getItemId()) {
caseR.id.item1:
Toast.makeText(this, you have clicked 1, 0).show();
returntrue;
caseR.id.item2:
Toast.makeText(this, you have clicked 2, 0).show();
returntrue;
caseR.id.item3:
Toast.makeText(this, you have clicked 3, 0).show();
returntrue;
caseR.id.item4:
Toast.makeText(this, you have clicked 4, 0).show();
returntrue;
default:
returnsuper.onContextItemSelected(item);
}
}
}
</string></string></string>

第三步;附上布局文件:



?
1
2
3
4
5
6
<relativelayout android:layout_height="match_parent"android:layout_width="match_parent"android:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context=".MainActivity"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools">
<textview android:id="@+id/textView1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/hello_world"><button android:id="@+id/showpop"android:layout_below="@+id/textView1"android:layout_height="wrap_content"android:layout_margintop="16dp"android:layout_width="wrap_content"android:onclick="showpop"android:text="show"popmenu="">
<listview android:id="@+id/listView1"android:layout_alignleft="@+id/showpop"android:layout_below="@+id/showpop"android:layout_height="wrap_content"android:layout_margintop="40dp"android:layout_width="match_parent">
</listview></button></textview></relativelayout>

备注;


我使用的



android:minSdkVersion=8
android:targetSdkVersion=18 />