如何在Delphi XE5中使用Android的toast
?
我尝试使用库FMX.AndroidLike.Toast
,但系统在Toast
显示之前执行期间关闭。
答案 0 :(得分:5)
我知道你已经知道......但是对于其他人......以防万一。
您需要安装该组件,将其添加到您要显示toastmessages的表单并致电...
componentname.now('Your toastmessage string');
如果您想使用基于组件的方法,请不要忘记添加单元FMX.Androidlike.Toast。 该组件可以在外观上进行相当多的配置(也就是显示消息的持续时间)。
如果你想使用Brians单元的JNI方法,请使用Android.JNI.Toast并调用程序
toast('Your toastmessage string', youroptionaltoastduration);
您可以找到该组件,现在也可以找到
上的截屏视频链接答案 1 :(得分:3)
实际上有一种更简单的方法:创建自己的吐司单位。
unit toast_unit;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms,
FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation;
type
TToastLength = (LongToast, ShortToast);
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$IFDEF ANDROID}
uses
Android.JNI.Toast,
Androidapi.Helpers,
FMX.Helpers.Android;
procedure Toast(const Msg: string; duration: TToastLength);
var
ToastLength: Integer;
begin
if duration = ShortToast then
ToastLength := TJToast.JavaClass.LENGTH_SHORT
else
ToastLength := TJToast.JavaClass.LENGTH_LONG;
CallInUiThread (
procedure
begin
TJToast.JavaClass.makeText (SharedActivityContext,
StrToJCharSequence(Msg), ToastLength).show
end
);
end;
{$ENDIF}
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
{$IFDEF ANDROID}
Toast ('aha', LongToast);
{$ENDIF}
end;
end.
答案 2 :(得分:1)
在Delphi 10.2.3 Tokyo中,我使用了以下方法:
uses Androidapi.JNI.Widget;
....
procedure TfrmBeaconClient.ShowMessageToast(const pMsg:String; pDuration :
Integer);
begin
TThread.Synchronize(nil, procedure begin
TJToast.JavaClass.makeText(TAndroidHelper.Context,
StrToJCharSequence(pMsg), pDuration).show
end);
end;
procedure ....
begin
...
ShowmessageToast('Logged in',TJToast.JavaClass.LENGTH_LONG);
....
end;
答案 3 :(得分:-1)
unit UI.Toast.Android;
// Java bridge class imported by hand by Brian Long (http://blong.com)
interface
{$IFDEF ANDROID}
uses
UI.Toast,
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText;
{$ENDIF}
{$IFDEF ANDROID}
type
JToast = interface;
JToastClass = interface(JObjectClass)
['{69E2D233-B9D3-4F3E-B882-474C8E1D50E9}']
{ Property methods }
function _GetLENGTH_LONG: Integer; cdecl;
function _GetLENGTH_SHORT: Integer; cdecl;
{ Methods }
function init(context: JContext): JToast; cdecl; overload;
function makeText(context: JContext; text: JCharSequence; duration: Integer)
: JToast; cdecl;
{ Properties }
property LENGTH_LONG: Integer read _GetLENGTH_LONG;
property LENGTH_SHORT: Integer read _GetLENGTH_SHORT;
end;
[JavaSignature('android/widget/Toast')]
JToast = interface(JObject)
['{FD81CC32-BFBC-4838-8893-9DD01DE47B00}']
{ Methods }
procedure cancel; cdecl;
function getDuration: Integer; cdecl;
function getGravity: Integer; cdecl;
function getHorizontalMargin: Single; cdecl;
function getVerticalMargin: Single; cdecl;
function getView: JView; cdecl;
function getXOffset: Integer; cdecl;
function getYOffset: Integer; cdecl;
procedure setDuration(value: Integer); cdecl;
procedure setGravity(gravity, xOffset, yOffset: Integer); cdecl;
procedure setMargin(horizontalMargin, verticalMargin: Single); cdecl;
procedure setText(s: JCharSequence); cdecl;
procedure setView(view: JView); cdecl;
procedure show; cdecl;
end;
TJToast = class(TJavaGenericImport<JToastClass, JToast>)
end;
procedure Toast(const Msg: string; duration: TToastLength = ShortToast);
{$ENDIF}
implementation
{$IFDEF ANDROID}
uses
{$IF CompilerVersion > 27}Androidapi.Helpers, {$ENDIF}
FMX.Helpers.Android;
procedure Toast(const Msg: string; duration: TToastLength);
var
ToastLength: Integer;
begin
if duration = ShortToast then
ToastLength := TJToast.JavaClass.LENGTH_SHORT
else
ToastLength := TJToast.JavaClass.LENGTH_LONG;
CallInUiThread(
procedure
begin
TJToast.JavaClass.makeText(
{$IF CompilerVersion > 27}
TAndroidHelper.Context,
{$ELSE}
SharedActivityContext,
{$ENDIF}
StrToJCharSequence(Msg),
ToastLength).show
end);
end;
{$ENDIF}
end.
https://github.com/yangyxd/FMXUI/blob/master/source/UI.Toast.pas https://github.com/yangyxd/FMXUI/blob/master/source/UI.Toast.Android.pas