Android实例-TTabControl的使用(XE8+小米2)
结果:
1.如果直接改变Tab的TabIndex,那样是没有动态效果的。如果想要动态效果需要用到ChangeTabAction1;
2.ChangeTabAction1可以直接为按钮指定Action这样不写代码,也可以支持动态效果。
3.ExecuteTarget是执行跳转功能,查看源代码,没有找到参数是做什么的。所以真心不明白里面的参数是什么意思。希望哪位前辈指点。
4.有人可能感觉出来了,为什么Android上的QQ、微信什么的手势都那么好用,而Delphi的有时不好使。其实不是这样的。那是因为Delphi中是直线向左、向右才识别。有关手势的我会重写文章说明,此文只是对Tab控件的功能介绍。
实例代码:
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, FMX.ListBox, FMX.Layouts, 9 FMX.TabControl, System.Actions, FMX.ActnList, FMX.NumberBox, FMX.Edit, 10 FMX.EditBox, FMX.SpinBox, FMX.Gestures; 11 12 type 13 TForm1 = class(TForm) 14 TabControl1: TTabControl; 15 Layout1: TLayout; 16 ComboBox1: TComboBox; 17 Label1: TLabel; 18 TabItem1: TTabItem; 19 TabItem2: TTabItem; 20 TabItem3: TTabItem; 21 TabItem4: TTabItem; 22 Button1: TButton; 23 Button2: TButton; 24 ActionList1: TActionList; 25 ChangeTabAction1: TChangeTabAction; //手动增加的Action 26 ChangeTabAction2: TChangeTabAction; //手动增加的Action 27 ChangeTabAction3: TChangeTabAction; //手动增加的Action 28 ChangeTabAction4: TChangeTabAction; //手动增加的Action 29 Button3: TButton; 30 Button4: TButton; 31 Button5: TButton; 32 Button6: TButton; 33 Button7: TButton; 34 Button8: TButton; 35 Button11: TButton; 36 Button12: TButton; 37 GestureManager1: TGestureManager; //手动增加的控件 38 Label2: TLabel; 39 Label3: TLabel; 40 Label4: TLabel; 41 Label5: TLabel; 42 procedure Button1Click(Sender: TObject); 43 procedure Button2Click(Sender: TObject); 44 procedure Button7Click(Sender: TObject); 45 procedure Button8Click(Sender: TObject); 46 procedure Button11Click(Sender: TObject); 47 procedure Button12Click(Sender: TObject); 48 procedure ComboBox1Change(Sender: TObject); 49 procedure TabControl1Gesture(Sender: TObject; 50 const EventInfo: TGestureEventInfo; var Handled: Boolean); 51 procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; 52 Shift: TShiftState); 53 procedure FormCreate(Sender: TObject); 54 private 55 { Private declarations } 56 public 57 { Public declarations } 58 end; 59 60 var 61 Form1: TForm1; 62 63 implementation 64 65 {$R *.fmx} 66 {$R *.NmXhdpiPh.fmx ANDROID} 67 68 //为了实现手势必须做的设置 69 procedure TForm1.FormCreate(Sender: TObject); 70 begin 71 //为Tab指定一个手势控件 72 TabControl1.Touch.GestureManager := GestureManager1; 73 //设置Tab控件支持的手势有哪些(如果这一项不设置,手势是没有反映的) 74 TabControl1.Touch.StandardGestures := [TStandardGesture.sgLeft, TStandardGesture.sgRight]; 75 end; 76 77 //Action上一页 78 procedure TForm1.Button11Click(Sender: TObject); 79 begin 80 if TabControl1.TabIndex > 0 then 81 begin 82 ChangeTabAction1.Tab := TTabItem(Form1.FindComponent('TabItem' + IntToStr(TabControl1.TabIndex))); 83 ChangeTabAction1.ExecuteTarget(Self); 84 end; 85 end; 86 87 //Action下一页 88 procedure TForm1.Button12Click(Sender: TObject); 89 begin 90 if TabControl1.TabIndex < TabControl1.TabCount - 1 then 91 begin 92 ChangeTabAction1.Tab := TTabItem(Form1.FindComponent('TabItem' + IntToStr(TabControl1.TabIndex + 2))); 93 ChangeTabAction1.ExecuteTarget(Self); 94 end; 95 end; 96 97 //Tab上一页 98 procedure TForm1.Button1Click(Sender: TObject); 99 begin 100 if TabControl1.TabIndex > 0 then 101 begin 102 TabControl1.TabIndex := TabControl1.TabIndex - 1; 103 end; 104 end; 105 106 //Tab下一页 107 procedure TForm1.Button2Click(Sender: TObject); 108 begin 109 if TabControl1.TabIndex < TabControl1.TabCount - 1 then 110 begin 111 TabControl1.TabIndex := TabControl1.TabIndex + 1; 112 end; 113 end; 114 115 //ExecuteTarget方法1 116 procedure TForm1.Button7Click(Sender: TObject); 117 begin 118 ChangeTabAction1.ExecuteTarget(self); 119 end; 120 121 //ExecuteTarget方法2 122 procedure TForm1.Button8Click(Sender: TObject); 123 begin 124 ChangeTabAction1.ExecuteTarget(TabControl1); 125 end; 126 127 procedure TForm1.ComboBox1Change(Sender: TObject); 128 begin 129 case ComboBox1.ItemIndex of 130 0: TabControl1.TabPosition := TTabPosition.PlatformDefault; 131 1: TabControl1.TabPosition := TTabPosition.Top; 132 2: TabControl1.TabPosition := TTabPosition.Bottom; 133 3: TabControl1.TabPosition := TTabPosition.None; 134 4: TabControl1.TabPosition := TTabPosition.Dots; 135 end; 136 end; 137 138 //利用手机的返回键实现Tab跳转 139 procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; 140 Shift: TShiftState); 141 begin 142 if Key = vkHardwareBack then 143 begin 144 if TabControl1.TabIndex > 0 then 145 begin 146 TabControl1.TabIndex := TabControl1.TabIndex - 1 mod TabControl1.TabCount; 147 Key := 0; 148 end; 149 end; 150 end; 151 152 //利用手势实现Tab跳转 153 procedure TForm1.TabControl1Gesture(Sender: TObject; 154 const EventInfo: TGestureEventInfo; var Handled: Boolean); 155 begin 156 case EventInfo.GestureID of 157 sgiLeft: 158 begin 159 case TabControl1.TabIndex of 160 0: ChangeTabAction2.ExecuteTarget(self); 161 1: ChangeTabAction3.ExecuteTarget(self); 162 2: ChangeTabAction4.ExecuteTarget(self); 163 end; 164 end; 165 sgiRight: 166 begin 167 case TabControl1.TabIndex of 168 3: ChangeTabAction3.ExecuteTarget(self); 169 2: ChangeTabAction2.ExecuteTarget(self); 170 1: ChangeTabAction1.ExecuteTarget(self); 171 end; 172 end; 173 end; 174 Handled := True; 175 end; 176 177 end.