Android为Menu添加item及响应函数

来源:互联网 发布:武汉矩阵互动面试 编辑:程序博客网 时间:2024/05/02 02:40

参考 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给定的属性的意义。

0 0