×
Namespaces

Variants
Actions

Archived:如何实现单选列表

From Nokia Developer Wiki
Jump to: navigation, search

Archived.png存档: 这篇文章是存档,因为考虑到今天它与第三方开发者建立商业解决方案不相关。

Article Metadata

代码示例
兼容于
文章
翻译:
chenziteng
最后由 hamishwillee 在 16 Aug 2012 编辑

Contents

概述

单选列表是一组纵身排列的单选按钮,该列表中只允许一个选项被选中。本文描述了如何使用S60标准列表框创建单选列表。

方案

首先用CAknSingleGraphicStyleListBox创建一个普通的选择列表框,

void CZoyvlyooListBox::InitializeControlsL()
{
iListBox = new ( ELeave ) CAknSingleGraphicStyleListBox;
iListBox->SetContainerWindowL( *this );
{
TResourceReader reader;
iEikonEnv->CreateResourceReaderLC( reader, R_ZOYVLYOO_LIST_BOX_LIST_BOX );
iListBox->ConstructFromResourceL( reader );
CleanupStack::PopAndDestroy(); // reader internal state
}
...

然后为列表框的图标数组添加两个图标(RadioOn.PNG表示未选中,RadioOff.PNG表示选中)。

void CZoyvlyooListBox::SetupListBoxIconsL()
{
CArrayPtr< CGulIcon >* icons = NULL;
icons = new (ELeave) CAknIconArray( 2 );
CleanupStack::PushL( icons );
// for EListBoxZoyvlyooRationonIndex
icons->AppendL( CEikonEnv::Static()->CreateIconL(
KZoyvlyooFile, EMbmZoyvlyooRationon ) );
// for EListBoxZoyvlyooRationoffIndex
icons->AppendL( CEikonEnv::Static()->CreateIconL(
KZoyvlyooFile, EMbmZoyvlyooRationoff ) );
CleanupStack::Pop( icons );
 
if ( icons != NULL )
{
iListBox->ItemDrawer()->ColumnData()->SetIconArray( icons );
}
}

CAknSingleGraphicStyleListBox列表项的格式串是"Icon\tText", 假定默认选中列表的第一项,我们可以添加若干项作为初始值,并使用iSelectedOption记录当前选中的项。

...
AddListBoxItemL(iListBox, _L("0\tItem1")); // zero is the index of the "selected" icon
AddListBoxItemL(iListBox, _L("1\tItem2")); // one is the index of the "unselected" icon
AddListBoxItemL(iListBox, _L("1\tItem3"));
...
iSelectedOption = 0;
...
void CZoyvlyooListBox::AddListBoxItemL(
CEikTextListBox* aListBox,
const TDesC& aString)
{
CTextListBoxModel* model = aListBox->Model();
CDesCArray* itemArray = static_cast< CDesCArray* > ( model->ItemTextArray() );
itemArray->AppendL( aString );
aListBox->HandleItemAdditionL();
}

当用户试图选中新的选项时,我们先把前一个选中项的图标置成“未选中”,然后再将新选中的项的图标置成“选中”,最后更新iSelectedOption的值。

void CZoyvlyooListBox::HandleListBoxEnterKeyPressedL( 
CEikListBox* aListBox,
TListBoxEvent anEventType)
{
// TODO: implement enterKeyPressed event handler
if((anEventType==EEventEnterKeyPressed)
||(anEventType==EEventItemClicked))
{
TInt index = aListBox->CurrentItemIndex();
if(index!=iSelectedOption)
{
CTextListBoxModel* model = static_cast<CTextListBoxModel*>(aListBox->Model());
CDesCArray* itemArray = static_cast< CDesCArray* > ( model->ItemTextArray() );
{
TPtrC ptr = (*itemArray)[iSelectedOption];
_LIT(KSeparater, "\t");
TInt i = ptr.Find(KSeparater);
ptr.Set(ptr.Right(ptr.Length()-i-1));
TBuf<512> listString;
CreateListBoxItemL( listString, EListBoxZoyvlyooRationoffIndex, ptr);
itemArray->Delete(iSelectedOption);
itemArray->InsertL(iSelectedOption, listString);
aListBox->HandleItemAdditionL();
}
{
TPtrC ptr = (*itemArray)[index];
_LIT(KSeparater, "\t");
TInt i = ptr.Find(KSeparater);
ptr.Set(ptr.Right(ptr.Length()-i-1));
TBuf<512> listString;
CreateListBoxItemL( listString, EListBoxZoyvlyooRationonIndex, ptr);
itemArray->Delete(index);
itemArray->InsertL(index, listString);
aListBox->HandleItemAdditionL();
}
iSelectedOption = index;
}
}
}

源代码

完整的示例程序: Zoyvlyoo(VertOptionList).zip

Zoyvlyoo(VertOptionList).PNG

相关文档

Creating a radio button settings page using Symbian C++

Mobile Design Pattern: Radio Button

This page was last modified on 16 August 2012, at 06:51.
182 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×