微APP,再按一次退出

delphi10年前 (2015)发布 admin
463 0

//var //程序原有
// Form1: TForm1;//程序原有
IsFirstBack:Boolean;
tmrForQuit:Boolean;
//implementation//程序原有
uses
Android.JNI.Toast, Androidapi.JNI.Widget.Toast;

主要代码


procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  if (Key = vkHardwareBack) or (Key = vkEscape) then
  begin
    if IsFirstBack then
    begin
      IsFirstBack := False;
      Key := 0;
      tmrForQuit:= True;
      Toast('再按一次退出',ShortToast);
    end
    else
    begin
    end;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  tmrForQuit:= False;
  IsFirstBack := True;
end;

unit Android.JNI.Toast;

// Java bridge class imported by hand by Brian Long (http://blong.com)
interface

{$IFDEF ANDROID}

uses
 Androidapi.Helpers,
 Androidapi.JNIBridge,
 Androidapi.JNI.JavaTypes,
 Androidapi.JNI.GraphicsContentViewText;
{$ENDIF}
{$IFDEF ANDROID}

type
 TToastLength = (LongToast, ShortToast);

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)
 end;

procedure Toast(const Msg: string; duration: TToastLength = ShortToast);

{$ENDIF}

implementation

{$IFDEF ANDROID}

uses
 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}

end.
{***************************************************************************}
{                                                                           }
{       功能:Android Toast提示消息框                                         }
{       名称:Androidapi.JNI.Widget.Toast.pas                               }
{       版本:1.0                                                           }
{       环境:Win7 Sp1 64bit                                                }
{       工具:Delphi XE7                                                    }
{       日期:2014-11-23 19:45:35                                           }
{       用法:TToast.Show(MessageText, isLongHint)                          }
{--------------------------------------------------------------------------}
{       原版作者:ying32                                                     }
{       QQ:396506155                                                       }
{       MSN :ying_32@live.cn                                               }
{       E-mail:yuanfen3287@vip.qq.com                                      }
{       Website:http://blog.csdn.net/zyjying520/article/details/26158941   }
{       版权所有 (C) 2013-2013 ying32.tk All Rights Reserved                 }
{---------------------------------------------------------------------------}
{       修改者:tht2009                                                      }
{       改动:1、去掉注释掉的方法                                               }
{            2、移动FMX.Helpers.Android引用声明到实现区                         }
{            3、增加Androidapi.Helpers引用                                    }
{                                                                           }
{***************************************************************************}

unit Androidapi.JNI.Widget.Toast;

interface

uses
  Androidapi.JNIBridge,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.GraphicsContentViewText;

type
  JToast = interface;//android.widget.Toast

  JToastClass = interface(JObjectClass)
  ['{CF455B66-683D-41F9-B386-0C49E42D4F92}']
    {Property Methods}
    function _GetLENGTH_LONG: Integer;
    function _GetLENGTH_SHORT : Integer;
    {Methods}
    // public Toast (Context context)
    function init(context: JContext): JToast; cdecl;
    // public static Toast makeText (Context context, int resId, int duration)
    function makeText(context: JContext; resId: Integer; duration: Integer): JToast;cdecl;overload;
    // public static Toast makeText (Context context, CharSequence text, int duration)
    function makeText(context: JContext; text: JCharSequence; duration: Integer): JToast;cdecl;overload;
    {Properties}
    property LENGTH_LONG: Integer read _GetLENGTH_LONG;
    property LENGTH_SHORT: Integer read _GetLENGTH_SHORT;
  end;

  [JavaSignature('android/widget/Toast')]
  JToast = interface(JObject)
  ['{4A284317-000C-4048-8674-C475F505CFAF}']
    {Methods}
    // public void cancel ()
    procedure cancel;cdecl;
    // public int getDuration ()
    function getDuration: Integer; cdecl;
    // public int getGravity ()
    function getGravity: Integer; cdecl;
    // public float getHorizontalMargin ()
    function getHorizontalMargin: Single; cdecl;
    // public float getVerticalMargin ()
    function getVerticalMargin: Single; cdecl;
    // public View getView ()
    function getView: JView; cdecl;
    // public int getXOffset ()
    function getXOffset: Integer; cdecl;
    // public int getYOffset ()
    function getYOffset: Integer; cdecl;
    // public void setDuration (int duration)
    procedure setDuration(duration: Integer); cdecl;
    // public void setGravity (int gravity, int xOffset, int yOffset)
    procedure setGravity(gravity, xOffset, yOffset: Integer);cdecl;
    // public void setMargin (float horizontalMargin, float verticalMargin)
    procedure setMargin(horizontalMargin, verticalMargin: Single);cdecl;
    // public void setText (int resId)
    procedure setText(resId: Integer);cdecl;overload;
    // public void setText (CharSequence s)
    procedure setText(s: JCharSequence);cdecl;overload;
    // public void setView (View view)
    procedure setView(view: JView);cdecl;
    // public void show ()
    procedure show;cdecl;
  end;

  TJToast = class(TJavaGenericImport) end;

  { TToast }
  TToast = class
  public
    class procedure Show(const text: string; LongHint: Boolean = False);
  end;

implementation

uses FMX.Helpers.Android, Androidapi.Helpers;

{ TToast }

class procedure TToast.Show(const text: string; LongHint: Boolean);
var
  LDuration: Integer;
begin
   CallInUIThread
   (
     procedure
     begin
       case LongHint of
         True  : LDuration := TJToast.JavaClass.LENGTH_LONG;
         False : LDuration := TJToast.JavaClass.LENGTH_SHORT;
       end;
       TJToast.JavaClass.makeText(SharedActivityContext, StrToJCharSequence(text), LDuration).show;
     end
   );
end;

end.
© 版权声明

相关文章