原文:http://hi.baidu.com/richiechyi/blog/item/780acf032c85fd8cd53f7cd8.html
Series60提供了两种类型的列表对话框: 选择列表对话框和可标记列表对话框。这些列表行为和标准列表一致,但包含在对话框内。
下面以选择列表对话框示例
1. 在资源中定义选择列表对话框
RESOURCE DIALOG r_listdlg_dialog
{
flags = EAknDialogSelectionList;
buttons = R_AVKON_SOFTKEYS_OK_CANCEL;
items =
{
DLG_LINE
{
type = EAknCtSingleGraphicListBox;
id = ESelectionListControl;
control = LISTBOX
{
flags = EAknListBoxSelectionList;
array_id = r_listdlg_list_array;
};
},
DLG_LINE
{
itemflags = EEikDlgItemNonFocusing;
id = EFindControl;
type = EAknCtSelectionListFixedFind;
}
};
}
RESOURCE ARRAY r_listdlg_list_array
{
items =
{
LBUF
{
txt = SAVED_GAME1_TEXT;
},
LBUF
{
txt = SAVED_GAME2_TEXT;
},
LBUF
{
txt = SAVED_GAME3_TEXT;
}
};
}
2. 构建选择列表对话框
使用选择列表对话框的NewL()方法执行构建,该函数需要三个参数,第一个TInt&, 用于返回列表中选择的项。第二个参数是由列表项组成的数组,示例中在资源中静态地给出了定义,因此被设置为NULL,如果需要使用动态项数组,则可以创建 并传入一个描述符数组。第三个参数是所需菜单栏使用的资源。该例中将对话框定义为“确定”和“取消”软键,所以不需要指定菜单栏,因此传入0.如果需要使 用“选项”菜单,则应该在此处传入适当的MENU_BAR资源。
TInt openedItem(0);
// Get the name of the file containing the icons
HBufC* iconFileName;
iconFileName = StringLoader::LoadLC(R_ICON_FILE_NAME); // Pushes iconFileName onto the Cleanup Stack.
// Construct and prepare the dialog
CAknSelectionListDialog* dialog = CAknSelectionListDialog::NewL(openedItem, NULL, 0);
dialog->PrepareLC (R_LISTDLG_DIALOG);
3. 为选择列表对话框添加图标
对于包含图形项的选择列表,必须把这些图形添加到对话框的数组中。
// Create an array of icons, reading them from the file and set them in the dialog
CArrayPtr<CGulIcon>* icons = new(ELeave) CAknIconArray(KNumberOfIcons);
CleanupStack::PushL(icons);
icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmListdlg1player, EMbmListdlg1player_mask));
icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmListdlg2player, EMbmListdlg2player_mask));
dialog->SetIconArrayL(icons); // transferring ownership of icons
CleanupStack::Pop(icons);
CleanupStack::PopAndDestroy(iconFileName);
4. 执行选择列表对话框
// Execute the dialog
if (dialog->RunLD ())
{
PlaySelectedGame(openedItem);
}
可标记列表对话框
和选择列表一样应定义DIALOG资源,但需要把标志设置为EAknDialogMarkableList,把LISTBOX的标志设置为 EAknListBoxMarkableList。由CAknMarkableListDialog类定义一个可标记的对话框。通过按引用传入一个数组 CArrayFix<TInt>实例它,该数组包含选择项的索引。此外,还可以传入对一个上下文敏感菜单的引用。
对于不带图标的可标记列表,将会自动提供默认的标记图标。对于带有图形的可标记列表,可以使用和在选项列表中类似的方式提供它们。
使用常规ExecuteLD()或PrepareLD()和RunLD()来执行可标记列表对话框。