Android:menu

代碼設置menu形式:
java

public class MainActivity extends Activity
{
    private final int MENU_ID_SETTING = 1;
    private final int MENU_ID_EXIT = 2;
                     
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
                     
    //重寫onCreateOptionsMenu建立菜單
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0,MENU_ID_SETTING,0,"設置");
        menu.add(0,MENU_ID_EXIT,0,"退出");
        return super.onCreateOptionsMenu(menu);
    }
                     
    //重寫onOptionsItemSelected設置菜單事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case MENU_ID_SETTING:
            Toast.makeText(this, "設置", Toast.LENGTH_LONG).show();
            break;
        case MENU_ID_EXIT:
            Toast.makeText(this, "退出", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}


清單配置形式:ide

public class MainActivity extends Activity
{
       
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
       
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //經過xml配置菜單
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.setmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item)
    {
        switch (item.getItemId())
        {
        case R.id.item1:
            Toast.makeText(this, "設置", Toast.LENGTH_LONG);
            break;
           
        default:
            break;
        }
        return super.onMenuItemSelected(featureId, item);
    }
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息