This shows you the differences between two versions of the page.
sample_code:textedit_search [2015/08/05 06:40] octazid Add simple Sample |
sample_code:textedit_search [2015/08/05 14:40] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Simple Custom Search Bar ====== | ||
- | A simple custom search bar could be made up from a TextEdit control and a search button control, contained within a horizontal Linear Layout. The code below is a very simple quick implementation. | ||
- | |||
- | <code javascript search.js> | ||
- | var txtSize = 22; | ||
- | |||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | lay = app.CreateLayout( " | ||
- | lay.SetBackground( "/ | ||
- | |||
- | //Create a layout to hold the search controls | ||
- | laySearch = app.CreateLayout( " | ||
- | laySearch.SetBackColor( "# | ||
- | laySearch.SetPadding( 0, 0.01, 0, 0.01 ); | ||
- | |||
- | //Create a TextEdit control for entering search text | ||
- | txeSearch = app.CreateTextEdit( "", | ||
- | txeSearch.SetTextSize( txtSize ); | ||
- | |||
- | //Create a search button | ||
- | btnSearch = app.CreateText( " | ||
- | btnSearch.SetTextSize( txtSize ); | ||
- | btnSearch.SetOnTouch( btnSearch_OnTouch ); | ||
- | |||
- | laySearch.AddChild( txeSearch ); | ||
- | laySearch.AddChild( btnSearch ); | ||
- | lay.AddChild( laySearch ); | ||
- | |||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | //Called when the search button is pressed | ||
- | function btnSearch_OnTouch( ev ) | ||
- | { | ||
- | if( ev.action==" | ||
- | { | ||
- | this.SetTextSize( txtSize-4 ); | ||
- | } | ||
- | else if( ev.action==" | ||
- | { | ||
- | this.SetTextSize( txtSize ); | ||
- | |||
- | var searchText = txeSearch.GetText(); | ||
- | app.ShowPopup( " | ||
- | } | ||
- | } | ||
- | </ | ||
- | //(Sample posted by Chris Hopkin in the Droidscript Forum)// | ||
- | ---- | ||
- | |||
- | |||
- | ====== 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, | ||
- | // | ||
- | // | ||
- | { | ||
- | // | ||
- | var lc = thisedt.GetText().toLowerCase(); | ||
- | sought = sought.toLowerCase(); | ||
- | // | ||
- | var curs = thisedt.GetCursorPos(); | ||
- | // | ||
- | lc = lc.slice(curs+1) | ||
- | // | ||
- | var pos = lc.search(sought); | ||
- | if ( pos > | ||
- | { | ||
- | // | ||
- | pos = pos + curs +1 | ||
- | thisedt.SetCursorPos(pos) | ||
- | thisedt.SetSelection(pos, | ||
- | } | ||
- | else | ||
- | { | ||
- | // | ||
- | // | ||
- | thisedt.SetCursorPos(0) | ||
- | } | ||
- | } | ||
- | </ | ||
- | This function can be used in an app in the manner shown in this sample code | ||
- | <code JavaScript samplefind.js> | ||
- | var edt, | ||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | var lay = app.CreateLayout( " | ||
- | var hlay = app.CreateLayout( " | ||
- | var btn = app.CreateButton(" | ||
- | btn.SetOnTouch(doFind); | ||
- | hlay.AddChild(btn); | ||
- | fnd = app.CreateTextEdit(" | ||
- | fnd.SetHint(" | ||
- | hlay.AddChild(fnd); | ||
- | lay.AddChild(hlay); | ||
- | // | ||
- | |||
- | var scrollEdit = app.CreateScroller( 1.0, | ||
- | // | ||
- | edt = app.CreateTextEdit( "", | ||
- | edt.SetText(app.ReadFile("/ | ||
- | // | ||
- | // | ||
- | scrollEdit.AddChild(edt); | ||
- | lay.AddChild( scrollEdit); | ||
- | |||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | function doFind() | ||
- | { | ||
- | app.HideKeyboard(); | ||
- | var findstring = fnd.GetText(); | ||
- | findtext(edt, | ||
- | } | ||
- | |||
- | function findtext(thisedt, | ||
- | // | ||
- | // | ||
- | { | ||
- | // | ||
- | var lc = thisedt.GetText().toLowerCase(); | ||
- | sought = sought.toLowerCase(); | ||
- | // | ||
- | var curs = thisedt.GetCursorPos(); | ||
- | // | ||
- | lc = lc.slice(curs+1) | ||
- | // | ||
- | var pos = lc.search(sought); | ||
- | if ( pos > | ||
- | { | ||
- | // | ||
- | pos = pos + curs +1 | ||
- | thisedt.SetCursorPos(pos) | ||
- | thisedt.SetSelection(pos, | ||
- | } | ||
- | else | ||
- | { | ||
- | // | ||
- | // | ||
- | thisedt.SetCursorPos(0) | ||
- | } | ||
- | } | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | </ |