This shows you the differences between two versions of the page.
sample_code:unit_convert [2014/11/01 12:45] 106.77.65.188 [Unit Converter] |
sample_code:unit_convert [2014/11/01 20:55] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Unit Converter ====== | ||
- | Simplistic demo of how a unit-converter app could be built.Supports two quantities. | ||
- | ===== The code ===== | ||
- | <code javascript unit_convert.js> | ||
- | var unitval = []; | ||
- | unitval [" | ||
- | unitval [" | ||
- | unitval [" | ||
- | |||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | //Create a horizontal layout. | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | edt = app.CreateTextEdit(" | ||
- | edt.SetTextSize(12); | ||
- | edt.SetOnChange(docalc); | ||
- | spinfrom = app.CreateSpinner(" | ||
- | spinfrom.SetOnTouch(docalc); | ||
- | spinto = app.CreateSpinner(" | ||
- | spinto.SetOnTouch(docalc); | ||
- | txt = app.CreateText( "????", | ||
- | txt.SetTextSize( 12 ); | ||
- | lay.AddChild(edt); | ||
- | lay.AddChild(spinfrom); | ||
- | lay.AddChild(spinto); | ||
- | lay.AddChild( txt ); | ||
- | | ||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | function docalc(){ | ||
- | qty = edt.GetText(); | ||
- | if(spinfrom.GetText() == " | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | var a = unitval[spinfrom.GetText()]; | ||
- | var b = unitval[spinto.GetText()]; | ||
- | var res = qty * a / b; | ||
- | txt.SetText(res); | ||
- | } | ||
- | |||
- | function isNumber(n) { | ||
- | return !isNaN(parseFloat(n)) && isFinite(n); | ||
- | } | ||
- | </ | ||
- | ===== Note ===== | ||
- | This app is in no way the sort that anyone might wish to use in real life. | ||
- | |||
- | It is just a very simplistic demo of the type of approach that might be taken. | ||