我有一个TextArea,一个TextInput和一个按钮。当文本输入到TextInput时,将在TextArea中搜索匹配的字符串,但它只突出显示第一个找到的字符串实例。如何进行“查找下一步”操作。
protected function searchBtn_clickHandler():void
{
text = mainTextField.text;
search_Str = searchTxt.text;
var search_result:int = text.search(search_Str);
trace(search_result);
mainTextField.setFocus();
mainTextField.selectRange(search_result,search_result+search_Str.length);
}
修改
protected function searchBtn_clickHandler():void
{
text = mainTextField.text;
search_Str = searchTxt.text;
search_result = text.search(search_Str);
trace(search_result);
mainTextField.setFocus();
mainTextField.selectRange(search_result,search_result+search_Str.length);
oldSearch_result = search_result;
}
protected function findNextBtn_clickHandler():void
{
search_Str = searchTxt.text;
// truncate the text using substring; this gives you everything in mainTextField after your previous search results
var truncatedText:String = mainTextField.text.substring(oldSearch_result+search_Str.length);
search_result = truncatedText.search(search_Str);
mainTextField.setFocus();
// when you select the range, you want to offset your result index w/ the characters your chopped off
mainTextField.selectRange(oldSearch_result+search_Str.length+search_result,search_result+search_Str.length);
}
答案 0 :(得分:2)
当用户点击“查找下一个”按钮时,只需根据search_str.length和search_result(在内存中)截断文本,然后再次执行搜索。
protected function searchAgain_ClickHandler(event:Event):void{
search_Str = searchTxt.text;
// truncate the text using substring; this gives you everything in mainTextField after your previous search results
var truncatedText = mainTextField.text.substring(oldSearchResult+search_Str.length);
var search_result:int = truncatedText.search(search_Str);
mainTextField.setFocus();
// when you select the range, you want to offset your result index w/ the characters your chopped off
mainTextField.selectRange(oldSearchResult+search_Str.length+search_result,search_result+search_Str.length);
}
的更多信息
编辑12/7/2011
以下是显示此方法的完整代码示例:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import spark.layouts.HorizontalLayout;
protected var oldSearchResult :int =0;
protected function button1_clickHandler(event:MouseEvent):void
{
var search_Str : String = searchTxt.text;
// truncate the text using substring; this gives you everything in mainTextField after your previous search results
var truncatedText : String;
truncatedText = mainTextField.text.substring(oldSearchResult);
var search_result:int = truncatedText.search(search_Str);
mainTextField.setFocus();
// when you select the range, you want to offset your result index w/ the characters your chopped off
if(search_result != -1){
mainTextField.selectRange(oldSearchResult+search_result,oldSearchResult+search_result+search_Str.length);
oldSearchResult = oldSearchResult+search_result+search_Str.length;
} else {
Alert.show('no more results');
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Form height="100%" width="100%">
<s:FormItem label="Search Text" layout="{new HorizontalLayout()}" width="100%" >
<s:TextInput id="searchTxt" text="the" />
<s:Button label="Search Next" click="button1_clickHandler(event)" />
</s:FormItem>
<s:FormItem height="100%" width="100%">
<s:TextArea id="mainTextField">
<s:text>
The Quick Brown Fox Jumped Over The Lazy Dogs and Landed on the Other Side of the River, bouncing back and forth between two turtles in an attempt to escape the flying vulture.
</s:text>
</s:TextArea>
</s:FormItem>
</s:Form>
</s:WindowedApplication>