您的位置:首页 > 移动开发 > Android开发

Android为Menu添加item及响应函数

2014-08-25 17:15 176 查看
参考 http://www.crifan.com/android_add_menu/
响应menu

@Override

public boolean onCreateOptionsMenu(Menu menu) {

Log.e(TAG,"onCreateOptionMenu");

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

menu.add("插入");



return true;

}

响应menu的条目

@Override

public boolean onOptionsItemSelected(MenuItem item) {

Log.e("ZHANGBIN","create option menu");

// Handle item selection

switch (item.getItemId()) {

case R.id.menu_discard:

Toast.makeText(MainActivity.this, "Menu Discard cliked", Toast.LENGTH_SHORT).show();

return true;

case R.id.menu_send:

Toast.makeText(MainActivity.this, "Menu Send cliked", Toast.LENGTH_SHORT).show();



return true;

default:

return super.onOptionsItemSelected(item);

}

}

menu的布局xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item

android:id="@+id/menu_discard"

android:orderInCategory="1"

android:showAsAction="ifRoom|withText"

android:title="试试"/>



<!-- android:icon="@drawable/error_white"

android:title="@string/discard"

android:icon="@drawable/forward_white"-->

<item

android:id="@+id/menu_send"

android:orderInCategory="2"

android:showAsAction="ifRoom|withText"

android:title="看看"/>

</menu>

没深入学习,可以参考

http://tech.it168.com/a2011/1031/1266/000001266576_all.shtml

学习下每个item的xml给定的属性的意义。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: