ボタンを押した後にボタンの色を変更したいだけです。
これを行うには「スタイル」を使用する必要がありますか....?
button.StyleLookupプロパティを変更して、スタイル(色)を変更できます。
スタイルブックに新しいスタイルを追加する必要があります。
スタイルの使用
別のスタイルを作成してその新しいスタイルに切り替える代わりに、ボタンのカスタム スタイルを作成し、実行時にそのスタイルの色を変更します。
ボタンのカスタム スタイルを作成しました。したがって、実行時に編集すると、そのボタンにのみ影響します。
ここで、OnClick イベントに次のように入力して、実行時に色を変更します。
var
r: TRectangle;
begin
// Find the background TRectangle style element for the button
r := (Button1.FindStyleResource('background') as TRectangle);
if Assigned(r) then
begin
r.Fill.Color := claBlue;
end;
end;
注: FMX.Objects を uses 句にまだ追加していない場合は追加します。それが TRectangle です。
ちょっと待って...
マウスがボタンから離れたりボタンに入ったりすると、ボタンの色がデフォルトに戻ることに気付くでしょう。それはアニメーションのせいです。カスタム スタイルのスタイル エディタで両方の TColorAnimation スタイル要素の stylename プロパティを設定すると、それらの色も設定できます。この例では、TColorAnimations に coloranimation1 と coloranimation2 という名前を付けました。
改訂されたコードは次のとおりです。
var
r: TRectangle;
ca: TColorAnimation;
begin
// Find the background TRectangle style element for the button
r := (Button1.FindStyleResource('background') as TRectangle);
if Assigned(r) then
begin
r.Fill.Color := claBlue;
end;
ca := (Button1.FindStyleResource('coloranimation1') as TColorAnimation);
if Assigned(ca) then
begin
ca.StartValue := claBlue;
end;
ca := (Button1.FindStyleResource('coloranimation2') as TColorAnimation);
if Assigned(ca) then
begin
ca.StopValue := claBlue;
end;
注: TColorAnimation の uses 句に FMX.Ani を追加します。