如何使用按钮激活键盘
How to activate keyboard with button?
我是新手。我正在使用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();
});
});
相关文章:
- 激活chrome上的chrome.notifications对象
- 提示使用服务器端事件处理程序激活JavaScript
- 如何从外部页面激活非默认引导选项卡
- 选项卡侦听器未被来自后台脚本的消息激活
- 基于localStorage的Meteor激活模板
- 如何通过按键激活按钮应用CSS效果
- Android键盘不适用于包含Javascript的网页
- javascript虚拟键盘
- iPad虚拟键盘-哪一个-javasctript解决方案
- 在BoilerplateJS浏览器加载所有脚本(在其他模块中),而不考虑激活的模块
- React Native DeviceEventEmitter键盘WillShow停止工作
- 我必须点击两次才能激活任何按钮操作(离子/角度)
- 通过键盘编写SVG文本
- 使用键盘箭头在画布上移动字符串
- 流星:点击激活其他模板
- 关闭在加载时激活,而不是复选框更改
- 将select2与jQuery虚拟键盘配合使用
- 显示数字键盘的Phonegap-js提示
- 如何使用按钮激活键盘
- Firefox键盘快捷键是否只在浏览器启动时激活?