android基础

来源:互联网 发布:p2p网络借贷 编辑:程序博客网 时间:2024/05/22 09:41
 


今天主要讲UI组件菜单项,菜单主要分为3种菜单类型。分别为options menucontext menusub menuOptions menu是通过按home键来显示的context menu需要在view上按上2s后显示。这两种menu都有可以加入子菜单,子菜单不能种不能嵌套子菜单。

options menu最多只能在屏幕最下面显示6个菜单选项,成为icon menuicon menu不能有checkable选项。多余6的会以more icon menu来调出,成为expanded menuoptions menu通过activityonCreateOptionsMenu来生成,这个函数只会在menu第一次生成时调用。任何想改变options menu的想法只能在onPrepareOptionsMenu来实现,这个函数会在menu显示前调用。onOptionsItemSelected处理选中的菜单项。

context menu是跟某个具体的view绑定在一起,在activity种用registerForContextMenu来为某个view注册context menucontext menu在显示前都会调用onCreateContextMenu来生成menuonContextItemSelected选中的菜单项。

android还提供了对菜单项进行分组的功能,可以把相似功能的菜单项分成同一个组,这样就可以通过调用setGroupCheckablesetGroupEnabled,setGroupVisible来设置菜单属性,而无须单独设置。

1、利用xml定义选项菜单

    1. options menu通过activityonCreateOptionsMenu来生成

    2. 这个函数只会在menu第一次生成时调用。

      1. 任何想改变options menu的想法只能在onPrepareOptionsMenu来实现,这个函数会在menu显示前调用。

    3. onOptionsItemSelected用来处理选中的菜单项。

      3响应菜单项单击事件

    android.view.Menu接口代表一个菜单,android用它来管理各种菜单项。注意我们一般不自己创建menu,因为每个Activity默认都自带了一个,我们要做的是为它加菜单项和响应菜单项的点击事件

3种方式

onMenuItemSelected(int featureId, MenuItem item)

public booleanonOptionsItemSelected(MenuItem item)

onMenuItemClick

  1. 动态添加、修改和删除菜单项

菜单项的增删改查

增:addaddSubMenu

add方法参数:
groupId
:组标识符,用于将menuItem分组。不需要分组时可以使用Menu.NONE赋值。
itemId
:菜单项标识符,
order
:菜单项顺序值
title
:菜单项显示的字符串

删:removeItemremoveGroup

改:

  • 选项菜单:onPrepareOptionsMenu()——>MenuItem->setXXX()

  • 上下文菜单:onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {} 方法在每次调用上下文菜单时都会被调用一次

    4子菜单的创建

  • Xml创建

  • 动态创建

  • Menu submenu = Menu.addSubMenu(x, x, x, x)

  • submenu.add()

  • submenu.add()

  • main_menu.addSubMenu(x,x,x,x);

  • 或:

    getMenuInflater ().inflater(R.menu.submenu, submenu)

    一些常见用到的方法:


abstract MenuItem

add(CharSequence title)

Add a new item to the menu.

abstract MenuItem

add(int groupId, int itemId, int order, int titleRes)

Variation on add(int, int, int,CharSequence) that takes a string resource identifier instead of the string itself.

abstract MenuItem

add(int titleRes)

Add a new item to the menu.

abstract MenuItem

add(int groupId, int itemId, int order,CharSequence title)

Add a new item to the menu.

abstract int

addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags,MenuItem[] outSpecificItems)

Add a group of menu items corresponding to actions that can be performed for a particular Intent.

abstract SubMenu

addSubMenu(int groupId, int itemId, int order,CharSequence title)

Add a new sub-menu to the menu.

abstract SubMenu

addSubMenu(int groupId, int itemId, int order, int titleRes)

Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.

abstract SubMenu

addSubMenu(CharSequence title)

Add a new sub-menu to the menu.

abstract SubMenu

addSubMenu(int titleRes)

Add a new sub-menu to the menu.

abstract void

clear()

Remove all existing items from the menu, leaving it empty as if it had just been created.

abstract void

close()

Closes the menu, if open.

abstract MenuItem

findItem(int id)

Return the menu item with a particular identifier.

abstract MenuItem

getItem(int index)

Gets the menu item at the given index.

abstract boolean

hasVisibleItems()

Return whether the menu currently has item items that are visible.

abstract boolean

isShortcutKey(int keyCode,KeyEvent event)

Is a keypress one of the defined shortcut keys for this window.

abstract boolean

performIdentifierAction(int id, int flags)

Execute the menu item action associated with the given menu identifier.

abstract boolean

performShortcut(int keyCode,KeyEvent event, int flags)

Execute the menu item action associated with the given shortcut character.

abstract void

removeGroup(int groupId)

Remove all items in the given group.

abstract void

removeItem(int id)

Remove the item with the given identifier.

abstract void

setGroupCheckable(int group, boolean checkable, boolean exclusive)

Control whether a particular group of items can show a check mark.

abstract void

setGroupEnabled(int group, boolean enabled)

Enable or disable all menu items that are in the given group.

abstract void

setGroupVisible(int group, boolean visible)

Show or hide all menu items that are in the given group.

abstract void

setQwertyMode(boolean isQwerty)

Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).

abstract int

size()

Get the number of items in the menu.

原创粉丝点击