在输入字段模糊时在Nativescript中隐藏Android键盘 [英] Hide Android keyboard in Nativescript on input field blur

查看:86
本文介绍了在输入字段模糊时在Nativescript中隐藏Android键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表,其中包含几个TextField和其他输入.当用户点击其中一个字段时,Android软键盘将始终按预期显示.如果我在字段外点击,尽管键盘没有隐藏.有没有一种方法可以捕获此事件,以便当用户在任何输入之外单击时都可以隐藏键盘?

I have a registration form which contains several TextFields and other inputs. When a user taps one of the fields the Android soft keyboard will always show as expected. If I tap outside of the field though the keyboard does not hide. Is there a way to capture this event so that I can hide the keyboard when ever a user taps outside of any of the inputs?

执行以下操作可以让我隐藏键盘

It looks like doing the following allows me to hide the keyboard

var pageContainer = page.getViewById('registration-container');
if(pageContainer.android) {
    pageContainer.android.clearFocus();
}

但是我不确定如何捕获使表格输入模糊的每个轻击事件.我什至不确定Android是否可以做到这一点.

But I'm unsure how to capture every tap event that blurs the forms inputs. I'm not even sure if this is possible with Android.

推荐答案

您可以将点击侦听器置于父视图中,以便在单击它时(在文本字段之外的任何地方),都可以清除该文本字段的焦点,正在显示键盘.您要做的是清除容器的焦点,而该容器应恰好是文本字段:

You can put the on tap listener to the parent view so that when you click on it (anywhere outside textfield), it will clear the focus of the textfield that are showing keyboard. The way you are doing is to clear the focus of the container while it should be exactly the textfield:

在XML中:

<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="onNavigatingTo">
    <StackLayout tap="clearTextfieldFocus">
        <Label text="Tap the button" class="title"/>
        <Label text="{{ message }}" class="message" textWrap="true"/>
        <TextField id="myTextfield" hint="Type here..."/>
    </StackLayout>
</Page>

page.js中:

function clearTextfieldFocus(args) {
    var layout = args.object;
    var myTextfield = layout.getViewById("myTextfield");
    myTextfield.android.clearFocus();
}
exports.clearTextfieldFocus = clearTextfieldFocus;

P/s:文本字段上的点击侦听器将覆盖父侦听器,因此在文本字段上单击仍然会聚焦并显示键盘

P/s: the tap listener on the textfield will override the parent listener, so clicking on textfield still focus and show keyboard

这篇关于在输入字段模糊时在Nativescript中隐藏Android键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆