Android实例-TTabControl的使用(XE8+小米2)

时间:2024-01-10 15:08:38

Android实例-TTabControl的使用(XE8+小米2)

结果: 

1.如果直接改变Tab的TabIndex,那样是没有动态效果的。如果想要动态效果需要用到ChangeTabAction1;

2.ChangeTabAction1可以直接为按钮指定Action这样不写代码,也可以支持动态效果。

3.ExecuteTarget是执行跳转功能,查看源代码,没有找到参数是做什么的。所以真心不明白里面的参数是什么意思。希望哪位前辈指点。

4.有人可能感觉出来了,为什么Android上的QQ、微信什么的手势都那么好用,而Delphi的有时不好使。其实不是这样的。那是因为Delphi中是直线向左、向右才识别。有关手势的我会重写文章说明,此文只是对Tab控件的功能介绍。

实例代码:

 unit Unit1;

 interface

 uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.ListBox, FMX.Layouts,
FMX.TabControl, System.Actions, FMX.ActnList, FMX.NumberBox, FMX.Edit,
FMX.EditBox, FMX.SpinBox, FMX.Gestures; type
TForm1 = class(TForm)
TabControl1: TTabControl;
Layout1: TLayout;
ComboBox1: TComboBox;
Label1: TLabel;
TabItem1: TTabItem;
TabItem2: TTabItem;
TabItem3: TTabItem;
TabItem4: TTabItem;
Button1: TButton;
Button2: TButton;
ActionList1: TActionList;
ChangeTabAction1: TChangeTabAction; //手动增加的Action
ChangeTabAction2: TChangeTabAction; //手动增加的Action
ChangeTabAction3: TChangeTabAction; //手动增加的Action
ChangeTabAction4: TChangeTabAction; //手动增加的Action
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button11: TButton;
Button12: TButton;
GestureManager1: TGestureManager; //手动增加的控件
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure TabControl1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} //为了实现手势必须做的设置
procedure TForm1.FormCreate(Sender: TObject);
begin
//为Tab指定一个手势控件
TabControl1.Touch.GestureManager := GestureManager1;
//设置Tab控件支持的手势有哪些(如果这一项不设置,手势是没有反映的)
TabControl1.Touch.StandardGestures := [TStandardGesture.sgLeft, TStandardGesture.sgRight];
end; //Action上一页
procedure TForm1.Button11Click(Sender: TObject);
begin
if TabControl1.TabIndex > then
begin
ChangeTabAction1.Tab := TTabItem(Form1.FindComponent('TabItem' + IntToStr(TabControl1.TabIndex)));
ChangeTabAction1.ExecuteTarget(Self);
end;
end; //Action下一页
procedure TForm1.Button12Click(Sender: TObject);
begin
if TabControl1.TabIndex < TabControl1.TabCount - then
begin
ChangeTabAction1.Tab := TTabItem(Form1.FindComponent('TabItem' + IntToStr(TabControl1.TabIndex + )));
ChangeTabAction1.ExecuteTarget(Self);
end;
end; //Tab上一页
procedure TForm1.Button1Click(Sender: TObject);
begin
if TabControl1.TabIndex > then
begin
TabControl1.TabIndex := TabControl1.TabIndex - ;
end;
end; //Tab下一页
procedure TForm1.Button2Click(Sender: TObject);
begin
if TabControl1.TabIndex < TabControl1.TabCount - then
begin
TabControl1.TabIndex := TabControl1.TabIndex + ;
end;
end; //ExecuteTarget方法1
procedure TForm1.Button7Click(Sender: TObject);
begin
ChangeTabAction1.ExecuteTarget(self);
end; //ExecuteTarget方法2
procedure TForm1.Button8Click(Sender: TObject);
begin
ChangeTabAction1.ExecuteTarget(TabControl1);
end; procedure TForm1.ComboBox1Change(Sender: TObject);
begin
case ComboBox1.ItemIndex of
: TabControl1.TabPosition := TTabPosition.PlatformDefault;
: TabControl1.TabPosition := TTabPosition.Top;
: TabControl1.TabPosition := TTabPosition.Bottom;
: TabControl1.TabPosition := TTabPosition.None;
: TabControl1.TabPosition := TTabPosition.Dots;
end;
end; //利用手机的返回键实现Tab跳转
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
Shift: TShiftState);
begin
if Key = vkHardwareBack then
begin
if TabControl1.TabIndex > then
begin
TabControl1.TabIndex := TabControl1.TabIndex - mod TabControl1.TabCount;
Key := ;
end;
end;
end; //利用手势实现Tab跳转
procedure TForm1.TabControl1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
case EventInfo.GestureID of
sgiLeft:
begin
case TabControl1.TabIndex of
: ChangeTabAction2.ExecuteTarget(self);
: ChangeTabAction3.ExecuteTarget(self);
: ChangeTabAction4.ExecuteTarget(self);
end;
end;
sgiRight:
begin
case TabControl1.TabIndex of
: ChangeTabAction3.ExecuteTarget(self);
: ChangeTabAction2.ExecuteTarget(self);
: ChangeTabAction1.ExecuteTarget(self);
end;
end;
end;
Handled := True;
end; end.