nativescript
Zugriff auf native Apis
Suche…
Schreiben Sie Java-Code in Nativescript und verwenden Sie ihn direkt in Javascript
Dies ist das Bild der Projektstruktur in Android Studio:
Dies ist das Bild der Projektstruktur des Nativescript-Projekts:
Wie Sie sehen, sind sie gleich. So können wir Java-Code in Nativescript schreiben, während wir in Android Studio schreiben.
Wir möchten Toast zur Standard-App von nativescript hinzufügen. Nachdem Sie ein neues Nativescript-Projekt erstellt haben, erstellen Sie ein Verzeichnis wie das java/org/example Verzeichnis:
Erstellen Sie eine neue Datei MyToast.java im example .
MyToast.java:
package org.example;
import android.widget.Toast;
import android.content.Context;
public class MyToast{
public static void showToast(Context context,String text ,String StrDuration ){
int duration;
switch (StrDuration){
case "short":
duration = Toast.LENGTH_SHORT;
break;
case "long":
duration = Toast.LENGTH_LONG;
break;
}
Toast.makeText(context,text, Toast.LENGTH_SHORT).show();
}
}
Hinweise : Den Paketnamen nicht vergessen.
app.component.ts:
import {Component} from "@angular/core";
let application = require("application");
declare var org:any;
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public counter: number = 16;
public get message(): string {
if (this.counter > 0) {
return this.counter + " taps left";
} else {
return "Hoorraaay! \nYou are ready to start building!";
}
}
public onTap() {
this.counter--;
org.example.MyToast.showToast(application.android.context,"You pressed the button","short");
}
}
Wenn Sie jetzt die Taste drücken, wird ein Toast angezeigt.
Anmerkungen :
- Die showToast-Funktion akzeptiert den Kontext, um ihn an
Toast.makeTextzuToast.makeTextwir übergeben ihm einen Kontext auf folgende Weise:application.android.context - typscript weiß nicht, was
orgist, also haben wir es deklariert:declare var org:any;
Verwenden Sie native Apis direkt in Javascript
Wir möchten Toast zur Standard-App für Nativescript hinzufügen.
import {Component} from "@angular/core";
let application = require("application");
declare var android:any;
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public counter: number = 16;
public get message(): string {
if (this.counter > 0) {
return this.counter + " taps left";
} else {
return "Hoorraaay! \nYou are ready to start building!";
}
}
public onTap() {
this.counter--;
this.showToast("You pressed the button","short");
}
public showToast(text:string ,StrDuration:string ):void{
let duration:number;
switch (StrDuration){
case "short":
duration = android.widget.Toast.LENGTH_SHORT;
break;
case "long":
duration = android.widget.Toast.LENGTH_LONG;
break;
}
android.widget.Toast.makeText(application.android.context,text, android.widget.Toast.LENGTH_SHORT).show();
}
}
Toast.makeText Toast zu erstellen, sollten wir Toast.makeText und es ist im android.widget.Toast Paket enthalten. Toast.makeText akzeptiert Kontext als erstes Argument und wir können den Kontext in Nativescript auf folgende Weise abrufen: application.android.context


