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

Pau*_*ber 13 android google-play nativescript angular2-nativescript google-play-console

我们正在为我们的移动应用程序使用带有角度的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>
Run Code Online (Sandbox Code Playgroud)

我做了一些研究,发现在原生android中,可以通过添加android:id属性为TextField添加一个id.

<TextView android:id="@+id/nameTextbox"/>
Run Code Online (Sandbox Code Playgroud)

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

SE *_*lio 5

编辑:原来我误解了 OP 的问题,即“如何将原生 Android 视图 ID 用于 NativeScript 的可视组件”。相反,我回答了“如何在 NativeScript 中使用原生 Android 字符串”的问题。

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

如何在 NativeScript 中使用原生 Android 字符串

为了访问Android原生资源,您可以使用方法getApplication(),并getStringId()该util包,就像这样:

// 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')
Run Code Online (Sandbox Code Playgroud)

我在这个 repo 中设置了一个简单的例子,它使用函数代码在来自nativescript create. 查看文件app/main-view-model.js,其中从原生 Android 获取字符串并添加到视图模型,以及文件app/main-page.xml,第 28 行,其中原生字符串应用于标签对象.