如何使用按钮激活键盘

How to activate keyboard with button?

本文关键字:键盘 激活 按钮 何使用      更新时间:2023-09-26

我是新手。我正在使用Phonegap做Android(将来还有iOS)应用程序。我希望我的键盘在点击某个按钮时打开,并且也用按钮关闭。我在 html 中添加了 https://github.com/driftyco/ionic-plugins-keyboard 和按钮中的离子键盘插头:

<div class="button">
             <button id="open_keyb">Click Me!</button>
         </div>

和JavaScript:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        alert(1);
        cordova.plugins.Keyboard.show();
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');
        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
        console.log('Received Event: ' + id);
    }
};
  app.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        if(window.cordova){
            cordova.plugins && cordova.plugins.Keyboard && cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        alert('test');
    });
});
app.directive('input', function($timeout){
    return {
        restrict: 'E',
        scope: {
            'returnClose': '=',
            'onReturn': '&',
            'onFocus': '&',
            'onBlur': '&'
        },
        link: function(scope, element, attr){
            element.bind('focus', function(e){
                if(scope.onFocus){
                    $timeout(function(){
                        scope.onFocus();
                    });
                }
            });
            element.bind('blur', function(e){
                if(scope.onBlur){
                    $timeout(function(){
                        scope.onBlur();
                    });
                }
            });
            element.bind('keydown', function(e){
                if(e.which == 13){
                    if(scope.returnClose) element[0].blur();
                    if(scope.onReturn){
                        $timeout(function(){
                            scope.onReturn();
                        });
                    }
                }
            });
        }
    }
});
 .run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
    ionic.Platform.isFullScreen = true;
  });
})
var fn = function() {
    alert('test');
    document.getElementById('one').onclick = function() {
        alert('click');
        cordova.plugins.Keyboard.show();
    };
};
document.addEventListener('DOMContentLoaded', fn, false);

在 www 文件夹中。这是行不通的。此警报也不会显示。你可以帮我吗?如何使其工作?

没有

事件侦听器被添加到open_keyb按钮,因此不会触发任何内容。

此外,由于您看起来正在使用移动设备,因此您可能还希望添加touchend事件,尽管click在任何情况下都应该有效。

请尝试更换:

var fn = function() {
    alert('test');
    document.getElementById('one').onclick = function() {
        alert('click');
        cordova.plugins.Keyboard.show();
    };
};
document.addEventListener('DOMContentLoaded', fn, false);

有了这个:

document.addEventListener('DOMContentLoaded',function(DOMLoaded){
   document.getElementById('open_keyb').addEventListener('click', function (clickEvent){
       alert('click');
       cordova.plugins.Keyboard.show();
   });
});