学习SYMBIAN 对话框(二)

来源:互联网 发布:百科知识竞赛题库软件 编辑:程序博客网 时间:2024/05/21 09:31

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

其中相应的对应关系如下:

Type               

Layout

Control

Time editor

ETimeLayout

TIME_EDITOR

Date editor

EDateLayout

DATE_EDITOR

 

Type               

Layout

Control

Alphanumeric text

EDataLayout    

EDWIN

Secret editor

ECodeLayout

SECRETED

Phone number

EPhoneLayout  

EDWIN

PIN code

EPinLayout

SECRETED

 


除此之外还有数值查询框,浮点数询问框等。

使用方法:

 

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

以上写法表示默认输入法为数字,并且屏蔽了输入法切换键,即不能通过输入法切换键来切换输入法。

数值查询类的写法:

items =

        {

        DLG_LINE

            {

            type = EAknCtQuery;

            id = EGeneralQuery;

            control= AVKON_DATA_QUERY

                {

                layout = ENumberLayout;

                label = qtn_aknexquery_num_label_text;

                control = AVKON_INTEGER_EDWIN

                    {

                    min = AKNEXQUERY_NUMBER_EDITOR_MIN;

                    max = AKNEXQUERY_NUMBER_EDITOR_MAX;

                    };

                };

            }

        };

时间询问类的资源写法:

DLG_LINE

            {

            type = EAknCtQuery;

            id = EGeneralQuery;

            control = AVKON_DATA_QUERY

                {

                layout = ETimeLayout;

                label = qtn_aknexquery_time_label_text;

                control = TIME_EDITOR

                    {

                    minTime = TIME

                        {

                        second =

                           AKNEXQUERY_TIME_EDITOR_MIN_SECOND;

                        minute =

                           AKNEXQUERY_TIME_EDITOR_MIN_MINUTE;

                        hour =

                           AKNEXQUERY_TIME_EDITOR_MIN_HOUR;

                        };

                    maxTime = TIME

                        {

                        second =

                           AKNEXQUERY_TIME_EDITOR_MAX_SECOND;

                        minute =

                           AKNEXQUERY_TIME_EDITOR_MAX_MINUTE;

                        hour =

                           AKNEXQUERY_TIME_EDITOR_MAX_HOUR;

                        };

                    flags = EEikTimeWithoutSecondsField;

                                                };

                };

            }

        };

(播放总)时间询问类的资源文件写法:

items =

        {

        DLG_LINE

            {

            type = EAknCtQuery;

            id = EGeneralQuery;

            control = AVKON_DATA_QUERY

                {

                layout = EDurationLayout;

                label = qtn_aknexquery_dura_label_text;

                control = DURATION_EDITOR

                    {

                    minDuration = DURATION

                        {

                        };

                    maxDuration = DURATION

                        {

                        seconds =

                           AKNEXQUERY_DURATION_EDITOR_MAX_SECOND;

                        };

                    flags = AKNEXQUERY_DURATION_EDITOR_FLAGS;

                    };

                };

            }

        };

浮点询问框资源写法:

items =

        {

        DLG_LINE

            {

            type = EAknCtQuery;

            id = EGeneralQuery;

            control = AVKON_DATA_QUERY

                {

                layout = EFloatingPointLayout;

                label = "Enter value :";

                control = FLPTED

                    {

                        maxlength=10;

                        min=0;

                        max=100;

                        default=0;

                    };

                };

            }

        };

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

原创粉丝点击