DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:decimal_input

Differences

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

Link to this comparison view

sample_code:decimal_input [2015/03/03 16:02] (current)
octazid created
Line 1: Line 1:
 +===== Decimal Values in a Textedit =====
 +(Sample code by Steve Garman posted in the DroidScript Google Group)\\
  
 +Please note that this sample does not allow negative numbers but it does allow more than 1 dot, so the string will still need to be checked later to see if it is a valid number. You can look at [[sample_code:​numeric_input|this sample]] too.\\
 +
 +<code javascript>​
 +//Called when application is started.
 +function OnStart()
 +{
 +    //Create a layout with objects vertically centered.
 +    lay = app.CreateLayout( "​linear",​ "​VCenter,​FillXY"​ );    ​
 +
 +    //Create a text label and add it to layout.
 +    txt = app.CreateTextEdit( "",​-1,​-1,"​Number"​ );
 +    txt.lastPos=0;​
 +    txt.SetOnChange( txtOnChange );
 +    lay.AddChild( txt );
 +    ​
 +    //Add layout to app.    ​
 +    app.AddLayout( lay );
 +}
 +
 +function txtOnChange()
 +{
 +   var s1 = this.GetText();​
 +   var s2 = s1.replace(/​[^0-9\.]/​g,​ ""​);​
 +   if(s1 != s2)
 +   {
 +      this.SetText(s2);​
 +      this.SetCursorPos(this.lastPos); ​
 +   }else
 +   {
 +      this.lastPos = this.GetCursorPos();​
 +
 +   }
 +}
 +
 +</​code>​
sample_code/decimal_input.txt ยท Last modified: 2015/03/03 16:02 by octazid