标签:out 文件 getmenu switch 内容 err 移动设备 img make
1. 为什么会有菜单
01.
<?
xmlversion
xmlversion
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
menuxmlns:android
menuxmlns:android
=
"http://schemas.android.com/apk/res/android"
>
03.
<
itemandroid:id
itemandroid:id
=
"@+id/file"
04.
android:title
=
"@string/file"
>
05.
<!-- "file" submenu 添加子菜单 -->
06.
<
menu
>
07.
<!-- 子菜单的选项 -->
08.
<
itemandroid:id
itemandroid:id
=
"@+id/create_new"
09.
android:title
=
"@string/create_new"
/>
10.
<
itemandroid:id
itemandroid:id
=
"@+id/open"
11.
android:title
=
"@string/open"
/>
12.
</
menu
>
13.
</
item
>
14.
</
menu
>
1.
publicbooleanonCreateOptionsMenu(Menu menu) {
2.
// Inflate the menu; this adds items to the action bar if it is present.
3.
getMenuInflater().inflate(R.menu.main, menu);
4.
returntrue;
5.
}
第三步:如果我们需要给菜单添加点击事件,一般我们是通过重写onMenuItemSelected来设置的,不同菜单使用不同的点击回调函数,比如ContextMenu使用onContextItemSelected写法如下:
01.
publicbooleanonMenuItemSelected(intfeatureId, MenuItem item) {
02.
switch
(item.getItemId()) {
03.
caseItem.ID1:
04.
//do something
05.
break
;
06.
caseItem.ID2:
07.
//do something
08.
break
;
09.
//......
10.
}
11.
returnsuper.onMenuItemSelected(featureId, item);
12.
}
01.
<
menuxmlns:android
menuxmlns:android
=
"http://schemas.android.com/apk/res/android"
>
02.
<
item
03.
android:id
=
"@+id/info"
04.
android:orderInCategory
=
"100"
05.
android:showAsAction
=
"never"
06.
android:title
=
"软件信息"
>
07.
<
menu
>
08.
<
itemandroid:id
itemandroid:id
=
"@+id/authorInfo"
09.
android:title
=
"软件开发者信息"
/>
10.
<
itemandroid:id
itemandroid:id
=
"@+id/versionInfo"
11.
android:title
=
"软件版本信息"
/>
12.
</
menu
>
13.
</
item
>
14.
<!-- 建立一个在Action的菜单选项 -->
15.
<
item
16.
android:id
=
"@+id/settings"
17.
android:orderInCategory
=
"100"
18.
android:showAsAction
=
"always"
19.
android:icon
=
"@drawable/ic_launcher"
>
20.
<
menu
>
21.
<
itemandroid:id
itemandroid:id
=
"@+id/sysSetting"
22.
android:title
=
"系统设置"
/>
23.
<
itemandroid:id
itemandroid:id
=
"@+id/softSetting"
24.
android:title
=
"软件设置"
/>
25.
</
menu
>
26.
</
item
>
27.
</
menu
>
01.
privateListView list;
02.
@Override
03.
protectedvoidonCreate(Bundle savedInstanceState) {
04.
super
.onCreate(savedInstanceState);
05.
setContentView(R.layout.activity_main);
06.
String[] listItem = {
"1"
,
"2"
,
"3"
,
"4"
,
"5"
};
07.
ArrayAdapter<String> aa = newArrayAdapter<String>(
this
,
08.
android.R.layout.simple_list_item_1, listItem);
09.
list = (ListView) findViewById(R.id.list);
10.
list.setAdapter(aa);
11.
registerForContextMenu(list);
12.
//为list注册上下文菜单,通过onCreateContextMenu把XML菜单资源文件压进来,
13.
//通过onContextItemSelectd为每个菜单选项添加长按点击事件。
14.
}
15.
@Override
16.
publicvoidonCreateContextMenu(ContextMenu menu, View v,
17.
ContextMenuInfo menuInfo) {
18.
super
.onCreateContextMenu(menu, v, menuInfo);
19.
getMenuInflater().inflate(R.menu.float_menu, menu);
20.
}
21.
@Override
22.
publicbooleanonContextItemSelected(MenuItem item) {
23.
switch
(item.getItemId()) {
//判断长按点击了那个选项,为每个选项添加长按点击事件
24.
caseR.id.exit:
25.
showToast(
"exit"
);
26.
break
;
27.
caseR.id.next:
28.
showToast(
"next"
);
29.
break
;
30.
caseR.id.back:
31.
showToast(
"back"
);
32.
break
;
33.
}
34.
returnsuper.onContextItemSelected(item);
35.
}
36.
privatevoidshowToast(String str) {
37.
Toast.makeText(MainActivity.
this
, str, Toast.LENGTH_SHORT).show();
38.
}
01.
<
LinearLayoutxmlns:android
LinearLayoutxmlns:android
=
"http://schemas.android.com/apk/res/android"
02.
xmlns:tools
=
"http://schemas.android.com/tools"
03.
android:layout_width
=
"match_parent"
04.
android:layout_height
=
"match_parent"
05.
android:paddingBottom
=
"@dimen/activity_vertical_margin"
06.
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
07.
android:paddingRight
=
"@dimen/activity_horizontal_margin"
08.
android:paddingTop
=
"@dimen/activity_vertical_margin"
09.
android:orientation
=
"vertical"
10.
tools:context
=
".MenuActivity2"
>
11.
<
Button
12.
android:id
=
"@+id/btn_actionMode"
13.
android:layout_width
=
"match_parent"
14.
android:layout_height
=
"wrap_content"
15.
android:text
=
"打开ActionMode菜单"
/>
16.
<
Button
17.
android:id
=
"@+id/btn_popUp"
18.
android:layout_width
=
"match_parent"
19.
android:layout_height
=
"wrap_content"
20.
android:text
=
"打开PopUp菜单"
/>
21.
</
LinearLayout
>
(2)实现Callback的Java代码:
01.
privateclasscallback implementsActionMode.Callback {
02.
@Override
03.
publicbooleanonActionItemClicked(ActionMode mode, MenuItem item) {
04.
switch
(item.getItemId()) {
05.
caseR.id.user_info:
06.
Toast.makeText(MenuActivity2.
this
,
"You click user_info"
,
07.
Toast.LENGTH_SHORT).show();
08.
returntrue;
09.
caseR.id.user_setting:
10.
Toast.makeText(MenuActivity2.
this
,
"You click user_setting"
,
11.
Toast.LENGTH_SHORT).show();
12.
returntrue;
13.
caseR.id.user_login:
14.
Toast.makeText(MenuActivity2.
this
,
"You click user_login"
,
15.
Toast.LENGTH_SHORT).show();
16.
returntrue;
17.
default
:
18.
returnfalse;
19.
}
20.
}
21.
@Override
22.
publicbooleanonCreateActionMode(ActionMode mode, Menu menu) {
23.
getMenuInflater().inflate(R.menu.menu_activity2, menu);
24.
returntrue;
25.
}
26.
@Override
27.
publicvoidonDestroyActionMode(ActionMode mode) {
28.
Log.i(
"cth"
,
"ActionMode is destroyed."
);
29.
actionMode =
null
;
30.
}
31.
@Override
32.
publicbooleanonPrepareActionMode(ActionMode mode, Menu menu) {
33.
returnfalse;
34.
}
35.
}
01.
btn_actionMode = (Button) findViewById(R.id.btn_actionMode);
02.
btn_actionMode.setOnLongClickListener(newOnLongClickListener() {
//添加长按事件
03.
@Override
04.
publicbooleanonLongClick(View v) {
05.
if
(actionMode !=
null
) {
06.
returnfalse;
07.
}
08.
actionMode = startActionMode(newcallback());
//开启ActionMode菜单
09.
v.setSelected(
true
);
//设置为可选
10.
returntrue;
11.
}
12.
});
01.
btn_popUp = (Button) findViewById(R.id.btn_popUp);
02.
btn_popUp.setOnClickListener(newOnClickListener() {
03.
@TargetApi
(Build.VERSION_CODES.HONEYCOMB)
04.
@SuppressLint
(
"NewApi"
)
05.
@Override
06.
publicvoidonClick(View v) {
07.
PopupMenu popup = newPopupMenu(MenuActivity2.
this
, v);
//建立PopupMenu对象
08.
popup.getMenuInflater().inflate(R.menu.menu_activity2,
//压入XML资源文件
09.
popup.getMenu());
10.
popup.setOnMenuItemClickListener(MenuActivity2.
this
);
//设置点击菜单选项事件
11.
popup.show();
//显示菜单
12.
}
13.
});
01.
@Override
02.
publicbooleanonMenuItemClick(MenuItem item) {
03.
switch
(item.getItemId()) {
04.
caseR.id.user_info:
05.
Toast.makeText(MenuActivity2.
this
,
"You click user_info"
,
06.
Toast.LENGTH_SHORT).show();
07.
returntrue;
08.
caseR.id.user_setting:
09.
Toast.makeText(MenuActivity2.
this
,
"You click user_setting"
,
10.
Toast.LENGTH_SHORT).show();
11.
returntrue;
12.
caseR.id.user_login:
13.
Toast.makeText(MenuActivity2.
this
,
"You click user_login"
,
14.
Toast.LENGTH_SHORT).show();
15.
returntrue;
16.
default
:
17.
returnfalse;
18.
}
19.
}
标签:out 文件 getmenu switch 内容 err 移动设备 img make
原文地址:http://www.cnblogs.com/aswdd/p/6819387.html