1、非阻塞提示框
symbian定義了幾個(gè)提示類(lèi),分別是:
confirm類(lèi):CAknConfirmationNote
info類(lèi): CAknInformationNote
warning類(lèi):CAknWarningNote
error類(lèi):CAknErrorNote
頭文件:aknnotewrappers.h
lib: avkon.lib eikcdlg.lib eikctl.lib
Code:
TBuf<32> buf;
buf.Copy(_L("info note"));
CAknInformationNote* iInfoNote = new (ELeave) CAknInformationNote;
iInfoNote->ExecuteLD(buf);
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類(lèi)的非靜態(tài)成員函數(shù),InfoWinL為CEikonEnv類(lèi)的靜態(tài)成員函數(shù)。
AlertWin只能在ui、view和container中使用,使用方法如下:
Code:
iEikonEnv->AlertWin(_L("text"));
InfoWinL可以在任意類(lèi)中使用,使用方法如下:
Code:
CEikonEnv::Static()->InfoWinL(_L("note:"), _L("text"));
為方便使用,常定義宏來(lái)使用這類(lèi)提示框,如:
Code:
#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"));
此類(lèi)提示框阻塞線(xiàn)程,只有用戶(hù)按鍵退出提示框后,后面的程序才能接著運(yùn)行。
3、進(jìn)度條對(duì)話(huà)框
進(jìn)度條對(duì)話(huà)框類(lèi)為:
CAknProgressDialog
頭文件:aknprogressdialog.h
lib:avkon.lib eikcdlg.lib eikctl.lib
Code:
//初始化進(jìn)度條
CAknProgressDialog* iProgressDialog;
CEikProgressInfo* iProgressInfo;
iProgressDialog = new ( ELeave ) CAknProgressDialog( reinterpret_cast
<CEikDialog**>
( &iProgressDialog ) );
iProgressDialog->SetCallback( this );
iProgressDialog->PrepareLC( R_RESOURCE_PROGRESS_NOTE ); //從資源文件構(gòu)造對(duì)話(huà)框,資源見(jiàn)下面的定義
iProgressInfo = iProgressDialog->GetProgressInfoL();
iProgressInfo->SetFinalValue( aMaxValue ); //設(shè)置進(jìn)度條的最大值(結(jié)束值)
iProgressDialog->RunLD();
//更新進(jìn)度條
iProgressInfo->IncrementAndDraw( aStep );
//結(jié)束進(jìn)度條
iProgressDialog->ProcessFinishedL();
delete iProgressDialog;
RESOURCE DIALOG R_RESOURCE_PROGRESS_NOTE //進(jìn)度條對(duì)話(huà)框資源
{
flags = EAknProgressNoteFlags;
buttons = R_AVKON_SOFTKEYS_CANCEL;
items =
{
DLG_LINE
{
type = EAknCtNote;
id = EMagicBoxCtrlIdProgressNote;
control = AVKON_NOTE
{
layout = EProgressLayout;
singular_label = "對(duì)話(huà)框中顯示的文字";
plural_label = "download";
imagefile = AVKON_BMPFILE_NAME; //第二版中 圖標(biāo)文件為 #define AVKON_BMPFILE_NAME "z:\\system\\data\\avkon.mbm"
imageid = EMbmAvkonQgn_note_sml; //這兩項(xiàng)可更改顯示不同圖標(biāo)
imagemask = EMbmAvkonQgn_note_sml_mask;
};
}
};
}
注意:In Series 60 3rd edition, avkon.mbm was replaced by avkon2.mbm and moved to a different location (and the SDK forgot to mention this and still provides erroneous location).
Old entry:
imagefile = "z:\\system\data\avkon.mbm";
New entry:
imagefile = "z:\\resource\apps\avkon2.mbm";
4、等待對(duì)話(huà)框
等待對(duì)話(huà)框要用到的類(lèi):CAknGlobalNote
頭文件:aknglobalnote.h
lib:aknnotify.lib eiksrv.lib
Code:
//顯示等待對(duì)話(huà)框
CAknGlobalNote* globalNote = CAknGlobalNote::NewL();
CleanupStack::PushL( globalNote );
TInt iWaitNoteId = globalNote->ShowNoteL( EAknGlobalWaitNote, _L("對(duì)話(huà)框中顯示的文字") );
CleanupStack::PopAndDestroy();
//結(jié)束等待對(duì)話(huà)框
CAknGlobalNote * note = CAknGlobalNote::NewL();
CleanupStack::PushL( note );
note->CancelNoteL( iWaitNoteId );
CleanupStack::PopAndDestroy();
注意:
CAknGlobalNote類(lèi)除了顯示等待對(duì)話(huà)框外還可顯示多種類(lèi)型的全局對(duì)話(huà)框,具體類(lèi)型可通過(guò)ShowNoteL的第一個(gè)參數(shù)指定,可能的類(lèi)型如下:
Code:
enum TAknGlobalNoteType
{
EAknGlobalInformationNote = 1,
EAknGlobalWarningNote,
EAknGlobalConfirmationNote,
EAknGlobalErrorNote,
EAknGlobalChargingNote,
EAknGlobalWaitNote,
EAknGlobalPermanentNote,
EAknGlobalNotChargingNote,
EAknGlobalBatteryFullNote,
EAknGlobalBatteryLowNote,
EAknGlobalRechargeBatteryNote,
EAknCancelGlobalNote,
EAknGlobalTextNote
};
5、詢(xún)問(wèn)對(duì)話(huà)框
詢(xún)問(wèn)對(duì)話(huà)框用到的類(lèi):CAknQueryDialog
頭文件:AknQueryDialog.h
lib:avkon.lib
Code:
CAknQueryDialog* dlg;
dlg = CAknQueryDialog::NewL( CAknQueryDialog::ENoTone );
dlg->PrepareLC( R_RESOURCE_QUERY_DIALOG ); //從資源文件構(gòu)造對(duì)話(huà)框,資源見(jiàn)下面的定義
TInt ret = dlg->RunLD(); //若用戶(hù)選擇“是”,返回非0,選擇“否”,則返回0
RESOURCE DIALOG R_RESOURCE_QUERY_DIALOG //詢(xún)問(wèn)對(duì)話(huà)框資源
{
flags = EGeneralQueryFlags;
buttons = R_AVKON_SOFTKEYS_YES_NO; //CBA顯示“是”和“否”兩個(gè)按鈕
items =
{
DLG_LINE
{
type = EAknCtQuery;
id = EGeneralQuery;
control = AVKON_CONFIRMATION_QUERY //表示這是confirm詢(xún)問(wèn)對(duì)話(huà)框,用戶(hù)選擇“是”或“否”
{
layout = EConfirmationQueryLayout;
label = "對(duì)話(huà)框中顯示的文字";
};
}
};
}
此類(lèi)對(duì)話(huà)框可以有聲音提示,由NewL的const TTone& aTone參數(shù)指定,可能的值如下:
Code:
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
};
通過(guò)定義不同的詢(xún)問(wèn)對(duì)話(huà)框資源,可實(shí)現(xiàn)不同的詢(xún)問(wèn)對(duì)話(huà)框,如讓用戶(hù)輸入文字的詢(xún)問(wèn)對(duì)話(huà)框資源定義如下:
Code:
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詢(xún)問(wèn)對(duì)話(huà)框,需要用戶(hù)輸入內(nèi)容
{
layout = EDataLayout;
label = "提示內(nèi)容";
control = EDWIN
{
flags = EEikEdwinNoHorizScrolling | EEikEdwinResizable;
width = 30;
lines = 2;
maxlength = 159;
};
};
}
};
}
Code:
TBuf<128> msg;
CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(msg,CAknQueryDialog::ENoTone);
TInt ret = dlg->ExecuteLD(R_RESOURCE_DATA_QUERY);
用戶(hù)輸入內(nèi)容后按“確定”,內(nèi)容就存儲(chǔ)到msg中,函數(shù)返回非0;按“取消”,函數(shù)返回0。
這里用到的類(lèi)是CAknQueryDialog的子類(lèi)CAknTextQueryDialog。
CAknQueryDialog的子類(lèi)有:
Code:
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
使用不同的類(lèi),資源文件會(huì)有所不同。
另外,在資源中定義EDWIN時(shí),可指定輸入發(fā),如:
Code:
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;
};
以上寫(xiě)法表示默認(rèn)輸入法為數(shù)字,并且屏蔽了輸入法切換鍵,即不能通過(guò)輸入法切換鍵來(lái)切換輸入法。
6、編輯框
編輯框使用的類(lèi):CEikGlobalTextEditor
頭文件:eikgted.h
Code:
CEikGlobalTextEditor* iGKeyEd;
TBuf<128> iKeyText;
TResourceReader reader;
iCoeEnv->CreateResourceReaderLC( reader, R_RESOURCE_EDITOR ); //從資源文件構(gòu)造編輯框,資源見(jiàn)下面的定義
iGKeyEd = new ( ELeave ) CEikGlobalTextEditor;
iGKeyEd->SetContainerWindowL( *this );
iGKeyEd->ConstructFromResourceL( reader );
CleanupStack::PopAndDestroy(); // Resource reader
//設(shè)置編輯框的初始文本和位置,編輯框大小在資源中定義
TBuf<32> buf;
buf.Copy(_L("demo"));
iGKeyEd->SetTextL(&buf);
iGKeyEd->SetExtent( TPoint(5,2), iGKeyEd->MinimumSize() );
iGKeyEd->SetFocus(ETrue);
// iGKeyEd->SetReadOnly(ETrue); //設(shè)置編輯框?yàn)橹蛔x
//使文字居中
CParaFormat paraFormat;
TParaFormatMask paraFormatMask;
paraFormatMask.SetAttrib( EAttAlignment ); // set mask
paraFormat.iHorizontalAlignment = CParaFormat::ECenterAlign;
iGKeyEd->ApplyParaFormatL( ¶Format, paraFormatMask );
iGKeyEd->GetText(iKeyText); //獲取編輯框中的內(nèi)容,保存到iKeyText中
RESOURCE GTXTED R_RESOURCE_EDITOR //編輯框資源
{
flags = EAknEditorFlagDefault;
width = 53;
height = 16;
numlines = 1;
textlimit= 1;
fontcontrolflags = EGulFontControlAll;
fontnameflags = EGulNoSymbolFonts;
//這里也可設(shè)置輸入法
// avkon_flags = EAknEditorFlagFixedCase |
EAknEditorFlagNoT9 | EAknEditorFlagSupressShiftMenu; //EAknEditorFlagSupressShiftMenu屏蔽切換輸入法鍵
// allowed_input_modes = EAknEditorNumericInputMode;
// default_input_mode = EAknEditorNumericInputMode;
// numeric_keymap = EAknEditorPlainNumberModeKeymap;
}
注意,要使編輯框正常顯示,記得更改container的CountComponentControls和ComponentControl函數(shù),正確處理控件數(shù)目和編輯框指針。另外,要使編輯框能正常接收按鍵事件,要顯示調(diào)用編輯框的OfferKeyEventL函數(shù),如下:
Code:
TKeyResponse CMobileGuardSetKeyContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
{
return iGKeyEd->OfferKeyEventL( aKeyEvent, aType );
}