2

如何toast在 Delphi XE5 中使用 Android?

我尝试使用该库FMX.AndroidLike.Toast,但系统在显示之前在执行期间关闭Toast

4

4 回答 4

5

我知道你已经知道了……但对其他人而言……以防万一。

您需要安装组件,将其添加到要显示 toastmessages 的表单中并调用...

componentname.now('Your toastmessage string');

如果您想使用基于组件的方法,请不要忘记添加 Unit FMX.Androidlike.Toast。该组件的外观几乎可以配置(还有显示消息的持续时间)。

如果您想使用 Brians 单元的 JNI 方法,请使用 Android.JNI.Toast 并调用该过程

toast('Your toastmessage string', youroptionaltoastduration);

您可以找到该组件,现在还可以找到指向截屏视频的链接

https://www.cybertribe.de/info/components/fmx/toast/

于 2013-09-24T22:34:36.917 回答
3

在 Delphi 10.2.3 Tokyo 我用这个:

uses
  ...
  Androidapi.JNI.Widget,
  Androidapi.Helpers;  

  ...

  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;
于 2018-10-24T13:21:57.837 回答
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.
于 2016-02-18T16:49:40.417 回答
0
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

于 2019-05-04T14:49:17.807 回答