FMX下Edit只能输入数字

时间:2023-12-22 10:24:38
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
var
i: Boolean;
begin
i := (keyChar <= #8) or (KeyChar<=#8) and (KeyChar<#48) or (KeyChar >#57) ;
if i then
keyChar := #0;
//   if not (keyChar in ['0'..'9',#13]) then KeyChar :=#0;
end;

在OnKeyDown中判断KeyChar的条件,以前delphi7里一般都在OnKeyPress事件下写

if not (key in ['0'..'9',#13]) then Key :=#0;

XE的FMX里没有OnKeyPress事件,一次在OnKeyDown下写,而Key参数是Word类型,所以要用到KeyChar