在一些应用中我们需要判断键盘是否已经出现。如果出现的话,我们有时不希望有键盘。我们也可以通过软件的方法让键盘消失。在这篇文章中,我们来介绍如何来实现这个。
import QtQuick 2.0
import Ubuntu.Components 1.1
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "inputmethod.liu-xiao-guo"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(60)
height: units.gu(85)
Page {
title: i18n.tr("inputmethod")
Column {
spacing: units.gu(2)
TextField {
id: input
}
Text {
text: "Input method: " + "<b>" + Qt.inputMethod.visible + "</b>"
}
Button {
text: "Hide Input method"
onClicked: {
Qt.inputMethod.hide();
}
}
}
Component.onCompleted: {
var keys = Object.keys(Qt.inputMethod);
for(var i = 0; i < keys.length; i++) {
var key = keys[i];
// prints all properties, signals, functions from object
console.log(key + ' : ' + Qt.inputMethod[key]);
if (key === "locale") {
console.log("Native lang: " + Qt.inputMethod[key].nativeLanguageName);
}
}
var rect = Qt.inputMethod.keyboardRectangle;
console.log("keyboard size: " + rect.width + " " + rect.height);
}
}
}
在上面的例子里,我们可以看到当键盘没有启动时:
Qt.inputMethod.visible
为false。当键盘启动后,它的值变为true。当然我们也可以通过方法:
Qt.inputMethod.hide();
来让键盘消失。
整个项目的源码在:https://github.com/liu-xiao-guo/inputmethod
具体更多关于Qt.inputMethod的介绍,请参阅:http://doc.qt.io/qt-5/qinputmethod.html