本文实例讲述了Android选项菜单用法。分享给大家供大家参考。具体如下:
Android平台下所提供的菜单大体上可分为三类:选项菜单、上下文菜单和子菜单。
当Activity在前台运行时,如果用户按下手机上的Menu键,此时就会在屏幕低端弹出相应的选项菜单。但这个功能需要开发人员编程来实现的,如果在开发应用程序时没有实现该功能,那么程序运行时按下手机的meun键是不会起作用的。
对于携带图标的选项菜单,每次最多只能显示6个,当菜单选项多于6个时,将只显示前5个和一个扩展菜单选项,点击扩展菜单选项将会弹出其余的菜单项。扩展菜单项中将不会显示图标,但是可以显示单选按钮和复选框。
以下模拟选项菜单
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <ScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="fill_parent" android:editable="false" android:cursorVisible="false" android:text="您的选择为/n"> </EditText> </ScrollView></LinearLayout>
MeunActivity类:
package com.ljq.activity;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import android.view.MenuItem.OnMenuItemClickListener;import android.widget.EditText;public class MeunActivity extends Activity { private final int MENU_GENDER_MALE = 0; private final int MENU_GENDER_FEMALE = 1; private final int MENU_HOBBY1 = 2; private final int MENU_HOBBY2 = 3; private final int MENU_HOBBY3 = 4; private final int MENU_OK = 5; private final int MENU_GENDER = 6; private final int MENU_HOBBY = 7; private final int GENDER_GROUP = 0; private final int HOBBY_GROUP = 1; private final int MAIN_GROUP = 2; MenuItem[] hoddyMenuItems = new MenuItem[3];// 爱好菜单项组 MenuItem maleMenuItem = null;// 男性菜单项 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } /**j * 初始化选项菜单 */ @Override public boolean onCreateOptionsMenu(Menu menu) { //单选菜单选项 SubMenu genderMenu = menu.addSubMenu(MAIN_GROUP, MENU_GENDER, 0, "性别"); genderMenu.setIcon(R.drawable.gender); genderMenu.setHeaderIcon(R.drawable.gender); maleMenuItem = genderMenu.add(GENDER_GROUP, MENU_GENDER_MALE, 0, "男"); maleMenuItem.setChecked(true); genderMenu.add(GENDER_GROUP, MENU_GENDER_FEMALE, 0, "女"); //设置菜单项为单选菜单项,互斥的 genderMenu.setGroupCheckable(GENDER_GROUP, true, true); //复选菜单选项 SubMenu hobbyMenu = menu.addSubMenu(MAIN_GROUP,MENU_HOBBY,0,"爱好"); hobbyMenu.setIcon(R.drawable.hobby); hobbyMenu.setHeaderIcon(R.drawable.hobby); hoddyMenuItems[0]=hobbyMenu.add(HOBBY_GROUP, MENU_HOBBY1, 0, "游泳"); hoddyMenuItems[1]=hobbyMenu.add(HOBBY_GROUP, MENU_HOBBY2, 0, "唱歌"); hoddyMenuItems[2]=hobbyMenu.add(HOBBY_GROUP, MENU_HOBBY3, 0, "编程"); //设置菜单项为复选菜单项 hoddyMenuItems[0].setCheckable(true); hoddyMenuItems[1].setCheckable(true); hoddyMenuItems[2].setCheckable(true); //确定菜单项 MenuItem ok=menu.add(MAIN_GROUP,MENU_OK,0,"确定"); ok.setOnMenuItemClickListener(new OnMenuItemClickListener(){ public boolean onMenuItemClick(MenuItem item) { appendStateStr(); return true; } }); //给确定菜单项添加快捷键 ok.setAlphabeticShortcut('o');//设置字符快捷键 //ok.setNumericShortcut('1');//设置数字快捷键 //ok.setShortcut('a', '2');//同时设置两种快捷键。注意:同时设置多次时只有最后一个设置起作用 return true; // 记得返回true,否则无效 } /** * 在菜单选项中某个选项被选中时调用该事件 */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_GENDER_MALE: case MENU_GENDER_FEMALE: item.setChecked(true); appendStateStr(); break; case MENU_HOBBY1: case MENU_HOBBY2: case MENU_HOBBY3: item.setChecked(!item.isChecked()); appendStateStr(); break; } return true; } public void appendStateStr() { String result = "您选择的性别为:"; if (maleMenuItem.isChecked()) { result = result + "男"; } else { result = result + "女"; } String hobbyStr = ""; for (MenuItem hoddy : hoddyMenuItems) { if (hoddy.isChecked()) { hobbyStr = hobbyStr + hoddy.getTitle() + "、"; } } if (hobbyStr.length() > 0) { result = result + ",您的爱好为:" + hobbyStr.substring(0, hobbyStr.length() - 1) + "。/n"; } else { result = result + "。/n"; } EditText et = (EditText) MeunActivity.this.findViewById(R.id.editText); et.append(result); }}
运行结果:
希望本文所述对大家的Android程序设计有所帮助。
新闻热点
疑难解答
图片精选