如何指定或获取本机脚本文本字段的资源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.
如何在视图中或通过背后的代码或通过任何其他方式指定或接收本机脚本文本字段的资源名称?
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属性将ID添加到TextField.
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"/>
这似乎不适用于本机脚本,此后我无法在R.java中找到资源.
This does not seem to work in nativescript, I cannot find the resource in R.java afterwards.
推荐答案
原来我误解了OP的问题,即"如何将Native 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中的方法getApplication()
和getStringId()
包,就像这样:
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')
我在此存储库中设置了一个简单示例,它使用功能代码在nativescript create
中的42单击应用程序的顶部显示Android本机字符串.查看文件 app/main-view-model.js ,其中从本地Android获取并添加到视图模型的字符串,以及文件
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.
这篇关于如何指定或获取本机脚本文本字段的资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!