Flex:在 TextArea 中查找字符串的下一个实例
我有一个 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);
}
I have a TextArea, a TextInput and a button. When text is entered into the TextInput the TextArea is searched for the matching string, however it only highlights the first found instance of the string. How can I do a 'find next' type of operation.
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);
}
EDIT
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当用户单击“查找下一个”按钮时,只需根据内存中的 search_str.length 和 search_result 截断文本,然后再次执行搜索。
有关子字符串的详细信息
编辑2011 年 12 月 7 日
以下是显示此方法工作原理的完整代码示例:
When the user clicks "find next" button, just truncate the text based on the search_str.length and search_result--in memory--and perform the search again.
More info on substring
Edit 12/7/2011
Here is a full code sample showing this approach working: