toast提示是Android系统中一种消息框类型,一种简易的消息提示框;是Android中用来显示提示信息的一种机制。
实际效果如下图红色框标示:
本实例测试环境:DELPHI11 + Windwos11测试成功
第一步:创建一个独立的单元
unit u_Toast;
interface
uses
System.SysUtils, System.Types, System.Classes
{$IFDEF ANDROID}
, FMX.Helpers.Android, Androidapi.JNI.app, Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes, Androidapi.Helpers, Androidapi.JNI.Widget
{$ENDIF}
;
type
TToastLength = record
const
ShortToast = 0;
LongToast = 1;
end;
TGravity = record
const
AXIS_CLIP = 8;
AXIS_PULL_AFTER = 4;
AXIS_PULL_BEFORE = 2;
AXIS_SPECIFIED = 1;
AXIS_X_SHIFT = 0;
AXIS_Y_SHIFT = 4;
BOTTOM = 80;
CENTER = 17;
CENTER_HORIZONTAL = 1;
CENTER_VERTICAL = 16;
CLIP_HORIZONTAL = 8;
CLIP_VERTICAL = 128;
DISPLAY_CLIP_HORIZONTAL = 16777216;
DISPLAY_CLIP_VERTICAL = 268435456;
&END = 8388613;
FILL = 119;
FILL_HORIZONTAL = 7;
FILL_VERTICAL = 112;
HORIZONTAL_GRAVITY_MASK = 7;
LEFT = 3;
NO_GRAVITY = 0;
RELATIVE_HORIZONTAL_GRAVITY_MASK = 8388615;
RELATIVE_LAYOUT_DIRECTION = 8388608;
RIGHT = 5;
START = 8388611;
TOP = 48;
VERTICAL_GRAVITY_MASK = 112;
end;
procedure Toast(const msg: string;
duration: Integer = TToastLength.ShortToast); overload;
procedure Toast(const msg: string; duration: Integer; gravity: Integer;
xOffset: Integer; yOffset: Integer); overload;
implementation
procedure Toast(const msg: string; duration: Integer = TToastLength.ShortToast);
begin
{$IFDEF ANDROID}
CallInUiThread(
procedure
begin
TJToast.JavaClass.makeText(TAndroidHelper.Activity,
StrToJCharSequence(msg), duration).show;
end);
{$ENDIF}
end;
procedure Toast(const msg: string; duration: Integer; gravity: Integer;
xOffset: Integer; yOffset: Integer);
{$IFDEF ANDROID}
var
vToast: JToast;
{$ENDIF}
begin
{$IFDEF ANDROID}
CallInUiThread(
procedure
begin
vToast := TJToast.JavaClass.makeText(TAndroidHelper.Activity,
StrToJCharSequence(msg), duration);
vToast.setGravity(gravity, xOffset, yOffset);
vToast.show;
end);
{$ENDIF}
end;
end.
第二步:调用方式
Toast('您输入的登录信息错误');