×
Namespaces

Variants
Actions

使用Toolbar API - S60 Touch UI

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

代码示例
兼容于
文章
dougcn 在 10 Mar 2009 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

Contents

概述

Toolbar API是S60第5版Touch UI的功能。本文中,我们将学习如何用不同的方式使用Toolbar API。

什么是Toolbar API

Toolbar.png

如上图所示,Toolbar是一个用于放置不同UI部件的定位器(或占位符)。它可以固定或漂浮。可以在Toolbar放置按钮。

使用资源创建Toolbar

ToolbarWith2Buttons.png

定义资源

RESOURCE EIK_APP_INFO
{
menubar = r_menubar;
cba = R_AVKON_SOFTKEYS_OPTIONS_EXIT;
custom_app_info_extension = r_toolbar_ext; // This is the line we need to include to enable Toolbar
}
 
// Add following resource definition for toolbar declared in EIK_APP_INFO
RESOURCE EIK_APP_INFO_EXT r_toolbar_ext
{
popup_toolbar = r_test_toolbar;
}
 
RESOURCE AVKON_TOOLBAR r_test_toolbar
{
flags = KAknToolbarFixed;
items =
{
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand1; // In this example, we are using same set of commands as of used in AppUi:HandleCommandL
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "button1";
helptxt = "help text1";
}
};
};
},
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand2; // In this example, we are using same set of commands as of used in AppUi:HandleCommandL
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "button2";
helptxt = "help text2";
}
};
};
}
};
}


实现Toolbar API

  • 我们需要从Toolbar的观察者派生以便得到Toolbar事件。这个混入类就是MAknToolbarObserver,我们感兴趣的回调函数是:
  1. virtual void OfferToolbarEventL( TInt aCommand ) = 0;
  2. virtual void DynInitToolbarL( TInt aResourceId, CAknToolbar* aToolbar );

这里的virtual void OfferToolbarEventL( TInt aCommand ) = 0;是纯虚函数,因此我们需要至少在自己的应用程序中定义之。在工具条条目有事件发生时它将会被调用。

ToolbarAppUi.h

#include <akntoolbarobserver.h> // Include this header file for MAknToolbarObserver and Link against: eikcoctl.lib
.....
.....
class CToolbarAppUi : public CAknAppUi, public MAknToolbarObserver
{
....
....
//From MAknToolbarObserver
void OfferToolbarEventL(TInt aCommand);
....
....
};

ToolbarAppUi.cpp

void CToolbarAppUi::ConstructL()
{
......
 
//Set "this" to toolbar observer - We will receive events in OfferToolbarEventL
if(CurrentFixedToolbar())
{
CurrentFixedToolbar()->SetToolbarObserver(this);
}
.......
.......
}
 
//From MAknToolbarObserver
void CToolbarAppUi::OfferToolbarEventL(TInt aCommand)
{
HandleCommandL(aCommand); // In this example we are simply calling HandleCommandL by passing received command
}


使用资源创建有工具条扩展的Toolbar

ToolbarWithExtension.png

定义具有工具条扩展的资源

RESOURCE EIK_APP_INFO
{
menubar = r_menubar;
cba = R_AVKON_SOFTKEYS_OPTIONS_EXIT;
custom_app_info_extension = r_toolbar_ext; // 加入这行启用工具条
}
 
// Add following resource definition for toolbar declared in EIK_APP_INFO
RESOURCE EIK_APP_INFO_EXT r_toolbar_ext
{
popup_toolbar = r_test_toolbar;
}
 
RESOURCE AVKON_TOOLBAR r_test_toolbar
{
flags = KAknToolbarFixed;
items =
{
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand1; // We are using same set of commands as of used in AppUi:HandleCommandL
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "button1";
helptxt = "help text1";
}
};
};
},
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand2;
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "button2";
helptxt = "help text2";
}
};
};
},
TBAR_CTRL
{
type = EAknCtToolbarExtension; // Here are we are using Toolbar Extension which contains another set of buttons
control = AVKON_TOOLBAR_EXTENSION
{
flags = KAknTbExtensionTransparent;
items =
{
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand2;
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "b2";
helptxt = "help text2";
}
};
};
},
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand2;
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "b2";
helptxt = "help text2";
}
};
};
},
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand2;
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "b2";
helptxt = "help text2";
}
};
};
},
TBAR_CTRL
{
type = EAknCtButton;
id = ECommand2;
control = AVKON_BUTTON
{
flags = KAknButtonTextInsideFrame;
states =
{
AVKON_BUTTON_STATE
{
txt = "b2";
helptxt = "help text2";
}
};
};
 
}
};
};
}
};
}

ToolbarAppUi.cpp

  • 这里我们处理工具条条目的命令。
  • 当执行工具条扩展上的任何动作时,应隐藏工具表扩展。
//From MAknToolbarObserver
void CToolbarAppUi::OfferToolbarEventL(TInt aCommand)
{
//Check this condition for toolbar extension visibility and hide it when receiving events from item in it
if(CurrentFixedToolbar()->ToolbarExtension()->IsShown())
{
CurrentFixedToolbar()->ToolbarExtension()->SetShown(EFalse);
}
HandleCommandL(aCommand);
}

Toolbar API的限制

  • 最多允许放三个条目,包括工具条扩展
  • 工具条扩展上最多能放12个条目
  • 这个限制是为了让用户界面体验舒适

有用的函数

CAknToolbar

  • AddItemL()
  • RemoveItemL()
  • SetToolbarVisibility()
  • IsShown()
  • DisableToolbarL()
  • IsToolbarDisabled()

CAknToolbarExtension

  • AddItemL()
  • RemoveItemL()
  • IsShown()
  • SetShown()

MAknToolbarObserver

  • DynInitToolbarL()
  • OfferToolbarEventL()

关键字

Headers

  • #include <AknToolbarObserver.h>
  • #include <AknToolbar.h>

Classes

  • CAknToolbar
  • MAknToolbarObserver

Libraries

  • eikcoctl.lib

例子应用

相关链接

参考列表

This page was last modified on 15 December 2011, at 09:22.
103 page views in the last 30 days.
×