如何在列表框项中响应动作
文章信息
兼容于
平台: S60 1st Edition, S60 2nd Edition and FP1, FP2, FP3, S60 3rd Edition and FP1
文章
翻译:
由 huwell
最后由 hamishwillee
在 08 Aug 2012 编辑
描述
下面的代码示例演示了如何响应用户选定的列表框项。
这里介绍一下如何生成一个简单列表框 How to create a simple listbox in Symbian C++
我们可以捕捉任何键盘事件借以产生各种响应(如弹出一个对话框),我们可以通过OfferKeyEventL()方法来处理
解决方案
TKeyResponse CMyExampleAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
if(aType != EEventKey)
{
return EKeyWasNotConsumed;
}
switch(aKeyEvent.iCode)
{
case EKeyUpArrow:
case EKeyDownArrow:
{
// Forward up and down key press events to the list box
return iListBox->OfferKeyEventL( aKeyEvent, aType );
}
case EKeyOK: // display an information note when item is selected
{
_LIT(KFormatMessage, "Selected item: %d");
TInt idx = iListBox->CurrentItemIndex();
TBuf<32> message;
message.Format(KFormatMessage, idx);
CAknInformationNote* Note = new (ELeave) CAknInformationNote;
Note->ExecuteLD(message);
return EKeyWasConsumed;
}
default:
break;
}
return EKeyWasNotConsumed;
}
注意要加上这行代码:
AddToStackL( iAppView );
在AppUi的构造函数ConstructL()中,要将该view/container添加到control stack中以便接收OfferKeyEventL()调用
响应list box的选择事件通常使用list box的观察器MEikListBoxObserver来实现:
class CYourContainer : public CCoeControl,public MEikListBoxObserver
{
public:
............
void HandleListBoxEventL(CEikListBox* aListBox, TListBoxEvent aEventType);
............
};
void CYourContainer ::HandleListBoxEventL( CEikListBox* aListBox, TListBoxEvent aEventType )
{
TInt index = aListBox->CurrentItemIndex();
if( index < 0 )
return;
if( aEventType == MEikListBoxObserver::EEventEnterKeyPressed )
{
_LIT(KFormatMessage, "Selected item: %d");
TBuf<32> message;
message.Format(KFormatMessage, index );
CAknInformationNote* Note = new (ELeave) CAknInformationNote;
Note->ExecuteLD(message);
}
}


(no comments yet)