在活动中使用 Menu

来源:互联网 发布:android php服务器 编辑:程序博客网 时间:2024/05/01 11:34

1、创建menu,创建main;

res-new-android resource directory选择menu,同样方法创建main.

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

<item android:id="@+id/add_item" android:title="Add"/>

<item android:id="@+id/remove_item" android:title="Remove"/>

</menu>

2、重写onCreateOptionsMenu()/onOptionsItemSelected()方法;

public boolean onCreateOptionsMenu(Menu menu) {

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

    return true;

}


public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){

    case R.id.add_item:

        Toast.makeText(this,"YouclickedAdd",Toast.LENGTH_SHORT).show();

    break;

        case R.id.remove_item: Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();

     break;

     default: }

    return true;

}


0 0
原创粉丝点击