Android-How to create an option menu
This post learned from http://www.droidnova.com/how-to-create-an-option-menu,427.html,
droidnova.com is very good site about Android dev,.
Today we learn how you can create an option menu for your application.
Lets start with an empty android project. The package name will be com.droidnova.android.howto.optionmenu and the activity will have the name SimpleOptionMenu.
Our activity should now look very familiar to us:
package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.os.Bundle;
public class SimpleOptionMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
First we have to make a new folder in our res/ directory named menu. In this new directory we will create a new xml file named menu.xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
android:icon="@drawable/icon" />
<item android:id="@+id/text"
android:title="Text" />
<item android:id="@+id/icontext"
android:title="Icon and text"
android:icon="@drawable/icon" />
</menu>
The content of the xml file should be very self explaining. We have an id for each item, so we have a reference for it. The title attribute, if defined, is nothing more than the text you see in the option menu. The same with the icon attribute which references to an icon, in our case the default icon.
Now we have to modify our SimpleOptionMenu activity class. First we have to override the method onCreateOptionsMenu().
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
On line 2 you see the parameter menu. In this menu we will inflate our own menu defined by our menu.xml.
On line 3 we get the MenuInflater from the Activity class. We need this MenuInflater object to inflate, or merge, our own menu in the menu given as a parameter on line 2.
Line 4 inflate the given menu with our own menu. Line 5 is just our return type.
Some explanation: I talked a lot of menus so far, but we just have two different menus. As far as I understand, the menu given as a parameter always exist inside an activity, we simply don’t see it if we press the menu button. The reason should be clear: there is no standard menu defined so the menu has nothing to display. We change that by overriding this method and inflating our own menu.
Right now we can start our application and we will see our 3 option items in our menu when we press the menu button. As long as nothing happens when we press one item, the menu is senseless.
To implement a reaction of our menu, we should override another method named onOptionsItemSelected().
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
break;
case R.id.text: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
break;
}
return true;
}
On line 2 you see the parameter which works the same way we know from onTouchEvent(). The parameter item represents the item we pressed in our menu.
A simple switch/case construction with the item.getItemId() execute the code we want. In our case it is a simple Toast which will display a text that describes which item you touched.
Full sources: SimpleOptionMenu (full eclipse project)


Some Random Posts
- 2009/06/05 -- AS3-Drop Menu Demo (0)
- 2010/02/22 -- Android-ListView简单用法 (0)
- 2009/12/21 -- Js-复制到粘贴板 (0)
- 2009/12/04 -- Android-学习笔记(四) 重构 (0)
- 2010/02/05 -- AS3-aSpaceEscape 迷宫脱离游戏(三)角色部分 (0)
- 2009/10/20 -- Icons-地图程序标记素材分享 (0)
- 2009/12/14 -- APK-aPKMyFriends(名字大作战) (0)
- 2010/05/13 -- AS3-LightOut (8)
- 2009/05/22 -- About Flash (1)
- 2009/02/07 -- AS3-RegExp正则表达式 (0)
听说你要找我?
- QQ:104357575
- Gmail:k5angle9527@gmail.com
Categories
- AIR+FB+AS3 (169)
- Android (28)
- Demo (78)
- fDEV (3)
- Flash AS2 (66)
- Game (22)
- iPhone (1)
- jQuery (14)
- Others (95)
- PHP (43)
- Wordpress (60)
Recent Comments
- Anonymous says under Discuz!验证码总是错误:wuwu :xizao :wuwu :wenhao :touxiang :shuaya ...
- Anonymous says under 有关Flash 8 Video Encoder无法启动的问题:chaocai :shengtian 高级感谢~~~我找了一个晚上了,嗨哭命呀! 不过幸好...
- DFdou says under AS3-留言板(GuestBook)更新@DFdou, :leilei 这样啊,thx 哦
- lightoy says under AS3-留言板(GuestBook)更新@DFdou, a...不要把amfphp里面的.htaccess文件上传即可~_!
- Anonymous says under Android-ListView用法:zhuang
- DFdou says under AS3-留言板(GuestBook)更新@Lightoy, amfphp和.htaccess不会有冲突啊
- DFdou says under GAME-NamePK 基于MD5的名字PK(上)@, 随意~~
- Anonymous says under jQuery-each(fn)的使用:loveu :leilei :kua
- Lightoy says under AS3-留言板(GuestBook)更新hi,你好,因为wordpress生成的.htaccess文件导致测试amfphp时产生了500 I...
- Anonymous says under GAME-NamePK 基于MD5的名字PK(上):shengtian 博主我可以弄走做个养成pk游戏么?
Blogroll
Archive
- August 2010 (3)
- July 2010 (3)
- June 2010 (3)
- May 2010 (4)
- April 2010 (4)
- March 2010 (8)
- February 2010 (13)
- January 2010 (9)
- December 2009 (19)
- November 2009 (23)
- October 2009 (17)
- September 2009 (22)
- August 2009 (13)
- July 2009 (20)
- June 2009 (18)
- May 2009 (18)
- April 2009 (29)
- March 2009 (31)
- February 2009 (46)
- January 2009 (37)
- December 2008 (16)
- November 2008 (12)
- October 2008 (21)
- September 2008 (10)
- August 2008 (12)
- July 2008 (14)
- June 2008 (8)
- May 2008 (11)
February 7th, 2010 - 23:19
板大 你好 我有問題想請教
就是 我把許願板檔案 swf . sql . php . js 這三個檔案 上傳到虛擬空間後 SWF卻不行 後來我把php裡的帳號密碼跟主機改成 虛擬空間的資料後 還是不行 我不知道是哪出問題 可否請教一下 因為 我好喜歡許願板 , 這許願板 我在本機試過OK 自己電腦試過 也OK 但上傳到虛擬空間後 卻不行 不知道哪個環節出問題了 還有就是 SWF裡面的語法
//設定Service連線
var gateway:String = “/amfphp/gateway.php”;
connection.connect(gateway); //連接amfphp資料夾裡的gateway.php
這段 設定需要更改嗎?gateway.php 也需要一起上傳到虛擬空間嗎?
諸多問題 請多多見諒 因為我找好多資料好多書好多網站 都看不董 ><~
[Reply]
DFdou Reply:
2010-2-8 at 12:04 am
@JJ, 整个amfphp文件夹都要上传到虚拟空间里。
var gateway:String = “/amfphp/gateway.php”;这里是配置gateway的路径,根据对应的路径修改。
看你描述基本是路径的问题了,实在不行设置绝对路径看看
[Reply]
JJ Reply:
2010-2-8 at 4:59 am
那個路徑我不會設屋屋 可以教我嗎? 還有阿 我在我的日誌
http://blog.xuite.net/jinyin/Cindyrella/30871684
PO文章 有附上圖解 可以看一下咪~ 我有看到你的許願版 超漂亮的 我這也是 不過簡單板 我超喜歡你那許願板
真是不好意思 因為我沒碰過php跟flash結合 SORRY 給你添麻煩了
PS:你那個許願板 可以給我 檔案咪 >_< 因為我好喜歡喔
[Reply]
DFdou Reply:
2010-2-8 at 9:13 am
@JJ, 留言板下载地址在这里http://nwhy.org/as3-guestbook-doc-interface.html
February 8th, 2010 - 20:38
那個..我..有個..很..笨的問題 那就是…我安裝的 MySQL 資料庫系統 跟 PHPMyAdminy 資料夾管理程式 裡面所設定之 帳號 密碼 是不是 設定為 我申請MySQL虛擬空間 帳號 密碼 一樣呢?
[Reply]
DFdou Reply:
2010-2-8 at 10:32 pm
@JJ,
我想,恐怕,是的~~
[Reply]
February 8th, 2010 - 23:14
那就是 MySQL 資料庫系統 跟 PHPMyAdminy 資料夾管理程式 這兩個 可在本機跟虛擬空間 都適用了咪
[Reply]
DFdou Reply:
2010-2-8 at 11:42 pm
@JJ, 如果配置正确的话应该是木有问题
[Reply]