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程序设计有所帮助。
赞 (0)