所以我使用Nativescript为Android和iOS创建应用程序,而且我在使用iOS时遇到了一些问题。
所以我有一个堆栈视图,我在其中添加了一个名为“closeKeyboard”的tap操作(当前只是向控制台发送日志消息)。 在堆栈视图中,我有文本字段。 问题是,当我按下文本字段时,堆栈视图操作也会被触发。
在Android上它按预期工作 - 选择文本字段不会触发堆栈视图操作。
以下是main-page.xml的代码
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="onNavigatingTo">
<StackLayout tap="closeKeyboard">
<Label class="conf-button-label" text="Netto sum" />
<TextField text="{{ sum }}" hint="Type number" returnKeyType="done" keyboardType="number" keyboardPatter="[0-9]*" returnPress="done" id="sum" col="0" row="0" class=""/>
</StackLayout>
</Page>
以下是main-page.js的代码
var createViewModel = require("./main-view-model").createViewModel;
function onNavigatingTo(args) {
page = args.object;
}
function done(){
console.log('Input Done');
}
function closeKeyboard(page){
console.log('Close Keyboard Tapped');
}
exports.done = done;
exports.closeKeyboard = closeKeyboard;
exports.onNavigatingTo = onNavigatingTo;
任何人都可以帮我吗?
答案 0 :(得分:1)
如果您想要创建类似javascript模糊事件或只是隐藏键盘,当您在TextView外部点击时,您可以使用本机代码为ios设置自定义手势。您可以查看下面附带的示例:
主page.xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="loaded">
<StackLayout id="stack">
<Label text="Tap the button" class="title"/>
<Button text="TAP" tap="{{ onTap }}" />
<Label text="{{ message }}" class="message" textWrap="true"/>
<TextView id="text" text="" hint="Enter some text" backgroundColor="yellow" updateTextTrigger="focusLost"/>
</StackLayout>
</Page>
主page.js
var createViewModel = require("./main-view-model").createViewModel;
var gestures = require("ui/gestures");
var observableModule = require("data/observable");
var observableObject = new observableModule.Observable();
var TapHandlerImpl1 = (function (_super) {
__extends(TapHandlerImpl1, _super);
function TapHandlerImpl1() {
_super.apply(this, arguments);
}
TapHandlerImpl1.initWithOwner = function (owner) {
var handler = TapHandlerImpl1.new();
handler._owner = owner;
return handler;
};
TapHandlerImpl1.prototype.tap = function () {
this._owner.ios.resignFirstResponder();
};
TapHandlerImpl1.ObjCExposedMethods = {
"tap": { returns: interop.types.void, params: [interop.types.id] }
};
return TapHandlerImpl1;
}(NSObject));
var tapHandler = null;
function loaded(args) {
var page = args.object;
var field = page.getViewById("text");
if (page.ios) {
tapHandler = TapHandlerImpl1.initWithOwner(field)
var recognizer = UITapGestureRecognizer.alloc().initWithTargetAction(tapHandler, "tap");
page.ios.view.addGestureRecognizer(recognizer);
}
field.addEventListener(observableModule.Observable.propertyChangeEvent, function(pcd){
console.log(pcd.eventName.toString() + " " + pcd.propertyName.toString() + " " + pcd.value.toString());
});
page.bindingContext = observableObject; }
exports.loaded = loaded;
然而,在this GitHub问题中也讨论了同样的问题。
答案 1 :(得分:0)
我认为唯一的方法是将07-08 19:42:14.222 798-815/? I/ActivityManager: START u0 {act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.android.packageinstaller cmp=com.android.packageinstaller/.permission.ui.GrantPermissionsActivity (has extras)} from uid 10206 on display 0
07-08 19:42:14.224 798-815/? W/ActivityManager: Bad activity token: android.os.BinderProxy@ae80cbb
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.am.ActivityRecord$Token
at com.android.server.am.ActivityRecord.forTokenLocked(ActivityRecord.java:424)
at com.android.server.am.ActivityStack.isInStackLocked(ActivityStack.java:467)
at com.android.server.am.ActivityStackSupervisor.isInAnyStackLocked(ActivityStackSupervisor.java:592)
at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1484)
at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1090)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4077)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4064)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:162)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2670)
at android.os.Binder.execTransact(Binder.java:453)
07-08 19:42:14.224 798-815/? W/ActivityManager: Bad activity token: android.os.BinderProxy@ae80cbb
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.am.ActivityRecord$Token
at com.android.server.am.ActivityRecord.forTokenLocked(ActivityRecord.java:424)
at com.android.server.am.ActivityStack.isInStackLocked(ActivityStack.java:467)
at com.android.server.am.ActivityStackSupervisor.isInAnyStackLocked(ActivityStackSupervisor.java:592)
at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1484)
at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1090)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4077)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4064)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:162)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2670)
at android.os.Binder.execTransact(Binder.java:453)
07-08 19:42:14.225 798-815/? W/ActivityManager: startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.content.pm.action.REQUEST_PERMISSIONS flg=0x800000 pkg=com.android.packageinstaller cmp=com.android.packageinstaller/.permission.ui.GrantPermissionsActivity (has extras) }
07-08 19:42:14.258 12029-12029/? I/GrantPermissionsActivity: No package: null
android.content.pm.PackageManager$NameNotFoundException
at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:138)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.getCallingPackageInfo(GrantPermissionsActivity.java:349)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:99)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
从Stacklayout移动到子视图Label。或者,您可以尝试为文本字段定义单独的点按操作,以覆盖stacklayout的点击操作。