天天看點

Symbian中的對話框

Symbian中的對話框

UI有的時候還是有要求的,收錄一下,以後需要的時候,查一下就OK.

1、非阻塞提示框

symbian定義了幾個提示類,分别是:

confirm類:CAknConfirmationNote

info類: CAknInformationNote

warning類:CAknWarningNote

error類: 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);
        

此類提示框不阻塞線程,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中使用,使用方法如下:

Code:

iEikonEnv->AlertWin(_L("text"));
        

InfoWinL可以在任意類中使用,使用方法如下:

Code:

CEikonEnv::Static()->InfoWinL(_L("note:"), _L("text"));
        

為友善使用,常定義宏來使用這類提示框,如:

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"));
        

此類提示框在S60 2nd中可阻塞線程,隻有使用者按鍵退出提示框後,後面的程式才能接着運作;在S60 3rd中不阻塞線程。

3、進度條對話框

進度條對話框類為:

CAknProgressDialog

頭檔案:aknprogressdialog.h

lib: avkon.lib eikcdlg.lib eikctl.lib

使用方法:

Code:

//初始化進度條
 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

使用方法:

Code:

//顯示等待對話框
   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的第一個參數指定,可能的類型如下:

Code:

enum TAknGlobalNoteType
 {
 EAknGlobalInformationNote = 1,
 EAknGlobalWarningNote,
 EAknGlobalConfirmationNote,
 EAknGlobalErrorNote,
 EAknGlobalChargingNote,
 EAknGlobalWaitNote,
 EAknGlobalPermanentNote,
 EAknGlobalNotChargingNote,
 EAknGlobalBatteryFullNote,
 EAknGlobalBatteryLowNote,
 EAknGlobalRechargeBatteryNote,
 EAknCancelGlobalNote,
 EAknGlobalTextNote
 };
        

5、詢問對話框

詢問對話框用到的類:

CAknQueryDialog

頭檔案:AknQueryDialog.h

lib:avkon.lib

使用方法:

Code:

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參數指定,可能的值如下:

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          
     };
        

通過定義不同的詢問對話框資源,可實作不同的詢問對話框,如讓使用者輸入文字的詢問對話框資源定義如下:

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詢問對話框,需要使用者輸入内容
               {
               layout = EDataLayout;
               label = "提示内容";
               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);
        

使用者輸入内容後按“确定”,内容就存儲到msg中,函數傳回非0;按“取消”,函數傳回0。

這裡用到的類是CAknQueryDialog的子類CAknTextQueryDialog。

CAknQueryDialog的子類有:

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
        

使用不同的類,資源檔案會有所不同。

另外,在資源中定義EDWIN時,可指定輸入發,如:

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;
   };
        

以上寫法表示預設輸入法為數字,并且屏蔽了輸入法切換鍵,即不能通過輸入法切換鍵來切換輸入法。

6、編輯框

編輯框使用的類:

CEikGlobalTextEditor

頭檔案:eikgted.h

使用方法:

Code:

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( &paraFormat, 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函數,如下:

Code:

TKeyResponse CMobileGuardSetKeyContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
 {
     return iGKeyEd->OfferKeyEventL( aKeyEvent, aType );
 }
        

繼續閱讀