问题:如果 SearchEdit 里已输入过搜寻字,再新建 ListView 项目后,会无法显示。
适用:Delphi XE5
</span><span style="color: #0000ff;">for</span> i:=<span style="color: #800080;">0</span> <span style="color: #0000ff;">to</span> <span style="color: #800080;">30</span> <span style="color: #0000ff;">do</span>
<span style="color: #0000ff;">begin</span><span style="color: #000000;">
Item1 :</span>=<span style="color: #000000;"> ListView1.Items.Add;
Item1.Text :</span>=<span style="color: #000000;"> i.ToString;
</span><span style="color: #0000ff;">end</span><span style="color: #000000;">;
ListView1.EndUpdate;
</span>//<span style="color: #000000;"> 如果 SearchEdit 里已输入过搜寻字,再新建项目后,会无法<a href="/tag/xianshi/" target="_blank" class="keywords">显示</a>,需要加入下行才能修正.
ListView1.ClearSearchEdit;
<span style="color: #0000ff;">end<span style="color: #000000;">;
<span style="color: #0000ff;">procedure<span style="color: #000000;"> TForm1.Button2Click(Sender: TObject);<span style="color: #0000ff;">begin<span style="color: #000000;">
ListView1.SearchEdit.Text := <span style="color: #800000;">'<span style="color: #800000;">3<span style="color: #800000;">'<span style="color: #000000;">;
<span style="color: #0000ff;">end;
FMX.ListViewHelper.pas
<span style="color: #0000ff;">uses<span style="color: #000000;">
FMX.ListView,FMX.Edit;
<span style="color: #0000ff;">type<span style="color: #000000;">
TListViewHelper = <span style="color: #0000ff;">class helper <span style="color: #0000ff;">for<span style="color: #000000;"> TListView
<span style="color: #0000ff;">function<span style="color: #000000;"> SearchEdit: TSearchBox;
<span style="color: #0000ff;">procedure<span style="color: #000000;"> ClearSearchEdit;
<span style="color: #0000ff;">end<span style="color: #000000;">;
<span style="color: #0000ff;">implementation
<span style="color: #008000;">{<span style="color: #008000;"> TListViewHelper <span style="color: #008000;">}
<span style="color: #0000ff;">function<span style="color: #000000;"> TListViewHelper.SearchEdit: TSearchBox;
<span style="color: #0000ff;">var<span style="color: #000000;"> i: Integer;
<span style="color: #0000ff;">begin<span style="color: #000000;">
Result := <span style="color: #0000ff;">nil<span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span> i:=<span style="color: #800080;">0</span> <span style="color: #0000ff;">to</span> Self.ComponentCount - <span style="color: #800080;">1</span> <span style="color: #0000ff;">do</span>
<span style="color: #0000ff;">if</span> (Self.Components[i] <span style="color: #0000ff;">is</span> TSearch<a href="/tag/Box/" target="_blank" class="keywords">Box</a>) <span style="color: #0000ff;">then</span>
<span style="color: #0000ff;">begin</span><span style="color: #000000;">
Result :</span>= Self.Components[i] <span style="color: #0000ff;">as</span><span style="color: #000000;"> TSearch<a href="/tag/Box/" target="_blank" class="keywords">Box</a>;
Break;
</span><span style="color: #0000ff;">end</span><span style="color: #000000;">;
<span style="color: #0000ff;">end<span style="color: #000000;">;
<span style="color: #0000ff;">procedure<span style="color: #000000;"> TListViewHelper.ClearSearchEdit;
<span style="color: #0000ff;">begin
<span style="color: #0000ff;">if SearchEdit <> <span style="color: #0000ff;">nil <span style="color: #0000ff;">then
<span style="color: #0000ff;">begin<span style="color: #000000;">
SearchEdit.Text := <span style="color: #800000;">' <span style="color: #800000;">'<span style="color: #000000;">;
SearchEdit.Text := <span style="color: #800000;">''<span style="color: #000000;">;
<span style="color: #0000ff;">end<span style="color: #000000;">;
<span style="color: #0000ff;">end<span style="color: #000000;">;
<span style="color: #0000ff;">end.