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

2024-01-18

我们在我们的移动应用程序中使用带有 Angular 的 NativeScript。我想使用 Google Play 预发布报告功能,但我们的应用程序需要输入密码。 Google Play 允许指定密码,但您需要一个资源名称,以便测试脚本可以识别将密码放在哪里。

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

有问题的观点:

      <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 中,可以通过添加 android:id 属性来向 TextField 添加 id https://developer.android.com/guide/topics/resources/layout-resource.html#idvalue.

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

这似乎在nativescript中不起作用,之后我在R.java中找不到资源。


Edit:原来我误解了OP的问题,那就是“如何将原生 Android 视图 ID 用于 NativeScript 的可视组件“。相反,我回答了这个问题”如何在 NativeScript 中使用原生 Android 字符串".

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

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

要访问 Android 本机资源,您可以使用方法getApplication() and getStringId() from 实用程序包 https://docs.nativescript.org/core-concepts/utils,像这样:

// 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')

我设置了一个简单的例子这个仓库 https://github.com/leoacevedo/nativescript-example-use-android-native-strings,它使用函数代码在 42-click 应用程序的顶部显示 Android 本机字符串nativescript create。看文件应用程序/主视图模型.js https://github.com/leoacevedo/nativescript-example-use-android-native-strings/blob/master/app/main-view-model.js,其中从本机 Android 获取并添加到视图模型的字符串,以及文件应用程序/main-page.xml https://github.com/leoacevedo/nativescript-example-use-android-native-strings/blob/master/app/main-page.xml,第 28 行,其中本机字符串应用于标签对象。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何指定或获取 NativeScript 文本字段的资源 ID 的相关文章

随机推荐