android中optionsmenu的创建
来源:互联网 发布:数控g76内螺纹编程实例 编辑:程序博客网 时间:2024/05/22 09:48
一、用xml配置
在res文件夹下新建menu文件夹,menu下新建options_menu.xml文件,加入代码
1、 <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/about"
android:title="关于"
android:checkable="true" />
<item android:id="@+id/choose"
android:title="选择" />
<item android:id="@+id/quit"
android:title="QUIT" />
</menu>
2、activity类中加入代码
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem aitem)
{
super.onOptionsItemSelected(aitem);
switch(aitem.getItemId())
{
case R.id.about:
setTitle("你按下了'关于'菜单项");
progressBarHorizontal.setProgress(200);
openOptionsDialog();
break;
case R.id.choose:
setTitle("你按下了'选择'菜单项");
openOptionsChooseDialog();
break;
case R.id.quit:
setTitle("你按下了'离开'菜单项");
finish();
break;
}
return true;
}
private void openOptionsDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
private void openOptionsChooseDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.str_alert)
.setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{/*
String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog);
new AlertDialog.Builder(this)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();*/
}
})
.show();
}
OK。。。
二、直接用代码写
public static final int ABOUT_ID = Menu.FIRST;
public static final int CHOOSE_ID = Menu.FIRST + 1;
public static final int EXIT_ID = Menu.FIRST + 2;
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, ABOUT_ID, 0, R.string.app_about);
menu.add(0, CHOOSE_ID, 0, R.string.str_choose);
menu.add(0, EXIT_ID, 0, R.string.str_exit);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem aitem)
{
super.onOptionsItemSelected(aitem);
switch(aitem.getItemId())
{
case ABOUT_ID:
setTitle("你按下了'关于'菜单项");
progressBarHorizontal.setProgress(200);
openOptionsDialog();
break;
case CHOOSE_ID:
setTitle("你按下了'选择'菜单项");
openOptionsChooseDialog();
break;
case EXIT_ID:
setTitle("你按下了'离开'菜单项");
finish();
break;
}
return true;
}
private void openOptionsDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
private void openOptionsChooseDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.str_alert)
.setItems(R.array.items_irdc_dialog, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{/*
String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog);
new AlertDialog.Builder(this)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();*/
}
})
.show();
}
- android中optionsmenu的创建
- Android中选项菜单optionsMenu的创建及使用---onCreateOptionsMenu()等
- android的OptionsMenu
- android 关于OptionsMenu的应用
- Android optionsMenu,菜单的使用
- OptionsMenu Android
- Android 更改ActionBar OptionsMenu中字体颜色
- Android添加optionsmenu的两种方法
- android之OptionsMenu功能菜单的使用
- android -- ContextMenu 的使用 和 OptionsMenu 区别
- 解决Android 4.0以上版本中OptionsMenu菜单不显示ICON图标的问题
- 创建选项菜单OptionsMenu
- OptionsMenu创建使用
- Android OptionsMenu和 ContextMenu
- android学习之OptionsMenu
- Android OptionsMenu菜单
- 【Android】选项菜单optionsMenu
- android OptionsMenu 和 ContextMenu
- SQL SERVER 数据库 批量 脚本 升级 更新工具 (C# .Net 2.0)
- == equals null "" 比较
- 中国人被释放的个性 ----- 湖南电视台的成功(一)
- SqlDataReader有空值高效办法
- [导入]Java调用.net的WebService
- android中optionsmenu的创建
- 不完美的人生才是真的
- 枚举
- 如何在MFC应用程序中使用MAPI32.DLL发送电子邮件
- 多继承的虚指针
- F#入门
- Tomcat 5.5 JNDI Resource 配置
- 向C#进军!
- C++ 快速排序