Android手机有一个"Menu"键,用来专门显示菜单,要实现菜单功能,需要通过onCreateOptionsMenu()来创建菜单,然后需要对其能够触发的事件进行监听,这样才能够在事件监听onOptionsItemSelected中根据不同的菜单选项来执行不同的任务。
添加菜单日有两种方式,一种是XAL布局,另一种是通过Menu.add()方法添加。
第一种,通过XML布局,在res下创建menu目录,在里面添加menu.xml文件,内容:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/about" android:id="@+id/about"> </item> <item android:title="@string/exit" android:id="@+id/exit"> </item> </menu>
第二种,Menu.add(0,0,0,R.string.btn_name)方示添加:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,R.string.ok);
menu.add(0,0,0,R.string.back);
return true;
}
效果图:
代码:
1.res/values/string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="about_me">详细内容可登录http://www.80080088.com了解!</string> <string name="main_menu">这里演示的是Andrid菜单功能!</string> <string name="app_name">菜单</string> <string name="about">关于</string> <string name="exit">退出</string> <string name="ok">确定</string> <string name="back">返回</string> </resources>
2.res/layout/main.xml和about.xml
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/main_menu" /> </LinearLayout>
about.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/about_me"/> </LinearLayout>
3,res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/about" android:id="@+id/about"> </item> <item android:title="@string/exit" android:id="@+id/exit"> </item> </menu>
4.A011Activity.java和AboutActivity.java
A011Activity.java
package com.cottsoft.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class A011Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int item_id = item.getItemId();
switch (item_id) {
case R.id.about:
Intent intent = new Intent();
intent.setClass(A011Activity.this, AboutActivity.class);
startActivity(intent);
A011Activity.this.finish();
break;
case R.id.exit:
A011Activity.this.finish();
break;
}
return true;
}
}
AboutActivity.java
/**********************************************
* @author Simon Hoo (simon@cottsoft.com)
* @contact QQ:9930323 MSN: simon_hoo@msn.com
* @OnlineSupport: http://www.cottsoft.com
* @Create 2011-4-4
* @version V1.0
********************************************/
package com.cottsoft.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class AboutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,R.string.ok);
menu.add(0,0,0,R.string.back);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int item_id = item.getItemId();
switch(item_id){
case 0:
case 1:
Intent intent = new Intent();
intent.setClass(AboutActivity.this, A011Activity.class);
startActivity(intent);
AboutActivity.this.finish();
break;
}
return true;
}
}