写一篇文章很辛苦啊!!!
转载请注明,联系请邮件nlp30508@qq.com
我学习Android都是结合源代码去学习,这样比较直观,非常清楚的看清效果,觉得很好,今天的学习源码是网上找的个db源码 百度搜就知道很多下载的地方 我写的东西有可能比较乱,如果单一的篇章没看明白,请看上一篇文章
上篇文章 地址:http://blog.youkuaiyun.com/u014737138/article/details/40678625
在上面的展示实例中我们使用到了菜单 使用的OptionsMenu 先看看这个应用程序中是做了哪些操作:
1.创建菜单
我们需要用到菜单,那么必须要创建一个菜单,负责创建菜单的类就是当前的activity类 这个必须要清楚,不要老是怀疑到底是谁在调用 onCreateOptionsMenu(Menu menu) ,明确的告诉你,是activity自己调用的,就像它调用onCreate(Bundle)一样,这两个函数都是由它调用的。
//由当前的activity负责执行创建OptionMenu模式的菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_ADD, 0, R.string.strAddButton);
menu.add(Menu.NONE, MENU_EDIT, 0, R.string.strEditButton);
menu.add(Menu.NONE, MENU_DELETE, 0, R.string.strDeleteButton);
return true;
}
这个函数是需要我们重载的,右键source ->Override/Implements Methods... 进入对话框之后找到这个函数勾选 确认
这个函数里面用到了这样的语句:menu.add(参数)
那么我们就 要学习这个函数是怎么用的,先看下API文档的介绍:
public abstract MenuItem add (CharSequence title)
Since:
API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
title The text to display for the item.
Returns
- The newly added menu item.
public abstract MenuItem add (int groupId, int itemId, int order, int titleRes)
Since:
API Level 1
Variation on add(int, int, int, CharSequence)
that takes a string resource identifier instead of the string itself.
Parameters
groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE
if an item should not be in a group. itemId Unique item ID. Use NONE
if you do not need a unique ID. order The order for the item. Use NONE
if you do not care about the order. See getOrder()
. titleRes Resource identifier of title string.
Returns
- The newly added menu item.
public abstract MenuItem add (int titleRes)
Since:
API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
titleRes Resource identifier of title string.
Returns
- The newly added menu item.
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Since:
API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters
groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE
if an item should not be in a group. itemId Unique item ID. Use NONE
if you do not need a unique ID. order The order for the item. Use NONE
if you do not care about the order. See getOrder()
. title The text to display for the item.
Returns
- The newly added menu item.
上面就是API提供的所有Menu的add()函数的说明情况
public abstract MenuItem add (CharSequence title)
Add a new item to the menu. This item displays the given title for its label.
Parameters
title | The text to display for the item. |
---|
Returns
- The newly added menu item.
public 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.
Parameters
groupId | The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group. |
---|---|
itemId | Unique item ID. Use NONE if you do not need a unique ID. |
order | The order for the item. Use NONE if you do not care about the order. See getOrder() . |
titleRes | Resource identifier of title string. |
Returns
- The newly added menu item.
public abstract MenuItem add (int titleRes)
Add a new item to the menu. This item displays the given title for its label.
Parameters
titleRes | Resource identifier of title string. |
---|
Returns
- The newly added menu item.
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Add a new item to the menu. This item displays the given title for its label.
Parameters
groupId | The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group. |
---|---|
itemId | Unique item ID. Use NONE if you do not need a unique ID. |
order | The order for the item. Use NONE if you do not care about the order. See getOrder() . |
title | The text to display for the item. |
Returns
- The newly added menu item.
Since: API Level 1
Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself.
Parameters:参数
groupId:The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId:Unique item ID. Use NONE if you do not need a unique ID.这个好理解,就是菜单的id号,比如添加菜单id号,删除菜单id 号等等
order:The order for the item. Use NONE if you do not care about the order. See getOrder().菜单显示的顺序 如果不关系他们的顺序的话就用NONE,我们一般用0,是个int类型
titleRes:Resource identifier of title string.资源索引号,它代表的意思是菜单的文本定义 比如添加菜单的 我们要写上 “添加”
Returns:The newly added menu item.返回值
protected final static int MENU_EDIT = Menu.FIRST + 1;
protected final static int MENU_DELETE = Menu.FIRST + 2;
<string name="strEditButton">修改</string>
<string name="strAddButton">新增</string>
<string name="strDeleteButton">刪除</string>
</resources>
到这里菜单的创建就完成了,接下来就是要处理菜单的点击响应事件了
2.菜单事件处理:
这个时候我们需要去重载菜单选项选中事件处理函数:public boolean onOptionsItemSelected(MenuItem item)
注意到这个参数是:MenuItem item 在我们创建的时候,他返回的就是这个类型的。
//菜单被选中的响应事件 参数是选中的那个菜单item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case MENU_ADD:
this.addTodo();
//处理你要做的操作
break;
case MENU_EDIT:
this.editTodo();
//处理你要做的操作
break;
case MENU_DELETE:
this.deleteTodo();
//处理你要做的操作
break;
}
return true;
}
这个函数非常的简单了,就不在介绍了。
3.关于OptionsMenu模式菜单的知识补充和总结:
菜单 menu
1、选项菜单 OptionsMenu
2、上下文菜单 ContextMenu
3、子菜单 SubMenu
菜单是用户界面中最常见的元素,使用也非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),下面分别举例说明。
一、选项菜单 OptionsMenu
Android手机上有个Menu按键,当Menu按下的时候,每个Activity都可以选择处理这一请求,在屏幕底部弹出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu,一般情况下,选项菜单最多显示2排每排3个菜单项,这些菜单项有文字有图标,也被称作Icon Menus,如果多于6项,从第六项开始会被隐藏,在第六项会出现一个More里,点击More才出现第六项以及以后的菜单项,这些菜单项也被称作Expanded Menus。
我们来看看一个模板:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainHelloMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// 点击Menu时,系统调用当前Activity的onCreateOptionsMenu方法,并传一个实现了一个Menu接口的menu对象供你使用
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
* add()方法的四个参数,依次是:
* 1、组别,如果不分组的话就写Menu.NONE,
* 2、Id,这个很重要,Android根据这个Id来确定不同的菜单
* 3、顺序,那个菜单现在在前面由这个参数的大小决定
* 4、文本,菜单的显示文本
*/
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon(
android.R.drawable.ic_menu_delete);
// setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
// android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon(
android.R.drawable.ic_menu_send);
// return true才会起作用
return true;
}
//菜单项被选择事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show();
break;
}
return false;
}
//选项菜单被关闭事件,菜单被关闭有三种情形,menu按钮被再次点击、back按钮被点击或者用户选择了某一个菜单项
@Override
public void onOptionsMenuClosed(Menu menu){
Toast.makeText(this, "选项菜单关闭了", Toast.LENGTH_LONG).show();
}
//菜单被显示之前的事件
@Override
public boolean onPrepareOptionsMenu(Menu menu){
Toast.makeText(this, "选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单", Toast.LENGTH_LONG).show();
//如果返回false,此方法就把用户点击menu的动作给消费了,onCreateOptionsMenu方法将不会被调用
return true;
}
}