【问题标题】:Showing soft keyboard in devices when focus() not working当 focus() 不起作用时在设备中显示软键盘
【发布时间】:2017-12-27 22:03:14
【问题描述】:

Angular 2 + Typescript 网络应用:

问题如下:

我发现,在使用 @ViewChild 聚焦元素或使用 .focus() 通过标签或 id 获取元素后,它并没有暴露 iOS 和 Android 设备中的软键盘。

我发现问题是在元素再次显示后使用*ngIfdisplay: none,由于某种原因它不会触发设备中的软键盘。

一种解决方法是设置 [ngClass] 来设置样式,例如: opacity: 0; 加上删除边距和填充。但是这种方式在聚焦和曝光键盘时会滚动。 所以我只需要添加一个position: absolute + bottom: 9999px 来避免滚动。

我想知道我是否可以为此使用不同的(不那么老套的)解决方案

谢谢!

编辑:

隐藏元素是它的父级父级:

<mr-navbar [ngClass]="{'local-fixed-header': isHeaderFixed(), 'local-non-fixed-header': !isHeaderFixed()}" *ngIf="canShowHeader()"></mr-navbar>

所以输入在mr-navbar 的一个子元素中,称为smb-component,即:

<input id="searchInput" #searchInputMobile placeholder="{{ Home.SEARCH_HELP_TEXT_MOBILE | translate }}" [(ngModel)]="searchText" (focus)="onSearchInputFocus($event)" (focusout)="onSearchInputFocusOut($event, Home.SEARCH_HELP_TEXT_MOBILE)" (keyup)="loadSearchModalWithInput($event)" class="search-input visible-xs" tabindex="{{ searchTabIndex }}"/>

【问题讨论】:

    标签: angular input keyboard focus device


    【解决方案1】:

    在 HTML 中:

    <p *ngIf="editing != id" (dblclick)="display(id)">
        {{title}}
    </p>
    <input #input *ngIf="editing===id" [value]="title" (blur)="hide()" >
    

    在你的课堂上:

      ...
      @ViewChild('input') private input;
      shouldFocus = false;
      ...
      ngAfterViewChecked() {
        if (this.shouldFocus) {
            this.input.nativeElement.focus();
        }
        this.shouldFocus = false;
      }
    
    display(index) {
        this.editing = index;
        this.shouldFocus = true;
      }
    
      hide() {
        this.editing = null;
        this.shouldFocus = false;
      }
    

    【讨论】:

    • 感谢 Vega,但焦点本身没有问题,问题是如果元素被隐藏,它将不会显示键盘。我什至尝试在显示元素后添加超时(如 3 秒)并且它也不暴露键盘(焦点有效)
    • 如何触发 this.shouldFocus = true;在元素再次显示之后。因为强制 focus() 应该强制软键盘出现在前面。至少在 IOS 中对我来说是这样的。
    • 这确实有效,但没有显示键盘。我尝试从不将 shouldFocus 更改为 false 并且它显示键盘(也许这是找出修复的好线索,例如超时或其他东西)
    • nvm,即使我从未将 shouldFocus 改回 false,它第一次也无法正常工作,所以这种方式也不起作用......
    • 太糟糕了...我会仔细看看是否可以提供帮助
    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2011-07-29
    • 2011-05-08
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多