Angular2/Nativescript:在软键盘上按下返回键时提交表单

use*_*257 3 nativescript angular2-nativescript angular

我有一个带有用户名和密码 TextFields 的 Angular2/Nativescript 登录组件...当编辑密码 textField 时,我将 returnKeyType 设置为“done”,并期望在软键盘上按下 Done 时调用 login() 函数。在按下完成键的那一刻,键盘被解除,但登录功能没有被调用,所以我仍然需要在键盘被解除后点击登录按钮才能提交表单。当在 Nativescript 中的特定 TextField 上按下返回键时,有什么方法可以提交表单?如果是这样,我该如何实施它?我尝试了 returnPress 事件,但没有任何反应......

我的代码:

<ActionBar title="Login"></ActionBar>
<StackLayout class="page">
    <GridLayout columns="*, auto" rows="auto">
        <ActivityIndicator class="m-l-10 m-t-10 activity-indicator" [busy]="busy" [visibility]="busy ? 'visible' : 'collapse'" horizontalAlignment="left"></ActivityIndicator>
        <Button row="0" col="1" id="setIPBtn" class=" m-t-20 pull-right font-awesome" text="&#xf0ad; Settings" (tap)="setIP()"></Button>
    </GridLayout>

    <Label class="m-x-auto m-t-20 title h1 text-primary p-x-10" text="Log In" backgroundColor="blue"></Label>
    <StackLayout class="form">
        <StackLayout class="input-field">
            <Label class="body label text-left" text="Enter Username"></Label>
            <TextField class="input input-border" hint="Username" [(ngModel)]="username" autocorrect="false" autocapitalizationType="none" returnKeyType="next"></TextField>
        </StackLayout>
        <StackLayout class="input-field">
            <Label class="body label text-left" text="Enter Username"></Label>
            <TextField class="input input-border" secure="true" hint="Password" [(ngModel)]="password" autocorrect="false" autocapitalizationType="none" returnKeyType="done" returnPress="login()"></TextField>            
        </StackLayout>
        <Button class="btn btn-submit font-awesome bg-primary" [text]="isLoggingIn ? 'Logging in...' : '&#xf090; Login'" (tap)="login()" [isEnabled]="username !== '' && username !== null && password !== '' && password !== null && !isLoggingIn"></Button>
    </StackLayout>      



</StackLayout>
Run Code Online (Sandbox Code Playgroud)

小智 5

尝试这个:

(returnPress)="login()"
Run Code Online (Sandbox Code Playgroud)

  • 此外,将 `returnKeyType="done"` 添加到输入组件也很重要,这样用户就知道它将像问题中的示例一样发送表单。 (2认同)