2016-03-06 31 views
9

Tôi muốn đặt một TEditButton động trong hộp tìm kiếm của ListView vì vậy tôi đã làm điều này:Làm cách nào để xóa nút rõ ràng trong hộp tìm kiếm của chế độ xem danh sách?

ListView       := TListView.Create(Self);  
ListView.Parent     := Self; 
ListView.Name     := 'hello'; 
ListView.Height     := 369; 
ListView.Width     := 369; 
ListView.Align     := TAlignLayout.Bottom; 
ListView.SearchVisible   := True; 
ListView.BeginUpdate;  

for i := 0 to ListView.controls.Count - 1 do 
begin 
    if ListView.controls[i].ClassType = TSearchBox then 
    begin  
    SearchBox := TSearchBox(ListView.controls[i]); 
    end; 
end; 

OKbtn := TEditButton.Create(SearchBox); 
OKbtn.Parent := SearchBox;  
OKbtn.Text := 'OK'; 
OKbtn.Width := 30; 

SearchBox.AddObject(OKbtn); 
ListView.EndUpdate;  

nhưng vấn đề là clear button đang đến quá khi chỉnh sửa hộp tìm kiếm. Làm cách nào để xóa nút rõ ràng (X) ở bên phải hộp tìm kiếm?

Trả lời

11

enter image description here

Các SearchBox là một hậu duệ của TEdit và sử dụng phong cách kiểm soát FireMonkey.

Đặt TEdit vào mẫu của bạn và mở StyleLookup tài sản của nó:

enter image description here

Bạn có thể thấy rằng có stlyes khác nhau đã có sẵn. Vì vậy, chúng tôi muốn thay đổi StyleLookup cho Hộp Tìm kiếm của chúng tôi.

Khi Hộp tìm kiếm là thành viên riêng (FSearchEdit) của điều khiển ListView, bạn không có cách nào truy cập trực tiếp vào nó.

Bạn có thể tạo điều khiển ListView của riêng mình là hậu duệ của TListView (TListViewBase) hoặc sử dụng trình trợ giúp lớp. Tôi lựa chọn thứ hai.

TListViewHelper = class helper for TListViewBase 
private 
    function GetClearButton: Boolean; 
    procedure SetClearButton(const Value: Boolean); 
public 
    property ShowSearchEditClearButton: Boolean read GetClearButton write SetClearButton; 
end; 

{ TListViewHelper } 

function TListViewHelper.GetClearButton: Boolean; 
begin 
    Result := Self.FSearchEdit.StyleLookup = ''; // default style 
end; 

procedure TListViewHelper.SetClearButton(const Value: Boolean); 
begin 
    if Value then 
    Self.FSearchEdit.StyleLookup := '' // default style 
    else 
    Self.FSearchEdit.StyleLookup := 'editstyle'; 
end; 

Trong FormCreate chúng ta có thể gọi ListView1.ShowSearchEditClearButton := False; và nút rõ ràng đã biến mất.

enter image description here

Tuy nhiên vào biểu tượng kính lúp cũng biến mất bởi vì nó không phải là một phần của Style editstyle mà chúng ta thiết lập như StyleLookup.

Để lấy lại biểu tượng, chúng tôi sẽ phải tạo Kiểu của riêng mình có biểu tượng kính lúp nhưng không có nút rõ ràng.

Thả một TEdit trên Form, nhấn chuột phải vào nó và sau đó chọn edit customized style:

enter image description here

Bây giờ chúng ta đang ở trong StyleBook Editor và được tiếp cận với cách bố trí kiểm soát.

Thêm một TActiveStyleObject đến structur, đổi tên nó để magnifierglass enter image description here

Thay đổi của TActiveStyleObject ActiveLink bitmap.

Trong BitmapLinks-Editor, hãy định vị biểu tượng kính lúp và chọn nó (cho ActiveLink và SourceLink).

enter image description here

Bây giờ văn bản của bạn sẽ chồng chéo với biểu tượng.

enter image description here

Để khắc phục nó, bạn phải thay đổi lề trái của nội dung (mà hiện nay được thiết lập để 2px) để một cái gì đó cao hơn như 20.

enter image description here

enter image description here

Bạn có thể bây giờ xóa bản chỉnh sửa trên biểu mẫu của bạn khi kiểu được tạo và trong StyleBook của biểu mẫu của bạn.

Mở StyleBook và đổi tên StyleName kiểu mới của bạn thành searcheditstylenoclearbtn.

enter image description here

Lưu nó và trong các chức năng classhelper của bạn thay đổi

Self.FSearchEdit.StyleLookup := 'editstyle'; 

để

Self.FSearchEdit.StyleLookup := 'searcheditstylenoclearbtn'; 

Bây giờ nút rõ ràng đã biến mất.

enter image description here

Nếu bạn không muốn phải trải qua những rắc rối của việc tạo ra searcheditstylenoclearbtn riêng của bạn, bạn có thể lưu các mã sau như searcheditstylenoclearbtn.style và tải nó trong trình soạn thảo StyleBook.

enter image description here

object TStyleContainer 
    object TLayout 
    StyleName = 'searcheditstylenoclearbtn' 
    Position.X = 530.000000000000000000 
    Position.Y = 399.000000000000000000 
    Size.Width = 100.000000000000000000 
    Size.Height = 22.000000000000000000 
    Size.PlatformDefault = False 
    Visible = False 
    TabOrder = 0 
    object TActiveStyleObject 
     StyleName = 'background' 
     Align = Contents 
     SourceLookup = 'Windows 10 Desktopstyle.png' 
     Size.Width = 100.000000000000000000 
     Size.Height = 22.000000000000000000 
     Size.PlatformDefault = False 
     ActiveTrigger = Focused 
     ActiveLink = < 
     item 
      CapInsets.Left = 7.000000000000000000 
      CapInsets.Top = 7.000000000000000000 
      CapInsets.Right = 7.000000000000000000 
      CapInsets.Bottom = 7.000000000000000000 
      SourceRect.Left = 266.000000000000000000 
      SourceRect.Top = 81.000000000000000000 
      SourceRect.Right = 305.000000000000000000 
      SourceRect.Bottom = 110.000000000000000000 
     end> 
     SourceLink = < 
     item 
      CapInsets.Left = 7.000000000000000000 
      CapInsets.Top = 7.000000000000000000 
      CapInsets.Right = 7.000000000000000000 
      CapInsets.Bottom = 7.000000000000000000 
      SourceRect.Left = 225.000000000000000000 
      SourceRect.Top = 81.000000000000000000 
      SourceRect.Right = 264.000000000000000000 
      SourceRect.Bottom = 110.000000000000000000 
     end> 
     TouchAnimation.Link = <> 
    end 
    object TLayout 
     StyleName = 'content' 
     Align = Client 
     Locked = True 
     Margins.Left = 20.000000000000000000 
     Margins.Top = 2.000000000000000000 
     Margins.Right = 2.000000000000000000 
     Margins.Bottom = 2.000000000000000000 
     Size.Width = 6.000000000000000000 
     Size.Height = 18.000000000000000000 
     Size.PlatformDefault = False 
    end 
    object TLayout 
     StyleName = 'buttons' 
     Align = Right 
     Locked = True 
     Margins.Top = 2.000000000000000000 
     Margins.Right = 2.000000000000000000 
     Margins.Bottom = 2.000000000000000000 
     Position.X = 48.000000000000000000 
     Position.Y = 2.000000000000000000 
     Size.Width = 50.000000000000000000 
     Size.Height = 18.000000000000000000 
     Size.PlatformDefault = False 
    end 
    object TBrushObject 
     StyleName = 'foreground' 
     Brush.Color = claBlack 
    end 
    object TBrushObject 
     StyleName = 'selection' 
     Brush.Color = x7F2A96FF 
    end 
    object TFontObject 
     StyleName = 'font' 
    end 
    object TLabel 
     StyleName = 'prompt' 
     Locked = True 
     Opacity = 0.500000000000000000 
     Visible = False 
    end 
    object TActiveStyleObject 
     StyleName = 'magnifierglass' 
     Align = Left 
     CapMode = Tile 
     Margins.Top = 1.000000000000000000 
     SourceLookup = 'Windows 10 Desktopstyle.png' 
     Position.Y = 1.000000000000000000 
     Size.Width = 20.000000000000000000 
     Size.Height = 21.000000000000000000 
     Size.PlatformDefault = False 
     WrapMode = Center 
     ActiveTrigger = Pressed 
     ActiveLink = < 
     item 
      SourceRect.Left = 4.000000000000000000 
      SourceRect.Top = 358.000000000000000000 
      SourceRect.Right = 20.000000000000000000 
      SourceRect.Bottom = 374.000000000000000000 
     end> 
     SourceLink = < 
     item 
      SourceRect.Left = 4.000000000000000000 
      SourceRect.Top = 358.000000000000000000 
      SourceRect.Right = 20.000000000000000000 
      SourceRect.Bottom = 374.000000000000000000 
     end> 
     TouchAnimation.Link = <> 
    end 
    end 
end 
5

Nếu bạn không muốn nhìn thấy Clearbutton trong tất cả tìm kiếm trong ứng dụng của bạn, bạn có thể sửa đổi FMX.Searchbox.Style.pas:

  1. Tìm FMX.Searchbox.Style.pas trong thư mục FMX (theo mặc định, C:\Program Files (x86)\Embarcadero\Studio\{your_version, e.g.17.0}\source\fmx\FMX.SearchBox.Style.pas và sao chép tệp vào thư mục dự án của bạn (gần your_application.dpr tệp)
  2. Trong tìm kiếm và nhận xét tệp mới tiếp theo dòng:

Đối với Delphi Seattle:

procedure TStyledSearchBox.RealignButtons; 
begin 
    if (LeftLayout <> nil) and (FMagGlass <> nil) then 
    LeftLayout.Width := FMagGlass.Width; 
    if (ButtonsLayout <> nil) and (FClearButton <> nil) then 
    //if Model.Text.IsEmpty then 
     ButtonsLayout.Width := 0 
    //else 
    // ButtonsLayout.Width := FClearButton.Width; 
end; 

Đối XE7:

procedure TStyledSearchBox.DoChangeTracking; 
begin 
    inherited; 
    if (ButtonsLayout <> nil) and (FClearButton <> nil) then 
    //if Model.Text.IsEmpty then 
     ButtonsLayout.Width := 0 
    //else 
    // ButtonsLayout.Width := FClearButton.Width; 
end; 

Như bạn có thể thấy, các mã không khác nhau nhiều tùy theo phiên bản Delphi, cho các phiên bản khác mà bạn có thể tự tìm thấy.

  1. Ứng dụng biên dịch và khởi chạy.

Những thay đổi mã này hợp lệ cho tất cả các nền tảng.

Các vấn đề liên quan