수색…


nativescript에 자바 코드를 작성하고 자바 스크립트에서 직접 사용하십시오.

Android Studio의 프로젝트 구조 이미지입니다.

여기에 이미지 설명을 입력하십시오.

이것은 nativescript 프로젝트의 프로젝트 구조 이미지입니다.

여기에 이미지 설명을 입력하십시오.

보시다시피 그들은 동일합니다. 그래서 우리는 안드로이드 스튜디오에서 작성하는 것처럼 nativescript에 자바 코드를 작성할 수 있습니다.

nativescript의 기본 앱에 Toast를 추가하려고합니다. 새 nativescript 프로젝트를 만든 후 다음과 같이 java/org/example 디렉토리에 디렉토리를 만듭니다.

여기에 이미지 설명을 입력하십시오.

example 디렉토리에 새로운 MyToast.java 파일을 생성합니다.

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

참고 사항 : 패키지 이름은 잊지 마십시오.

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

이제 버튼을 누르면 축배가 표시됩니다.

참고 사항 :

  1. showToast 함수는 컨텍스트를 받아 들여 Toast.makeText 에 전달합니다.이 방식으로 컨텍스트를 전달했습니다 : application.android.context
  2. typescript는 어떤 org 인지 알지 못하므로 선언했습니다. declare var org:any;

javascript에서 네이티브 api를 직접 사용하십시오.

우리는 nativescript default app에 Toast를 추가하려고합니다.

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를 만들기 위해 Toast.makeText 를 호출해야하며 android.widget.Toast 패키지에 있습니다. Toast.makeText 는 컨텍스트를 첫 번째 인수로 받아들이고 다음과 같이 Toast.makeText 컨텍스트를 가져올 수 있습니다. application.android.context



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow