如何指定或获取 nativescript 文本字段的资源 ID [英] How can I specify or get the resource id of a nativescript textfield
问题描述
我们正在为我们的移动应用程序使用带有 angular 的 nativescript.我想使用 Google Play 发布前报告功能,但我们的应用需要输入密码.Google Play 允许指定密码,但您需要一个资源名称,以便测试脚本可以识别密码的放置位置.
We are using nativescript with angular for our mobile app. I want to use the Google Play pre-launch report feature, but our app requires a password to be entered. Google Play allows specifying a password but you need a resource name so the testing script can identify where to put the password.
如何在视图中或通过代码隐藏或通过任何其他方式指定或接收 nativescript 文本字段的资源名称?
How can I specify or receive the resource name of a nativescript textfield, either in the view or by code behind or via any other means?
有问题的观点:
<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 " (tap)="enter(inviteTx.text)"></Button>
</GridLayout>
</StackLayout>
我做了一些研究,发现在原生 android 中,可以通过添加 android:id 属性向 TextField 添加 id.
I did some research and found out that in native android, one could add an id to a TextField by adding an android:id attribute.
<TextView android:id="@+id/nameTextbox"/>
这在 nativescript 中似乎不起作用,之后我在 R.java 中找不到资源.
This does not seem to work in nativescript, I cannot find the resource in R.java afterwards.
推荐答案
原来我误解了 OP 的问题,即如何将原生 Android 视图 ID 用于 NativeScript 的可视组件".相反,我回答了如何在 NativeScript 中使用原生 Android 字符串"的问题.
Turns out I misunderstood OP's question, which was "how to use native Android view ID's for NativeScript's visual components". Instead I answered the question "how to use native Android strings in NativeScript".
我最终在一个单独的答案中回答了原始问题,这个答案仍然是关于在 NativeScript 中使用原生 Android 字符串,因为我认为这比使用原生 Android ID 更有用.
I ended up answering the original question on a separate answer here, and this answer will still be about using native Android strings in NativeScript, because I think that is more useful than using native Android ID's.
如何在 NativeScript 中使用原生 Android 字符串
要访问 Android 原生资源,您可以使用 Util 包,像这样:
For accessing Android native resources you can use methods getApplication()
and getStringId()
from the Util package, like so:
// 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')
我在 这个 repo 中设置了一个简单的例子,它使用函数代码在 nativescript create
的 42 次点击应用程序的顶部显示一个 Android 原生字符串.查看文件 app/main-view-model.js,其中从原生Android获取并添加到视图模型中的字符串,以及文件app/main-page.xml,第 28 行,其中本机字符串应用于标签对象.
I set up a simple example in this repo, which uses function code to show an Android native string at the top of the 42-click app from nativescript create
. Look at file app/main-view-model.js, where the string obtained from native Android and added to the view model, and file app/main-page.xml, line 28, where the native string is applied to a label object.
这篇关于如何指定或获取 nativescript 文本字段的资源 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!