『分享』各種提示、輸入對話框的使用

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 );
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章