首页 文章

android 4.4离子硬件按钮后退关闭键盘

提问于
浏览
1

我使用了离子框架并在android 4.4中构建了一个应用程序 . 当我在一些视图上放置输入字段并在其中进行焦点时,键盘显示,但是当我按下硬件后退按钮时,我希望它隐藏键盘,但它关闭当前视图并返回历史视图,如何让它隐藏键盘并且不要关闭当前的视图页面?如果焦点不在输入字段中,则按返回按钮让它关闭当前视图是否正常 .

$ionicPlatform.ready(function() {
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            // org.apache.cordova.statusbar required
            // StatusBar.styleDefault();
            StatusBar.styleBlackOpaque();
        }
    });
$ionicPlatform.registerBackButtonAction(function (e) {
        e.preventDefault();
       console.log("aaaaaa:"+$cordovaKeyboard.isVisible());
       function showConfirm() {
           var confirmPopup = $ionicPopup.confirm({
               title: '<strong>exit?</strong>',
               template: 'exit?',
               okText: 'exit',
               cancelText: 'cancel'
           });
           confirmPopup.then(function (res) {
               if (res) {
                   ionic.Platform.exitApp();
               } else {

               }
           });
       }

       if ($location.path() == '/app/home') {
           showConfirm();
       } else
       if ($ionicHistory.backView()) {

            if($cordovaKeyboard.isVisible()) {
                  $cordovaKeyboard.close();
            } else {
                $ionicHistory.goBack();
            }
       } else {
           showConfirm();
        }
        return false;
    }, 101);

$ cordovaKeyboard.isVisible()总是在$ ionicPlatform.registerBackButtonAction()中返回false .

1 回答

  • 1

    好的,我找到了关于这个问题的解决方案 .

    window.addEventListener('native.keyboardhide', keyboardHideHandler);
            function keyboardHideHandler(e){
              var test=$ionicPlatform.registerBackButtonAction(function (e) {
              },1000);
                $timeout(function() {
                  test();
                }, 100);
            }
    

    我知道这种方法不是最佳实践,但它可以工作 . 如果有人发现更好的方法或这种方法产生一些错误,请告诉我 .

相关问题