您的位置:首页 > 产品设计 > UI/UE

Symbian关于listBox的常用技巧小结

2010-02-04 15:42 615 查看
其实官方出了一个关于LISTBOX的例子,例子很全基本包含了所有关于LISTBOX的
应用.小弟根据自己的研究和自己开发中的经验在此总结一下.
其实LISTBOX有多种方格用法都非常接近,精通一个后基本都会了.

1.创建listbox

void CSingleListBoxContainer::CreateList()
{
iListBox = new (ELeave)CAknSingleStyleListBox;//创建listbox
iListBox->SetContainerWindowL(*this);//绑定窗口

// Second Phase Construction
TResourceReader reader;//加载资源
CEikonEnv::Static()->CreateResourceReaderLC

(reader,R_LISTBOX_SINGLE);//R_SIMPLELIST_LISTBOX);

iListBox->ConstructFromResourceL(reader);//用资源初始化
CleanupStack::PopAndDestroy(); // reader

///Add icon//加载图标这里的图标可以有很多,不同类型的图标代用户使用
CArrayPtr<CGulIcon>* icons = new (ELeave) CArrayPtrFlat<CGulIcon>(1);
CleanupStack::PushL(icons);
icons->AppendL(iEikonEnv->CreateIconL(KDoubleListBoxmbm,EMbm20028c74Marked,

EMbm20028c74Marked_mask));
CleanupStack::Pop();
iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);

****AppUi* pAppui = static_cast<****AppUi*>(iCoeEnv->AppUi());//通常用的技巧.用UI保存全

局变量数据
iOldIndex = pAppui->iSChoicendex;//得到上次选择的索引

//以下是创建动态的LISTBOX,根据需要创建listbox项
TInt len = pAppui->iNameArray.Count();

pAppui->iChoice = pAppui->iNameArray[iOldIndex]->GetName();//得到上次选择的文件名

if(len > 0)
{
//添加文本
CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(len);
CleanupStack::PushL(array);

for(TInt i=0;i<len;i++)
{
TBuf<50> buf;
buf.Append(_L("/t"));
buf.Append(pAppui->iNameArray[i]->GetName()->Des());
if(i == iOldIndex)
{
buf.Append(_L("/t0"));//在文件最后添加/t0的方式选择图标
}
array->AppendL(buf);
}
CleanupStack::Pop();
iListBox->Model()->SetItemTextArray(array);
}

if(pAppui->iPriviewIndex < 0)
{
iListBox->SetCurrentItemIndex(iOldIndex);//创建完成LISTBOX后,设置选中的项
}
else
{
iListBox->SetCurrentItemIndex(pAppui->iPriviewIndex);//创建完成LISTBOX后,设置选

中的项
}

}

//响应用户按OK键
void CSingleListBoxContainer::HandleListBoxEventL(CEikListBox* aListBox, TListBoxEvent

aListBoxEvent)
{
if (aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed)
{
//响应OK键
}
}

//根据需求,重新设置LISTBOX上的文本.或者调整图标
void CSingleListBoxContainer::ModifyItem()
{

CDesCArray* iListBoxArray= STATIC_CAST(CDesCArray*, iListBox->Model()->ItemTextArray());
//得到LISTBOX上的所有文本,并转换成数组
TInt len = iListBoxArray->Count();

for(TInt i=0;i<len;i++)
{
if(i == iOldIndex)
{
TBuf<120> buf;
buf.Append(_L("/t"));
buf.Append(pAppui->iNameArray[i]->GetName()->Des());
iListBoxArray->Delete(i);//先删掉原有文本
iListBoxArray->InsertL(i,buf);//再重新插入文本.
//用此方式才能修改内容
}

if(i == index)
{
TBuf<120> buf;
buf.Append(_L("/t"));
buf.Append(pAppui->iNameArray[i]->GetName()->Des());
buf.Append(_L("/t0"));
iListBoxArray->Delete(i);
iListBoxArray->InsertL(i,buf);
}
}
iListBox->DrawNow();//设置完成后,记得一定要重绘
}

TKeyResponse CSingleListBoxContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode

aType)
{
//响应上下左右键
if (iListBox)
return iListBox->OfferKeyEventL (aKeyEvent, aType);
else
return EKeyWasNotConsumed;
}

//得到当将用户选中的项
TInt CSingleListBoxContainer::GetListBoxIndex()
{
if(iListBox)
{
return iListBox->CurrentItemIndex();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  symbian icons ui c