खोज…


नेटिवस्क्रिप्ट में जावा कोड लिखें और इसे सीधे जावास्क्रिप्ट में उपयोग करें

यह एंड्रॉइड स्टूडियो में प्रोजेक्ट संरचना की छवि है:

यहाँ छवि विवरण दर्ज करें

यह मूल संरचना परियोजना की परियोजना संरचना की छवि है:

यहाँ छवि विवरण दर्ज करें

जैसा कि आप देखते हैं कि वे समान हैं। इसलिए हम nivescript में java code लिख सकते हैं जैसा कि हम android studio में लिखते हैं।

हम मूल पाठ के डिफ़ॉल्ट ऐप में टोस्ट जोड़ना चाहते हैं। एक नई मूल परियोजना बनाने के बाद इस तरह एक निर्देशिका 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. टाइपस्क्रिप्ट को पता नहीं है कि org क्या है, इसलिए हमने इसे घोषित किया: declare var org:any;

जावास्क्रिप्ट में सीधे देशी एपिस का उपयोग करें

हम मूल पाठ डिफ़ॉल्ट ऐप में टोस्ट जोड़ना चाहते हैं।

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 को कॉल करना चाहिए और यह 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