Android实例-退出程序(XE8+小米2)
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 Label1: TLabel; 16 Button4: TButton; 17 Button5: TButton; 18 Button6: TButton; 19 procedure Button1Click(Sender: TObject); 20 procedure Button2Click(Sender: TObject); 21 procedure Button3Click(Sender: TObject); 22 procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; 23 Shift: TShiftState); 24 procedure Button4Click(Sender: TObject); 25 procedure Button6Click(Sender: TObject); 26 procedure Button5Click(Sender: TObject); 27 private 28 { Private declarations } 29 public 30 { Public declarations } 31 end; 32 33 var 34 Form1: TForm1; 35 36 implementation 37 uses 38 FMX.Platform.Android, 39 FMX.Helpers.Android, 40 Androidapi.JNI.Net, 41 Androidapi.JNI.GraphicsContentViewText, 42 Androidapi.JNI.JavaTypes, 43 Androidapi.Helpers, 44 system.IOUtils; 45 46 {$R *.fmx} 47 {$R *.NmXhdpiPh.fmx ANDROID} 48 {$R *.LgXhdpiPh.fmx ANDROID} 49 {$R *.Macintosh.fmx MACOS} 50 {$R *.XLgXhdpiTb.fmx ANDROID} 51 {$R *.LgXhdpiTb.fmx ANDROID} 52 {$R *.GGlass.fmx ANDROID} 53 54 procedure TForm1.Button1Click(Sender: TObject); 55 begin 56 Label1.Text := '按返回键将会出现一个对话框!'; 57 end; 58 59 procedure TForm1.Button2Click(Sender: TObject); 60 begin 61 MainActivity.finish; { 退出程序 } 62 end; 63 64 procedure TForm1.Button3Click(Sender: TObject); 65 begin 66 {$IFDEF ANDROID} 67 Application.MainForm.DisposeOf; { 退出程序 } 68 halt(0); 69 {$ENDIF} 70 end; 71 72 procedure TForm1.Button4Click(Sender: TObject); 73 var 74 Intent: JIntent; 75 begin 76 Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_MAIN); 77 Intent.addCategory(TJIntent.JavaClass.CATEGORY_HOME); 78 Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK); 79 SharedActivityContext.startActivity(Intent); 80 end; 81 82 procedure TForm1.Button5Click(Sender: TObject); 83 begin 84 Application.Terminate; 85 end; 86 87 procedure TForm1.Button6Click(Sender: TObject); 88 begin 89 Close; 90 end; 91 //此代码报错,不可用,原因不明。 92 procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; 93 Shift: TShiftState); 94 begin 95 if Key = vkHardwareBack then//如果按下物理返回键 96 begin 97 Label1.Text := '1'; 98 if MessageDlg('确认退出吗?', TMsgDlgType.mtConfirmation, 99 [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1) = mrOK then 100 MainActivity.finish; { 退出程序 } 101 Key := 0;//必须的,不然按否也会退出 102 Exit; 103 end; 104 end; 105 106 end.
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我