Symbian中的对话框
UI有的时候还是有要求的,收录一下,以后需要的时候,查一下就OK.
1、非阻塞提示框
symbian定义了几个提示类,分别是:
confirm类:CAknConfirmationNote
info类: CAknInformationNote
warning类:CAknWarningNote
error类: CAknErrorNote
头文件:aknnotewrappers.h
lib:avkon.lib eikcdlg.lib eikctl.lib
使用方法:
TBuf<32> buf; buf.Copy(_L("info note")); CAknInformationNote* iInfoNote = new (ELeave) CAknInformationNote; iInfoNote->ExecuteLD(buf);
此类提示框不阻塞线程,ExecuteLD执行显示提示框后,后面的语句继续执行。
2、阻塞提示框
void CEikonEnv::AlertWin(const TDesC& aMsg);
void CEikonEnv::AlertWin(const TDesC& aMsg1,const TDesC& aMsg2);
static void CEikonEnv::InfoWinL(const TDesC& aFirstLine,const TDesC& aSecondLine);
AlertWin为CEikonEnv类的非静态成员函数,InfoWinL为CEikonEnv类的静态成员函数。
AlertWin只能在ui、view和container中使用,使用方法如下:
iEikonEnv->AlertWin(_L("text"));
InfoWinL可以在任意类中使用,使用方法如下:
CEikonEnv::Static()->InfoWinL(_L("note:"), _L("text"));
为方便使用,常定义宏来使用这类提示框,如:
#define DEBUG_DIALOG(x) iEikonEnv->AlertWin(##x); #define DEBUG_DIALOG1(x) CEikonEnv::Static()->InfoWinL(_L("note:"), ##x); #define DEBUG_DIALOG2(x,y) CEikonEnv::Static()->InfoWinL(##x, ##y); 可以这么使用: TBuf<32> buf; buf.Copy(_L("test")); DEBUG_DIALOG(buf); DEBUG_DIALOG1(buf); DEBUG_DIALOG2(buf,_L("text"));
此类提示框在S60 2nd中可阻塞线程,只有用户按键退出提示框后,后面的程序才能接着运行;在S60 3rd中不阻塞线程。
3、进度条对话框
进度条对话框类为:
CAknProgressDialog
头文件:aknprogressdialog.h
lib: avkon.lib eikcdlg.lib eikctl.lib
使用方法:
//初始化进度条 CAknProgressDialog* iProgressDialog; CEikProgressInfo* iProgressInfo; iProgressDialog = new ( ELeave ) CAknProgressDialog( reinterpret_cast <CEikDialog**> ( &iProgressDialog ) ); iProgressDialog->SetCallback( this ); iProgressDialog->PrepareLC( R_RESOURCE_PROGRESS_NOTE ); //从资源文件构造对话框,资源见下面的定义 iProgressInfo = iProgressDialog->GetProgressInfoL(); iProgressInfo->SetFinalValue( aMaxValue ); //设置进度条的最大值(结束值) iProgressDialog->RunLD(); //更新进度条 iProgressInfo->IncrementAndDraw( aStep ); //结束进度条 iProgressDialog->ProcessFinishedL(); delete iProgressDialog; RESOURCE DIALOG R_RESOURCE_PROGRESS_NOTE //进度条对话框资源 { flags = EAknProgressNoteFlags; buttons = R_AVKON_SOFTKEYS_CANCEL; items = { DLG_LINE { type = EAknCtNote; id = EMagicBoxCtrlIdProgressNote; control = AVKON_NOTE { layout = EProgressLayout; singular_label = "对话框中显示的文字"; plural_label = "download"; imagefile = AVKON_BMPFILE_NAME; //第二版中 图标文件为 #define AVKON_BMPFILE_NAME "z://system//data//avkon.mbm" imageid = EMbmAvkonQgn_note_sml; //这两项可更改显示不同图标 imagemask = EMbmAvkonQgn_note_sml_mask; }; } }; }
4、等待对话框
等待对话框要用到的类:
CAknGlobalNote
头文件:aknglobalnote.h
lib:aknnotify.lib eiksrv.lib
使用方法:
//显示等待对话框 CAknGlobalNote* globalNote = CAknGlobalNote::NewL(); CleanupStack::PushL( globalNote ); TInt iWaitNoteId = globalNote->ShowNoteL( EAknGlobalWaitNote, _L("对话框中显示的文字") ); CleanupStack::PopAndDestroy(); //结束等待对话框 CAknGlobalNote * note = CAknGlobalNote::NewL(); CleanupStack::PushL( note ); note->CancelNoteL( iWaitNoteId ); CleanupStack::PopAndDestroy();
注:
CAknGlobalNote类除了显示等待对话框外还可显示多种类型的全局对话框,具体类型可通过ShowNoteL的第一个参数指定,可能的类型如下:
enum TAknGlobalNoteType { EAknGlobalInformationNote = 1, EAknGlobalWarningNote, EAknGlobalConfirmationNote, EAknGlobalErrorNote, EAknGlobalChargingNote, EAknGlobalWaitNote, EAknGlobalPermanentNote, EAknGlobalNotChargingNote, EAknGlobalBatteryFullNote, EAknGlobalBatteryLowNote, EAknGlobalRechargeBatteryNote, EAknCancelGlobalNote, EAknGlobalTextNote };
5、询问对话框
询问对话框用到的类:
CAknQueryDialog
头文件:AknQueryDialog.h
lib:avkon.lib
使用方法:
CAknQueryDialog* dlg; dlg = CAknQueryDialog::NewL( CAknQueryDialog::ENoTone ); dlg->PrepareLC( R_RESOURCE_QUERY_DIALOG ); //从资源文件构造对话框,资源见下面的定义 TInt ret = dlg->RunLD(); //若用户选择“是”,返回非0,选择“否”,则返回0 RESOURCE DIALOG R_RESOURCE_QUERY_DIALOG //询问对话框资源 { flags = EGeneralQueryFlags; buttons = R_AVKON_SOFTKEYS_YES_NO; //CBA显示“是”和“否”两个按钮 items = { DLG_LINE { type = EAknCtQuery; id = EGeneralQuery; control = AVKON_CONFIRMATION_QUERY //表示这是confirm询问对话框,用户选择“是”或“否” { layout = EConfirmationQueryLayout; label = "对话框中显示的文字"; }; } }; }
此类对话框可以有声音提示,由NewL的const TTone& aTone参数指定,可能的值如下:
enum TTone { /// No tone is played ENoTone = 0, /// A confirmation tone is played EConfirmationTone = EAvkonSIDConfirmationTone, /// A warning tone is played EWarningTone = EAvkonSIDWarningTone, /// An error tone is played EErrorTone = EAvkonSIDErrorTone };
通过定义不同的询问对话框资源,可实现不同的询问对话框,如让用户输入文字的询问对话框资源定义如下:
RESOURCE DIALOG R_RESOURCE_DATA_QUERY { flags = EGeneralQueryFlags; buttons = R_AVKON_SOFTKEYS_OK_CANCEL; //CBA按钮显示“确定”和“取消” items = { DLG_LINE { type = EAknCtQuery; id = EGeneralQuery; control = AVKON_DATA_QUERY //表示这是data询问对话框,需要用户输入内容 { layout = EDataLayout; label = "提示内容"; control = EDWIN { flags = EEikEdwinNoHorizScrolling | EEikEdwinResizable; width = 30; lines = 2; maxlength = 159; }; }; } }; }
使用方法:
TBuf<128> msg; CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(msg,CAknQueryDialog::ENoTone); TInt ret = dlg->ExecuteLD(R_RESOURCE_DATA_QUERY);
用户输入内容后按“确定”,内容就存储到msg中,函数返回非0;按“取消”,函数返回0。
这里用到的类是CAknQueryDialog的子类CAknTextQueryDialog。
CAknQueryDialog的子类有:
CAknFloatingPointQueryDialog //This class should be used when user is reguest to enter a flotaing point number CAknFixedPointQueryDialog //... CAknDurationQueryDialog //This class should be used when user is reguest to enter duration CAknIpAddressQueryDialog //This class should be used when user is reguest to enter IP address,@since 2.1 CAknMultiLineDataQueryDialog //Query Dialog with data input on more than one line (2 lines at the moment) Create using NewL methods and passing parameters as appropriate. Attention: When deriving from this class, you must call SetDataL during second phase construction. CAknMultiLineIpQueryDialog //... CAknNumberQueryDialog //This class should be used when user is reguest to enter number CAknTextQueryDialog //This class should be used when user is reguest to enter plain text, secret text, phonenumber or PIN-code CAknTimeQueryDialog //This class should be used when user is reguest to enter time or date
使用不同的类,资源文件会有所不同。
另外,在资源中定义EDWIN时,可指定输入发,如:
control = EDWIN { flags = EEikEdwinNoHorizScrolling | EEikEdwinResizable; width = 11; lines = 1; maxlength = 11; avkon_flags = EAknEditorFlagFixedCase | EAknEditorFlagNoT9 | EAknEditorFlagSupressShiftMenu; //EAknEditorFlagSupressShiftMenu屏蔽切换输入法键 allowed_input_modes = EAknEditorNumericInputMode; default_input_mode = EAknEditorNumericInputMode; numeric_keymap = EAknEditorPlainNumberModeKeymap; };
以上写法表示默认输入法为数字,并且屏蔽了输入法切换键,即不能通过输入法切换键来切换输入法。
6、编辑框
编辑框使用的类:
CEikGlobalTextEditor
头文件:eikgted.h
使用方法:
CEikGlobalTextEditor* iGKeyEd; TBuf<128> iKeyText; TResourceReader reader; iCoeEnv->CreateResourceReaderLC( reader, R_RESOURCE_EDITOR ); //从资源文件构造编辑框,资源见下面的定义 iGKeyEd = new ( ELeave ) CEikGlobalTextEditor; iGKeyEd->SetContainerWindowL( *this ); iGKeyEd->ConstructFromResourceL( reader ); CleanupStack::PopAndDestroy(); // Resource reader //设置编辑框的初始文本和位置,编辑框大小在资源中定义 TBuf<32> buf; buf.Copy(_L("demo")); iGKeyEd->SetTextL(&buf); iGKeyEd->SetExtent( TPoint(5,2), iGKeyEd->MinimumSize() ); iGKeyEd->SetFocus(ETrue); // iGKeyEd->SetReadOnly(ETrue); //设置编辑框为只读 //使文字居中 CParaFormat paraFormat; TParaFormatMask paraFormatMask; paraFormatMask.SetAttrib( EAttAlignment ); // set mask paraFormat.iHorizontalAlignment = CParaFormat::ECenterAlign; iGKeyEd->ApplyParaFormatL( ¶Format, paraFormatMask ); iGKeyEd->GetText(iKeyText); //获取编辑框中的内容,保存到iKeyText中 RESOURCE GTXTED R_RESOURCE_EDITOR //编辑框资源 { flags = EAknEditorFlagDefault; width = 53; height = 16; numlines = 1; textlimit= 1; fontcontrolflags = EGulFontControlAll; fontnameflags = EGulNoSymbolFonts; //这里也可设置输入法 // avkon_flags = EAknEditorFlagFixedCase | EAknEditorFlagNoT9 | EAknEditorFlagSupressShiftMenu; //EAknEditorFlagSupressShiftMenu屏蔽切换输入法键 // allowed_input_modes = EAknEditorNumericInputMode; // default_input_mode = EAknEditorNumericInputMode; // numeric_keymap = EAknEditorPlainNumberModeKeymap; }
注意,要使编辑框正常显示,记得更改container的CountComponentControls和ComponentControl函数,正确处理控件数目和编辑框指针。另外,要使编辑框能正常接收按键事件,要显示调用编辑框的OfferKeyEventL函数,如下:
TKeyResponse CMobileGuardSetKeyContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType ) { return iGKeyEd->OfferKeyEventL( aKeyEvent, aType ); }