用javascript寫Android和iOS naitve應用,實在炫酷。

關注NativeScript有一段時間了,說好了的三月發第一個Beta版,終於發佈了。javascript

// declare the extended NativeScriptActivity functionality
var extendsObject = {
    onCreate: function(savedState){
        // call the base NativeScriptActivity.onCreate method
        // the "this" variable points to a NativeScriptActivity instance
        this.super.onCreate(savedState);

        // create a button and set it as the main content
        var button = new android.widget.Button(this);
        button.setText("Hello World");

        this.setContentView(button);
    }
}

// pass the extends object to create a new NativeScriptActivity instance
var mainActivity = com.tns.NativeScriptActivity.extends(extendsObject);

var applicationInitObject = {
    getActivity: function(intent) {
        // this method is called whenever a new instance of NativeScriptActivity is about to be created
        return mainActivity;
    },
    onCreate: function() {
        // This is the first method called. Called from the android.app.Application.onCreate method.
    } 
}

// The NativeScriptRuntime exposes the app object within the global context
app.init(applicationInitObject);

如上面代碼,用javascript創那建一個Android的Activity,是否是很炫酷!css

固然也能夠寫iOS native應用,這還不是最炫酷的,更炫酷的是,它還提供了一套「公共的」api,和一套能夠用xml描述的ui控件庫,能夠這樣定義界面,並在不一樣的平臺下生成相應用本地控件。java

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
  <StackLayout>
    <Label text="Tap the button" cssClass="title"/>
    <Button text="TAP" tap="{{ tapAction }}" />
    <Label text="{{ message }}" cssClass="message" textWrap="true"/>
    <Button text="test button"/>
  </StackLayout>
</Page>

它還支持「數據綁定」,這你敢信?!android

上樣例:api

相關文章
相關標籤/搜索