Zoeken…


Schrijf java-code in nativescript en gebruik deze direct in javascript

Dit is het beeld van de projectstructuur in Android studio:

voer hier de afbeeldingsbeschrijving in

Dit is het beeld van de projectstructuur van het nativescript-project:

voer hier de afbeeldingsbeschrijving in

Zoals je ziet zijn ze hetzelfde. dus we kunnen Java-code in nativescript schrijven terwijl we in Android Studio schrijven.

We willen Toast toevoegen aan de standaardapp van nativescript. na het maken van een nieuw nativescript-project, maak een map aan zoals de map java/org/example :

voer hier de afbeeldingsbeschrijving in

maak een nieuw MyToast.java bestand in de 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();
    }
}

Opmerkingen : vergeet de pakketnaam niet;

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");
    }
}

wanneer u nu op de knop drukt, wordt er een toast getoond;

Opmerkingen :

  1. showToast-functie accepteert context om het door te geven aan Toast.makeText en we hebben er op deze manier een context aan doorgegeven: application.android.context
  2. typoscript weet niet wat org is, dus hebben we het verklaard: declare var org:any;

gebruik native apis rechtstreeks in javascript

We willen Toast toevoegen aan de standaardapp van Nativescript.

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();
   }
}

voor het maken van toast moeten we Toast.makeText en deze bevindt zich in het android.widget.Toast pakket. Toast.makeText accepteert context als eerste argument en we kunnen de context op deze manier in het native script krijgen: application.android.context



Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow