标签:
方法一支持。
方法二与方法三都是三方单元,功能相同。
方法4与方法5报错,提示平台不支持。
第三方单元一:
1 unit Android.JNI.Toast; 2 3 // Java bridge class imported by hand by Brian Long (http://blong.com) 4 interface 5 6 {$IFDEF ANDROID} 7 8 uses 9 Androidapi.Helpers, 10 Androidapi.JNIBridge, 11 Androidapi.JNI.JavaTypes, 12 Androidapi.JNI.GraphicsContentViewText; 13 {$ENDIF} 14 {$IFDEF ANDROID} 15 16 type 17 TToastLength = (LongToast, ShortToast); 18 19 JToast = interface; 20 21 JToastClass = interface(JObjectClass) 22 [‘{69E2D233-B9D3-4F3E-B882-474C8E1D50E9}‘] 23 { Property methods } 24 function _GetLENGTH_LONG: Integer; cdecl; 25 function _GetLENGTH_SHORT: Integer; cdecl; 26 { Methods } 27 function init(context: JContext): JToast; cdecl; overload; 28 function makeText(context: JContext; text: JCharSequence; duration: Integer) 29 : JToast; cdecl; 30 { Properties } 31 property LENGTH_LONG: Integer read _GetLENGTH_LONG; 32 property LENGTH_SHORT: Integer read _GetLENGTH_SHORT; 33 end; 34 35 [JavaSignature(‘android/widget/Toast‘)] 36 JToast = interface(JObject) 37 [‘{FD81CC32-BFBC-4838-8893-9DD01DE47B00}‘] 38 { Methods } 39 procedure cancel; cdecl; 40 function getDuration: Integer; cdecl; 41 function getGravity: Integer; cdecl; 42 function getHorizontalMargin: Single; cdecl; 43 function getVerticalMargin: Single; cdecl; 44 function getView: JView; cdecl; 45 function getXOffset: Integer; cdecl; 46 function getYOffset: Integer; cdecl; 47 procedure setDuration(value: Integer); cdecl; 48 procedure setGravity(gravity, xOffset, yOffset: Integer); cdecl; 49 procedure setMargin(horizontalMargin, verticalMargin: Single); cdecl; 50 procedure setText(s: JCharSequence); cdecl; 51 procedure setView(view: JView); cdecl; 52 procedure show; cdecl; 53 end; 54 55 TJToast = class(TJavaGenericImport<JToastClass, JToast>) 56 end; 57 58 procedure Toast(const Msg: string; duration: TToastLength = ShortToast); 59 60 {$ENDIF} 61 62 implementation 63 64 {$IFDEF ANDROID} 65 66 uses 67 FMX.Helpers.Android; 68 69 procedure Toast(const Msg: string; duration: TToastLength); 70 var 71 ToastLength: Integer; 72 begin 73 if duration = ShortToast then 74 ToastLength := TJToast.JavaClass.LENGTH_SHORT 75 else 76 ToastLength := TJToast.JavaClass.LENGTH_LONG; 77 CallInUiThread( 78 procedure 79 begin 80 TJToast.JavaClass.makeText(SharedActivityContext, StrToJCharSequence(Msg), 81 ToastLength).show 82 end); 83 end; 84 {$ENDIF} 85 86 end.
第三方单元二:
1 {***************************************************************************} 2 { } 3 { 功能:Android Toast提示消息框 } 4 { 名称:Androidapi.JNI.Widget.Toast.pas } 5 { 版本:1.0 } 6 { 环境:Win7 Sp1 64bit } 7 { 工具:Delphi XE7 } 8 { 日期:2014-11-23 19:45:35 } 9 { 用法:TToast.Show(MessageText, isLongHint) } 10 {--------------------------------------------------------------------------} 11 { 原版作者:ying32 } 12 { QQ:396506155 } 13 { MSN :ying_32@live.cn } 14 { E-mail:yuanfen3287@vip.qq.com } 15 { Website:http://blog.csdn.net/zyjying520/article/details/26158941 } 16 { 版权所有 (C) 2013-2013 ying32.tk All Rights Reserved } 17 {---------------------------------------------------------------------------} 18 { 修改者:tht2009 } 19 { 改动:1、去掉注释掉的方法 } 20 { 2、移动FMX.Helpers.Android引用声明到实现区 } 21 { 3、增加Androidapi.Helpers引用 } 22 { } 23 {***************************************************************************} 24 25 unit Androidapi.JNI.Widget.Toast; 26 27 interface 28 29 uses 30 Androidapi.JNIBridge, 31 Androidapi.JNI.JavaTypes, 32 Androidapi.JNI.GraphicsContentViewText; 33 34 type 35 JToast = interface;//android.widget.Toast 36 37 JToastClass = interface(JObjectClass) 38 [‘{CF455B66-683D-41F9-B386-0C49E42D4F92}‘] 39 {Property Methods} 40 function _GetLENGTH_LONG: Integer; 41 function _GetLENGTH_SHORT : Integer; 42 {Methods} 43 // public Toast (Context context) 44 function init(context: JContext): JToast; cdecl; 45 // public static Toast makeText (Context context, int resId, int duration) 46 function makeText(context: JContext; resId: Integer; duration: Integer): JToast;cdecl;overload; 47 // public static Toast makeText (Context context, CharSequence text, int duration) 48 function makeText(context: JContext; text: JCharSequence; duration: Integer): JToast;cdecl;overload; 49 {Properties} 50 property LENGTH_LONG: Integer read _GetLENGTH_LONG; 51 property LENGTH_SHORT: Integer read _GetLENGTH_SHORT; 52 end; 53 54 [JavaSignature(‘android/widget/Toast‘)] 55 JToast = interface(JObject) 56 [‘{4A284317-000C-4048-8674-C475F505CFAF}‘] 57 {Methods} 58 // public void cancel () 59 procedure cancel;cdecl; 60 // public int getDuration () 61 function getDuration: Integer; cdecl; 62 // public int getGravity () 63 function getGravity: Integer; cdecl; 64 // public float getHorizontalMargin () 65 function getHorizontalMargin: Single; cdecl; 66 // public float getVerticalMargin () 67 function getVerticalMargin: Single; cdecl; 68 // public View getView () 69 function getView: JView; cdecl; 70 // public int getXOffset () 71 function getXOffset: Integer; cdecl; 72 // public int getYOffset () 73 function getYOffset: Integer; cdecl; 74 // public void setDuration (int duration) 75 procedure setDuration(duration: Integer); cdecl; 76 // public void setGravity (int gravity, int xOffset, int yOffset) 77 procedure setGravity(gravity, xOffset, yOffset: Integer);cdecl; 78 // public void setMargin (float horizontalMargin, float verticalMargin) 79 procedure setMargin(horizontalMargin, verticalMargin: Single);cdecl; 80 // public void setText (int resId) 81 procedure setText(resId: Integer);cdecl;overload; 82 // public void setText (CharSequence s) 83 procedure setText(s: JCharSequence);cdecl;overload; 84 // public void setView (View view) 85 procedure setView(view: JView);cdecl; 86 // public void show () 87 procedure show;cdecl; 88 end; 89 90 TJToast = class(TJavaGenericImport<JToastClass, JToast>) end; 91 92 { TToast } 93 TToast = class 94 public 95 class procedure Show(const text: string; LongHint: Boolean = False); 96 end; 97 98 implementation 99 100 uses FMX.Helpers.Android, Androidapi.Helpers; 101 102 { TToast } 103 104 class procedure TToast.Show(const text: string; LongHint: Boolean); 105 var 106 LDuration: Integer; 107 begin 108 CallInUIThread 109 ( 110 procedure 111 begin 112 case LongHint of 113 True : LDuration := TJToast.JavaClass.LENGTH_LONG; 114 False : LDuration := TJToast.JavaClass.LENGTH_SHORT; 115 end; 116 TJToast.JavaClass.makeText(SharedActivityContext, StrToJCharSequence(text), LDuration).show; 117 end 118 ); 119 end; 120 121 end.
应用单元:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, 8 FMX.Controls.Presentation, FMX.StdCtrls; 9 10 type 11 TForm1 = class(TForm) 12 Button1: TButton; 13 Button2: TButton; 14 Button3: TButton; 15 Button4: TButton; 16 Button5: TButton; 17 Button6: TButton; 18 Button7: TButton; 19 procedure Button1Click(Sender: TObject); 20 procedure Button2Click(Sender: TObject); 21 procedure Button3Click(Sender: TObject); 22 procedure Button4Click(Sender: TObject); 23 procedure Button5Click(Sender: TObject); 24 procedure Button6Click(Sender: TObject); 25 procedure Button7Click(Sender: TObject); 26 private 27 { Private declarations } 28 public 29 { Public declarations } 30 end; 31 32 var 33 Form1: TForm1; 34 35 implementation 36 37 uses 38 Android.JNI.Toast, Androidapi.JNI.Widget.Toast; 39 {$R *.fmx} 40 {$R *.NmXhdpiPh.fmx ANDROID} 41 42 procedure TForm1.Button1Click(Sender: TObject); 43 begin 44 ShowMessage(‘与原来的一样哦!‘); //消息提示方法一 45 end; 46 47 procedure TForm1.Button2Click(Sender: TObject); 48 begin 49 Toast(‘消息提示方法二‘, LongToast); 50 end; 51 52 procedure TForm1.Button3Click(Sender: TObject); 53 begin 54 Toast(‘消息提示方法二‘, ShortToast); 55 end; 56 57 procedure TForm1.Button4Click(Sender: TObject); 58 begin 59 TToast.Show(‘消息提示方法3。‘, False); 60 end; 61 62 procedure TForm1.Button5Click(Sender: TObject); 63 begin 64 TToast.Show(‘消息提示方法3。‘, True); 65 end; 66 67 procedure TForm1.Button6Click(Sender: TObject); 68 begin 69 if MessageDlg(‘你喜欢我吗?‘, TMsgDlgType.mtConfirmation, 70 [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], -1) = mrYes then 71 begin 72 73 end; 74 end; 75 76 procedure TForm1.Button7Click(Sender: TObject); 77 var 78 msresult: string; 79 begin 80 msresult := FMX.Dialogs.InputBox(‘询问‘, ‘你多大了?‘, ‘我 10 岁了。‘); 81 ShowMessage(‘你输入的是:‘ + msresult); 82 end; 83 84 end.
标签:
原文地址:http://www.cnblogs.com/FKdelphi/p/4690020.html