nativescript
ネイティブAPIへのアクセス
サーチ…
nativescriptにJavaコードを記述し、javascriptで直接使用する
これはAndroidスタジオのプロジェクト構造のイメージです:
これは、nativescriptプロジェクトのプロジェクト構造のイメージです。
あなたが見る通り、彼らは同じです。私たちはアンドロイドスタジオで書くので、私たちはnativescriptでJavaコードを書くことができます。
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");
}
}
ボタンを押すとトーストが表示されます。
注 :
- showToast関数はコンテキストを受け入れて
Toast.makeTextに渡します。このようにコンテキストを渡します:application.android.context - typescriptはどのような
orgか分からないので、それを宣言しました:declare var org:any;
ネイティブapisをjavascriptで直接使用する
私はToastを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();
}
}
Toast.makeTextを作成するためにToast.makeTextを呼び出すToast.makeTextあり、それはandroid.widget.Toastパッケージにあります。 Toast.makeTextは最初の引数としてコンテキストを受け取り、このようにしてnativescriptでコンテキストを取得できます: application.android.context
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow


