User Tools

Site Tools


sample_code:textedit_search

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:textedit_search [2015/04/01 12:26]
stevegarman
sample_code:textedit_search [2015/08/05 14:40]
Line 1: Line 1:
-====== TextEdit Search ====== 
-This function is designed to search for a string in a textEdit. 
  
-It starts searching from the current position of the cursor and, if it finds an occurrence of the string, positions the cursor immediately before the occurrence.  
- 
-An area immediately following the cursor is highlighted as the selected area. 
- 
-If it does not find an occurrence, the cursor is positioned at the beginning of the text, allowing the user to search again from there if required. 
-<code JavaScript findtext.js> 
-function findtext(thisedt,sought) 
-//thisedt = textEdit to be searched. 
-//sought = string to be searched for. 
-{ 
-   //both toLowerCase for case-insensitive search 
-   var lc = thisedt.GetText().toLowerCase(); 
-   sought = sought.toLowerCase(); 
-   //current cursor position is start for search 
-   var curs = thisedt.GetCursorPos(); 
-   //only compare against the searchable range 
-   lc = lc.slice(curs+1) 
-   // try to find next occurence of sought 
-   var pos = lc.search(sought); 
-   if ( pos > -1) 
-   {  
-      //we found it 
-      pos  = pos + curs +1 
-      thisedt.SetCursorPos(pos) 
-      thisedt.SetSelection(pos, pos+sought.length); 
-   } 
-   else 
-   { 
-      //not found - go to start of text 
-      //user can search again from there if required. 
-      thisedt.SetCursorPos(0) 
-   } 
-} 
-</code> 
-This function can be used in an app in the manner shown in this sample code 
-<code JavaScript samplefind.js> 
-var edt,fnd; 
-//Called when application is started. 
-function OnStart() 
-{ 
-    //Create a layout with objects vertically centered. 
-    var lay = app.CreateLayout( "linear", "VCenter,FillXY" );     
-    var hlay = app.CreateLayout( "linear", "Horizontal,FillX" ); 
-    var btn = app.CreateButton("Find"); 
-    btn.SetOnTouch(doFind); 
-    hlay.AddChild(btn); 
-    fnd = app.CreateTextEdit("show"); 
-    fnd.SetHint("Find"); 
-    hlay.AddChild(fnd); 
-    lay.AddChild(hlay); 
-    //Create a text label and add it to layout. 
- 
-    var scrollEdit = app.CreateScroller( 1.0, 0.83, "" ); 
-    //Create a textedit and add it to layout. 
-    edt = app.CreateTextEdit( "",1,-1,"Left,NoSpell" ); 
-    edt.SetText(app.ReadFile("/android_asset/app.js")) 
-    //edt.SetBackColor("#ffcccccc"); 
-    //edt.SetTextColor("#ff333333"); 
-    scrollEdit.AddChild(edt); 
-    lay.AddChild( scrollEdit); 
- 
-     
-    //Add layout to app.     
-    app.AddLayout( lay ); 
-} 
- 
-function doFind() 
-{ 
-   app.HideKeyboard(); 
-   var findstring = fnd.GetText(); 
-   findtext(edt,findstring) 
-} 
- 
-function findtext(thisedt,sought) 
-//thisedt = textEdit to be searched. 
-//sought = string to be searched for. 
-{ 
-   //both toLowerCase for case-insensitive search 
-   var lc = thisedt.GetText().toLowerCase(); 
-   sought = sought.toLowerCase(); 
-   //current cursor position is start for search 
-   var curs = thisedt.GetCursorPos(); 
-   //only compare against the searchable range 
-   lc = lc.slice(curs+1) 
-   // try to find next occurence of sought 
-   var pos = lc.search(sought); 
-   if ( pos > -1) 
-   {  
-      //we found it 
-      pos  = pos + curs +1 
-      thisedt.SetCursorPos(pos) 
-      thisedt.SetSelection(pos, pos+sought.length); 
-   } 
-   else 
-   { 
-      //not found - go to start of text 
-      //user can search again from there if required. 
-      thisedt.SetCursorPos(0) 
-   } 
-} 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
- 
-</code> 
sample_code/textedit_search.txt · Last modified: 2015/08/05 14:40 (external edit)