×
Namespaces

Variants
Actions

处理edwin事件

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

文章
翻译:
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑
  • 详细描述

下列示例演示了如何处理edwin的文本更新事件。有时我们希望限制edwin中的内容,例如只能是数字或其它字符。

需要的头文件

#include <eikedwin.h>

增加下列内容到.rss中

RESOURCE EDWIN r_your_edwin
{
maxlength = 511;
default_case = EAknEditorTextCase;
allowed_case_modes = EAknEditorAllCaseModes;
numeric_keymap = EAknEditorStandardNumberModeKeymap;
default_input_mode = EAknEditorTextInputMode;
allowed_input_modes = EAknEditorAllInputModes;
special_character_table = R_AVKON_SPECIAL_CHARACTER_TABLE_DIALOG;
}


增加下列代码至头文件

class CYourContainer : public CCoeControl, public MEikEdwinObserver
{
public:
void HandleEdwinEventL(CEikEdwin *aEdwin, TEdwinEvent aEventType);
};


增加下列代码至cpp文件

//In your container class
 
CEikEdwin* yourEdwin = new (ELeave)CEikEdwin;
yourEdwin->SetEdwinObserver(this); //to ensure you get callbacks when anything happens in the edwin
 
// Create a resource reader that we'll use to get the settings
TResourceReader reader;
iEikonEnv->CreateResourceReaderLC(reader, R_YOUR_EDWIN);
yourEdwin->ConstructFromResourceL(reader);
CleanupStack::PopAndDestroy(); // reader
 
// Implement the HandleEdwinEventL in your container class
void CYourContainer::HandleEdwinEventL(CEikEdwin *aEdwin, TEdwinEvent aEventType)
{
if (aEventType == EEventTextUpdate)
{
TBuf<32> buf16;
aEdwin->GetText(buf16);
TBool needReDraw = EFalse;
for (TInt i = buf16.Length()-1; i > -1; i--)
{
if (!TChar(buf16[i]).IsDigit())
{
buf16.Delete(i, 1);
needReDraw = ETrue;
}
}
if (needReDraw )
{
aEdwin->SetTextL(&buf16);
aEdwin->HandleTextChangedL();
aEdwin->SetCursorPosL(buf16.Length(), EFalse);
}
}
}

这里的CEikRichTextEditor,调用SetTextL将直接导致ETEXT 12或EIKCOCTL 70异常,为了防止这样的事件HandleEdwinEventL需要做一些修改,这里需要做如下的修改

CEikRichTextEditor* editor = static_cast< CEikRichTextEditor* >(aEdwin);
CRichText* richText = editor->RichText();
richText->Reset();
richText->InsertL(0, buf16); // insert starting from 0 pos
//aEdwin->SetTextL(&buf16); // This causes the EText12 or EIKCOCTL 70 panics
aEdwin->HandleTextChangedL();
aEdwin->SetCursorPosL(buf16.Length(), EFalse);

现在HandleEdwinEvent方法获得aEdinw(指向你之前创建的edwin)如果用户有输入行为,它将会检查譬如是否是数字等,这样可以确保用户输入你想要的内容。

This page was last modified on 22 December 2011, at 07:42.
64 page views in the last 30 days.
×