android – 如何在listview的搜索框中删除清除按钮?

前端之家收集整理的这篇文章主要介绍了android – 如何在listview的搜索框中删除清除按钮?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在ListView的搜索框中动态地放置一个TEditButton,所以我这样做:
  1. ListView := TListView.Create(Self);
  2. ListView.Parent := Self;
  3. ListView.Name := 'hello';
  4. ListView.Height := 369;
  5. ListView.Width := 369;
  6. ListView.Align := TAlignLayout.Bottom;
  7. ListView.SearchVisible := True;
  8. ListView.BeginUpdate;
  9.  
  10. for i := 0 to ListView.controls.Count - 1 do
  11. begin
  12. if ListView.controls[i].ClassType = TSearchBox then
  13. begin
  14. SearchBox := TSearchBox(ListView.controls[i]);
  15. end;
  16. end;
  17.  
  18. OKbtn := TEditButton.Create(SearchBox);
  19. OKbtn.Parent := SearchBox;
  20. OKbtn.Text := 'OK';
  21. OKbtn.Width := 30;
  22.  
  23. SearchBox.AddObject(OKbtn);
  24. ListView.EndUpdate;

但问题是编辑搜索框时清除按钮也是来了.如何删除搜索框右侧的清除按钮(X)?

解决方法

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

在您的表单上放置TEdit并打开其StyleLookup属性

你可以看到有不同的stlyes已经可用.
所以我们要更改我们的SearchBox的StyleLookup.

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

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

  1. TListViewHelper = class helper for TListViewBase
  2. private
  3. function GetClearButton: Boolean;
  4. procedure SetClearButton(const Value: Boolean);
  5. public
  6. property ShowSearchEditClearButton: Boolean read GetClearButton write SetClearButton;
  7. end;
  1. { TListViewHelper }
  2.  
  3. function TListViewHelper.GetClearButton: Boolean;
  4. begin
  5. Result := Self.FSearchEdit.StyleLookup = ''; // default style
  6. end;
  7.  
  8. procedure TListViewHelper.SetClearButton(const Value: Boolean);
  9. begin
  10. if Value then
  11. Self.FSearchEdit.StyleLookup := '' // default style
  12. else
  13. Self.FSearchEdit.StyleLookup := 'editstyle';
  14. end;

在FormCreate中,我们可以调用ListView1.ShowSearchEditClearButton:= False;并且清除按钮不见了.

然而,放大镜玻璃图标也消失了,因为它不是我们设置为StyleLookup的编辑风格的一部分.

要获得图标,我们必须创建我们自己的风格,它有一个放大镜玻璃图标,但没有清除按钮.

在表单上放一个TEdit,右键单击它,然后选择编辑自定义样式:

我们现在在StyleBook Editor中,可以访问控件布局.

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

更改TActiveStyleObject的ActiveLink位图.

在BitmapLinks-Editor中找到放大镜玻璃图标并选择它(对于ActiveLink和SourceLink).

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

要修复它,您必须将内容的左边距(目前设置为2px)更改为20像素.

您现在可以在创建样式并在表单“StyleBook”中删除表单上的编辑.

打开StyleBook并将您的新样式的StyleName重命名为searcheditstylenoclearbtn.

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

  1. Self.FSearchEdit.StyleLookup := 'editstyle';

  1. Self.FSearchEdit.StyleLookup := 'searcheditstylenoclearbtn';

现在,清除按钮已经消失了.

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

  1. object TStyleContainer
  2. object TLayout
  3. StyleName = 'searcheditstylenoclearbtn'
  4. Position.X = 530.000000000000000000
  5. Position.Y = 399.000000000000000000
  6. Size.Width = 100.000000000000000000
  7. Size.Height = 22.000000000000000000
  8. Size.PlatformDefault = False
  9. Visible = False
  10. TabOrder = 0
  11. object TActiveStyleObject
  12. StyleName = 'background'
  13. Align = Contents
  14. SourceLookup = 'Windows 10 Desktopstyle.png'
  15. Size.Width = 100.000000000000000000
  16. Size.Height = 22.000000000000000000
  17. Size.PlatformDefault = False
  18. ActiveTrigger = Focused
  19. ActiveLink = <
  20. item
  21. CapInsets.Left = 7.000000000000000000
  22. CapInsets.Top = 7.000000000000000000
  23. CapInsets.Right = 7.000000000000000000
  24. CapInsets.Bottom = 7.000000000000000000
  25. SourceRect.Left = 266.000000000000000000
  26. SourceRect.Top = 81.000000000000000000
  27. SourceRect.Right = 305.000000000000000000
  28. SourceRect.Bottom = 110.000000000000000000
  29. end>
  30. SourceLink = <
  31. item
  32. CapInsets.Left = 7.000000000000000000
  33. CapInsets.Top = 7.000000000000000000
  34. CapInsets.Right = 7.000000000000000000
  35. CapInsets.Bottom = 7.000000000000000000
  36. SourceRect.Left = 225.000000000000000000
  37. SourceRect.Top = 81.000000000000000000
  38. SourceRect.Right = 264.000000000000000000
  39. SourceRect.Bottom = 110.000000000000000000
  40. end>
  41. TouchAnimation.Link = <>
  42. end
  43. object TLayout
  44. StyleName = 'content'
  45. Align = Client
  46. Locked = True
  47. Margins.Left = 20.000000000000000000
  48. Margins.Top = 2.000000000000000000
  49. Margins.Right = 2.000000000000000000
  50. Margins.Bottom = 2.000000000000000000
  51. Size.Width = 6.000000000000000000
  52. Size.Height = 18.000000000000000000
  53. Size.PlatformDefault = False
  54. end
  55. object TLayout
  56. StyleName = 'buttons'
  57. Align = Right
  58. Locked = True
  59. Margins.Top = 2.000000000000000000
  60. Margins.Right = 2.000000000000000000
  61. Margins.Bottom = 2.000000000000000000
  62. Position.X = 48.000000000000000000
  63. Position.Y = 2.000000000000000000
  64. Size.Width = 50.000000000000000000
  65. Size.Height = 18.000000000000000000
  66. Size.PlatformDefault = False
  67. end
  68. object TBrushObject
  69. StyleName = 'foreground'
  70. Brush.Color = claBlack
  71. end
  72. object TBrushObject
  73. StyleName = 'selection'
  74. Brush.Color = x7F2A96FF
  75. end
  76. object TFontObject
  77. StyleName = 'font'
  78. end
  79. object TLabel
  80. StyleName = 'prompt'
  81. Locked = True
  82. Opacity = 0.500000000000000000
  83. Visible = False
  84. end
  85. object TActiveStyleObject
  86. StyleName = 'magnifierglass'
  87. Align = Left
  88. CapMode = Tile
  89. Margins.Top = 1.000000000000000000
  90. SourceLookup = 'Windows 10 Desktopstyle.png'
  91. Position.Y = 1.000000000000000000
  92. Size.Width = 20.000000000000000000
  93. Size.Height = 21.000000000000000000
  94. Size.PlatformDefault = False
  95. WrapMode = Center
  96. ActiveTrigger = Pressed
  97. ActiveLink = <
  98. item
  99. SourceRect.Left = 4.000000000000000000
  100. SourceRect.Top = 358.000000000000000000
  101. SourceRect.Right = 20.000000000000000000
  102. SourceRect.Bottom = 374.000000000000000000
  103. end>
  104. SourceLink = <
  105. item
  106. SourceRect.Left = 4.000000000000000000
  107. SourceRect.Top = 358.000000000000000000
  108. SourceRect.Right = 20.000000000000000000
  109. SourceRect.Bottom = 374.000000000000000000
  110. end>
  111. TouchAnimation.Link = <>
  112. end
  113. end
  114. end

猜你在找的Android相关文章