android - 按键事件在 Android 设备中不起作用

标签 android angular ionic-framework ionic2 ionic3

我使用 keydown 事件编写了一个函数,让输入只允许数字、'-' 和 BackSpace 键。当我在 ionic serve 或 iOS 中测试时,它起作用了。但是当我在 Android 设备上测试时,它不起作用,输入字段将允许输入所有字符。 当我使用 console.log(事件)时,它总是返回 key=“Unidentified”,keyCode = 229。

环境:Ionic 3、Node v6.11、NPM 3.10、Android 5.1.1

我不知道如何解决这个问题。

.ts文件

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  constructor(public navCtrl: NavController) {

  }

  isValidNumber(event) {
    //console.log(event);
    return /\d|-|Backspace/.test(event.key);

  }
}

.html文件

<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>
<ion-content>
  <ion-item>
      <ion-input  type="text" (keydown)="isValidNumber($event)"></ion-input>
  </ion-item> 
</ion-content>

最佳答案

尝试以下操作:

function isValidNumber(e) {
if (
    !(e.keyCode === 8 || !(e.keyCode >= 48 && e.keyCode <= 57) ||  // 0-9
        !(e.keyCode >= 96 && e.keyCode <= 105) // numpad 0-9
        // some more checking like arrows, delete, backspace, etc.
        || e.keyCode == 229)) { // Check for the 'placeholder keyCode'
    return false;
}
{
    // This is not a valid key
    e.preventDefault();
}
return true;

找到 here

另请阅读以下内容:http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html

关于android - 按键事件在 Android 设备中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45793564/

相关文章:

javascript - Firebase 函数执行和订阅由 Firebase 函数更新的列表

java - 如何在windows平台上显示android文件浏览器

android - mapping.MapView 无法转换为 mapping.SupportMapFragment

angular - 无法绑定(bind)到 'ngForOf',因为它不是 Angular 5 中的已知属性

javascript - Angular 2 rxjs 嵌套 Observables

android - 谷歌地图 API 错误 : RefererNotAllowedMapError on play store

javascript - Ionic 与谷歌分析

android - Android上的渐变按钮颜色变化

java - 从时间戳到正常日期

angular - Observable 中的更改未反射(reflect)在 View 中