我是使用Delphi / Firemonkey 10.3.1进行Android开发的初次经验,并且在运行时在TListbox组件和TMenu组件中填充和显示项目时遇到了以下问题。
我已经在面板上放置了TListbox和TMenu组件,以及一个按钮来触发随机数序列的生成并将它们显示在这些组件中。 TListbox组件包含类TListboxItemRandomNo的对象,这些对象从TListboxItem继承而来。此类的代码为:
TListboxItemRandomNo= class(TListboxItem)
private
FRandomNo: integer; {Number displayed by Listbox item}
protected
procedure HandlePaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
function GetItemText: string;
public
constructor Create(Owner: TComponent; RandomNoValue: integer);
property RandomNo: integer read FRandomNo;
end;
constructor TListboxItemRandomNo.Create(Owner: TComponent;
RandomNoValue: integer);
var
Listbox: TListbox;
begin
Listbox:= Owner as TListbox;
inherited Create(Owner);
Listbox.AddObject(Self);
{Save RandonNo:}
FRandomNo:= RandomNoValue;
{Assign OnPaint event handler:}
OnPaint:= HandlePaint;
end;
procedure TListboxItemRandomNo.HandlePaint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
var
Text: string;
TextSize: TSizeF;
TextPosn: TPointF;
TextRect: TRectF;
begin
{Calculate item text:}
Text:= GetItemText;
{Set colour for text:}
Canvas.Fill.Color:= TAlphaColorRec.Black;
{Determine text image size,position and bounding rectangle:}
TextSize.cx:= Canvas.TextWidth(Text);
TextSize.cy:= Canvas.TextHeight(Text);
TextPosn.x:= ARect.Left + 5;
TextPosn.y:= (ARect.Top + ARect.Bottom - TextSize.cy)/2;
TextRect.TopLeft:= TextPosn;
TextRect.Right:= ARect.Right;
TextRect.Bottom:= TextRect.Top + TextSize.cy;
{Draw item text:}
Canvas.FillText(TextRect,Text,False,1.0,[],TTextAlign.Leading,TTextAlign.Center);
end;
function TListboxItemRandomNo.GetItemText: string;
begin
Result:= IntToStr(Index) + ': ' + IntToStr(FRandomNo);
end;
最初,我将代码生成并显示在按钮的OnClick事件处理程序中并显示随机数。尽管此方法在Windows 32位平台上有效,但在TListbox组件和TMemo组件的Android平台上,这些项目均未显示。
然后,我创建了一个名为actionCalcRandomNos的动作,并将代码从按钮的OnClick事件处理程序中生成并显示了随机数,并将其显示到该动作的OnExecute事件处理程序中。我断开了按钮的OnClick事件处理程序的连接,并为其分配了actionCalcRandomNos。经过这些更改后,我发现TListbox和TMemo组件中的项目都显示在Windows-32和Android平台上。有问题的代码是:
procedure TForm1.actionCalcRandomNosExecute(Sender: TObject);
{Calculates a random sequence of numbers and displays them in TListbox
and TMemo components}
var
i: integer;
xi: integer;
ListboxItemRandomNo: TListboxItemRandomNo;
LineI: string;
begin
{Read number of random numbers required:}
FRandomNoCount:= StrToInt(EditItemCount.Text);
SetLength(FRandomNoSequence,FRandomNoCount);
{Generate random number sequence:}
Randomize;
for i := 0 to FRandomNoCount-1 do
begin
xi:= Random(FRandomNoCount);
FRandomNoSequence[i]:= xi;
end;
{Generate Listbox items dislaying random numbers:}
ListBoxRandomSequence.Clear;
ListBoxRandomSequence.BeginUpdate;
for i:= 0 to FRandomNoCount-1 do
begin
ListboxItemRandomNo:= TListboxItemRandomNo.Create(ListBoxRandomSequence,FRandomNoSequence[i]);
end;
ListBoxRandomSequence.EndUpdate;
{Write random numbers to TMemo:}
MemoRandomNos.Lines.Clear;
MemoRandomNos.BeginUpdate;
for i:= 0 to FRandomNoCount-1 do
begin
LineI:= (ListBoxRandomSequence.ListItems[i] as TListboxItemRandomNo).GetItemText;
MemoRandomNos.Lines.Add(LineI);
end;
MemoRandomNos.EndUpdate;
end;
为什么直接从按钮OnClick事件处理程序中调用此代码不起作用,但是通过连接到按钮的actionCalcRandomNos间接调用该代码却起作用?
TListbox唯一剩下的次要问题是这个。仅在Android平台版本上,在对象检查器中设置为0的项目高度显示得太大。在我将ItemHeight属性先前设置为20后将其重新设置为0之后,此操作就开始了。
阐明任何这些问题都是有帮助的。