亲宝软件园·资讯

展开

android创建optionsmenu android创建optionsmenu的方法

craigyang 人气:0
想了解android创建optionsmenu的方法的相关内容吗,craigyang在本文为您仔细讲解android创建optionsmenu的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,创建,optionsmenu,下面大家一起来学习吧。

本文实例讲述了android创建optionsmenu的方法。分享给大家供大家参考。具体实现方法如下:

一、用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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论