如何在单击文本框时隐藏IONIC 2中的原生Android键盘?

时间:2017-11-03 09:46:22

标签: ionic2

如何使用IONIC 2单击文本框时隐藏原生Android键盘?我从https://ionicframework.com/docs/native/keyboard/链接安装了IONIC键盘插件并使用了this.keyboard.close(); 但键盘仍在打开。帮我看看如何关闭键盘。我基本上是在TEXTBOX enter image description here中的datepicker插件中显示DOB。

这是ts文件(datepicker1.ts)

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
    import { DatePicker } from '@ionic-native/date-picker';
    import { Keyboard } from '@ionic-native/keyboard';
    @IonicPage()
@Component({
  selector: 'page-datepicker1',
  templateUrl: 'datepicker1.html',
})
export class Datepicker1Page {
  public today:any;
  constructor(public navCtrl: NavController, public navParams: NavParams,private datePicker: DatePicker,private keyboard: Keyboard) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad Datepicker1Page');
  }
  openDatepicker()
  {

    this.keyboard.close();
    this.datePicker.show({
      date: new Date(),
      mode: 'date',
      androidTheme: this.datePicker.ANDROID_THEMES.THEME_DEVICE_DEFAULT_LIGHT
    }).then(
      date => {
        this.today=date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear()},
      err => console.log('Error occurred while getting date: ', err)
    );
  }

}

这是datepicker1.html网页

<ion-header>
  <ion-navbar>
    <ion-title>datepicker page</ion-title>
  </ion-navbar>
</ion-header>
<ion-content padding>
    <ion-item>
        <ion-label>DOB</ion-label>
        <ion-input type="text" name="DOB" (click)="openDatepicker()" [(ngModel)]="today" ng-readonly></ion-input>
      </ion-item>
</ion-content>

2 个答案:

答案 0 :(得分:0)

您错过了在班级中声明today变量,但您错过了在disabled="true"标记中添加ion-input。一切都很好,我已经测试过了。

TS档案

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Keyboard } from '@ionic-native/keyboard';
import { DatePicker } from '@ionic-native/date-picker';

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

  constructor(public navCtrl: NavController, public keyboard : Keyboard, public datePicker : DatePicker) {

  }

  today : any;
  openDatepicker(){
    this.keyboard.close();
    this.datePicker.show({
      date: new Date(),
      mode: 'date',
      androidTheme: this.datePicker.ANDROID_THEMES.THEME_DEVICE_DEFAULT_LIGHT
    }).then(
      date => {
        this.today=date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear()},
      err => console.log('Error occurred while getting date: ', err)
    );
  }

}

HTML文件

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <ion-item>
      <ion-label>DOB</ion-label>
      <ion-input disabled="true" type="text" name="DOB" (click)="openDatepicker()" [(ngModel)]="today" ng-readonly></ion-input>
    </ion-item>
</ion-content>

答案 1 :(得分:0)

1)import { DatePicker } from '@ionic-native/date-picker/ngx';在app.module.ts和keyboard.page.ts

2)public keyboard : Keyboard,在构造函数中注入

3)https://ionicframework.com/docs/native/keyboard从此官方网站获取参考

openDatepickerStart(){

    setTimeout(() => {
      this.keyboard.hide();
    }, 100);

    this.datePicker.show({
      date: new Date(),
      mode: 'date',
      androidTheme: this.datePicker.ANDROID_THEMES.THEME_DEVICE_DEFAULT_LIGHT
    }).then(
      date => {
        this.SelectDateModelDetails.get('StartTime').patchValue(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear())},
      err => console.log('Error occurred while getting date: ', err)
    );
  }