首页 文章

如何指定或获取本机脚本文本字段的资源ID

提问于
浏览
11

我们正在为我们的移动应用程序使用带有角度的nativescript . 我想使用Google Play预启动报告功能,但我们的应用需要输入密码 . Google Play允许指定密码,但您需要一个资源名称,以便测试脚本可以识别密码的放置位置 .

如何在视图中或通过后面的代码或通过任何其他方式指定或接收本机文本字段的资源名称?

有问题的观点:

<StackLayout class="form">
    <GridLayout columns="*,90" rows="50">
      <TextField #inviteTx
        col="0"
        height="50"
        autocorrect="false"
        returnKeyType="next"
        (returnPress)="enter(inviteTx.text)"
        class="input input-border"
        secure="false"
        keyboardType="url">
      </TextField>
      <Button col="1" height="50" class="btn btn-primary w-full fa" text="START &#xf105;" (tap)="enter(inviteTx.text)"></Button>
    </GridLayout>
  </StackLayout>

我做了一些研究,发现在原生android中,one could add an id to a TextField by adding an android:id attribute .

<TextView android:id="@+id/nameTextbox"/>

这似乎不适用于nativescript,之后我无法在R.java中找到该资源 .

2 回答

  • 4

    Edit: 原来我误解了OP的问题,那就是“如何使用原生Android视图ID 's for NativeScript'的可视化组件". Instead I answered the question "如何在NativeScript中使用原生Android字符串” .

    我最后在这里单独回答了原始问题,这个答案仍然是关于在NativeScript中使用原生Android字符串,因为我认为这比使用原生Android ID更有用 .

    How to use native Android strings in NativeScript

    要访问Android本机资源,您可以使用the Util package中的方法 getApplication()getStringId() ,如下所示:

    // This module will give us access to the native Android context
    // See https://docs.nativescript.org/core-concepts/utils
    var utilsModule = require("tns-core-modules/utils/utils");
    
    // This method receives a native Android string ID name (like "example_string"),
    // and it returns a native Android integer ID
    const getAndroidStringId = utilsModule.ad.resources.getStringId;
    
    // Android Application object.
    // This object's getString() method receives a native Android integer ID,
    // and returns an actual Android native string
    const androidApp = utilsModule.ad.getApplication()
    
    /*
     *  @param id: a string with the name of the native ID
     *  @return  : the native Android string with that ID
     */
    function getAndroidNativeString(idName) {
        var androidId = getAndroidStringId(idName);
        return androidApp.getString(androidId);
    }
    
    // This is how you use it
    const nativeAndroidString = getAndroidNativeString('example_string_id')
    

    我在this repo中设置了一个简单的示例,该示例使用函数代码在 nativescript create 的42点击应用程序的顶部显示Android本机字符串 . 查看文件app/main-view-model.js,其中字符串从本机Android获取并添加到视图模型,文件app/main-page.xml,第28行,其中本机字符串应用于标签对象 .

  • 2

    How to use native Android ID's as ID's for NativeScript's visual components

    首先,虽然您可以使用Android本机ID作为NS ID 's, native Android won' t做任何有用的事情 . 特别是,如果您使用Android的 View.findViewById() ,则无法找到具有Android本机ID的NS视图 .

    NativeScript's source code中,定义了NativeScript 's view ID'的文件(view-base.ts),无法找到对原生Android的引用,这意味着NativeScript 's view ID'和Android的实际上没有任何关系或交互 . 而且,我查看了Android视图ID 's with a native Android tool called Stetho, and found NativeScript'的所有视图,即使是NativeScript 's XML files do define view ID' s .

    To answer your question ,有一种方法可以使用视图模型为NativeScript提供原生Android ID .

    假设你想在XML中使用Android“@ id / nameTextbox” . 在Java中,这将转换为R.id.nameTextBox,因此在您的NativeScript XML中,您编写:

    <TextField id="{{R.id.nameTextbox}}"/>
    

    大括号的使用意味着您需要从视图模型中获取 R.id.nameTextbox ,因此在视图模型创建功能中添加:

    viewModel.R = <your.application.id>.R
    

    是您的Android应用程序的applicationId,您可以在 app/App_Resources/Android/app.gradle 找到它 .

    该行使您的R类对XML视图可见,因此您可以将R.id中的值不仅用作ID,还可以用作TextField和标签的 text . 如果你这样做,你会发现Android ID真的是长整数,而不是字符串 .

    另外要记住的是,Android上的R是收集库或应用程序上所有资源ID的类,它是在构建时生成的 . 因此,如果您想要查看您的Android项目目前没有的ID R.id.totallyNewId ,您必须在Android上创建它,这不是很方便(here is how

    因此,作为结论,可以在NativeScript上使用R.id,但没有理由为什么你想要这样做 .

相关问题