问题
I have a Firemonkey TListBox in an Android app and in OnPaint I place 3 buttons on the selected TListBoxItem. They are being displayed and all have a OnClick event assigned. But when clicking them this event usually does not fire. Only seldom I get the button clicked.
I guess the TListBox already received the click and declared it as handled. Is there any way otherwise? Can I register a tap in the form itsself and determine from the position of the tap which of the buttons must have been pressed? How would that work?
Here's the code of the current solution, the TListBox is client aligned to the form and the buttons in the TLayout are right aligned.
unit MainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
FMX.Dialogs, FMX.Layouts, FMX.ListBox, FMX.Controls.Presentation,
FMX.StdCtrls, FMX.Objects;
type
TForm1 = class(TForm)
lb_Files: TListBox;
Layout1: TLayout;
Image1: TImage;
Image2: TImage;
Button1: TButton;
Button2: TButton;
Rectangle1: TRectangle;
Rectangle2: TRectangle;
procedure FormCreate(Sender: TObject);
procedure lb_FilesPaint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
uses
System.UIConsts;
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
lb_Files.ListItems[lb_Files.ItemIndex].Text := 'Info';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
lb_Files.ListItems[lb_Files.ItemIndex].Text := 'Test';
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
begin
for i := 0 to 30 do
lb_Files.Items.Add('File' + i.ToString +'.dat');
end;
procedure TForm1.lb_FilesPaint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
if lb_Files.ItemIndex > -1 then
begin
Layout1.Parent := lb_Files.ListItems[lb_Files.ItemIndex];
Layout1.Align := TAlignLayout.Right;
Layout1.Visible := true;
end;
end;
end.
Here is the FMX content's now:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 480
ClientWidth = 640
StyleBook = StyleBook1
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
DesignerMasterStyle = 3
object lb_Files: TListBox
Align = Client
Enabled = False
Size.Width = 640.000000000000000000
Size.Height = 480.000000000000000000
Size.PlatformDefault = False
StyleLookup = 'listboxstyle'
TabOrder = 0
OnPaint = lb_FilesPaint
DisableFocusEffect = True
DefaultItemStyles.ItemStyle = 'listboxitemstyle'
DefaultItemStyles.GroupHeaderStyle = ''
DefaultItemStyles.GroupFooterStyle = ''
Viewport.Width = 640.000000000000000000
Viewport.Height = 480.000000000000000000
object Layout1: TLayout
Position.X = 384.000000000000000000
Position.Y = 192.000000000000000000
Size.Width = 153.000000000000000000
Size.Height = 50.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
object Rectangle1: TRectangle
Align = Right
Fill.Color = claBlue
HitTest = False
Position.X = 53.000000000000000000
Size.Width = 50.000000000000000000
Size.Height = 50.000000000000000000
Size.PlatformDefault = False
Stroke.Kind = None
OnClick = Button1Click
object Button1: TButton
Align = Client
Size.Width = 50.000000000000000000
Size.Height = 50.000000000000000000
Size.PlatformDefault = False
StyleLookup = 'buttonstyle'
TabOrder = 0
TintColor = claBlue
OnClick = Button1Click
end
end
object Rectangle2: TRectangle
Align = Right
Fill.Color = claGreen
HitTest = False
Position.X = 103.000000000000000000
Size.Width = 50.000000000000000000
Size.Height = 50.000000000000000000
Size.PlatformDefault = False
Stroke.Kind = None
OnClick = Button2Click
object Button2: TButton
Align = Client
Size.Width = 50.000000000000000000
Size.Height = 50.000000000000000000
Size.PlatformDefault = False
StyleLookup = 'buttonstyle'
TabOrder = 0
TintColor = claGreen
OnClick = Button2Click
end
end
end
end
end
来源:https://stackoverflow.com/questions/52328368/button-placed-on-selected-tlistitem-in-most-cases-doesnt-receive-click