【问题标题】:How do I remove clear button in searchbox of listview?如何删除列表视图搜索框中的清除按钮?
【发布时间】:2016-06-19 19:27:47
【问题描述】:

我想在 ListViewsearchbox 中动态放置一个 TEditButton,所以我这样做了:

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;    

但问题是 clear button 在编辑 搜索框 时也会出现。如何删除搜索框右侧的清除按钮 (X)?

【问题讨论】:

    标签: android delphi listview firemonkey


    【解决方案1】:

    SearchBox 是 TEdit 的后代,使用 FireMonkey 控件样式。

    在表单上放置一个 TEdit 并打开其 StyleLookup 属性:

    您可以看到已经有不同的样式可用。 所以我们想改变 SearchBox 的 StyleLookup。

    由于 SearchBox 是 ListView 控件的私有成员 (FSearchEdit),因此您无法直接访问它。

    您可以创建自己的 ListView 控件,它是 TListView (TListViewBase) 的后代,也可以使用类助手。我选择后者。

    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;
    

    在 FormCreate 中我们可以调用ListView1.ShowSearchEditClearButton := False; 并且清除按钮消失了。

    但是放大镜图标也消失了,因为它不是我们设置为StyleLookupeditstyle 样式的一部分。

    要恢复图标,我们必须创建自己的样式,该样式具有放大镜图标但没有清除按钮。

    在窗体上拖放一个TEdit,右键单击它,然后选择edit customized style

    我们现在在样书编辑器中并且可以访问控件布局。

    在结构中添加一个 TActiveStyleObject,将其重命名为 magnifierglass

    更改 TActiveStyleObject 的 ActiveLink 位图。

    在 BitmapLinks-Editor 中找到放大镜图标并选择它(用于 ActiveLink 和 SourceLink)。

    您的文字现在将与图标重叠。

    要修复它,您必须将内容的左边距(当前设置为 2px)更改为更高的值,例如 20。

    现在,您可以在创建样式时删除表单上的编辑,也可以删除表单样本中的编辑。

    打开样书并将新样式的StyleName 重命名为searcheditstylenoclearbtn

    保存它并在你的 classhelper 函数中更改

    Self.FSearchEdit.StyleLookup := 'editstyle';
    

    Self.FSearchEdit.StyleLookup := 'searcheditstylenoclearbtn';
    

    现在清除按钮不见了。

    如果您不想经历创建自己的 searcheditstylenoclearbtn 的麻烦,您可以将以下代码保存为 searcheditstylenoclearbtn.style 并将其加载到样书编辑器中。

    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
    

    【讨论】:

      【解决方案2】:

      如果您不想在应用程序的all搜索框中看到Clearbutton,您可以修改FMX.Searchbox.Style.pas

      1. 在 fmx 文件夹中找到 FMX.Searchbox.Style.pas(默认为 C:\Program Files (x86)\Embarcadero\Studio\{your_version, e.g.17.0}\source\fmx\FMX.SearchBox.Style.pas 并将文件复制到您的项目文件夹(靠近 your_application.dpr 文件)
      2. 在新文件中查找并注释下一行:

      德尔福西雅图:

      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;
      

      对于 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;
      

      如您所见,代码根据Delphi版本差别不大,其他版本您可以自行查找。

      1. 编译并启动应用程序。

      这些代码更改对所有平台都有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-03
        • 1970-01-01
        • 1970-01-01
        • 2023-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多