import { BusinessError } from '@kit.BasicServicesKit';
import { inputMethod } from '@kit.IMEKit';
import { webview } from '@kit.ArkWeb';
let textConfig: inputMethod.TextConfig = {
inputAttribute: {
textInputType: 0,
enterKeyType: 1
}
};
@Entry
@Component
struct WebFocus1 {
controller: webview.WebviewController = new webview.WebviewController();
inputMethodController = inputMethod.getController();
onPageShow(): void {
setTimeout(() => { // 因为inputController的挂载时间点晚于onPageShow,应用在启动时可以自动聚焦并且弹出软键盘,但是退出后台后再进入app焦点在输入框,没有弹出键盘,此时需要增加延时代码。
console.info(`Failed to attach:${this.inputMethodController}`);
try {
this.inputMethodController.attach(true, textConfig, (err: BusinessError) => {
if (err) {
console.error(`Failed to attach: ${err.code}, ${err.message}`); // 从后台切进来,报错Input method client error
return;
}
console.info('Succeeded in attaching the inputMethod.');
});
} catch (err) {
console.error(`Failed to attach: ${err.code}, ${err.message}`);
}
}, 100);
}
build() {
Row() {
Column() {
Web({ src: $rawfile('index1.html'), controller: this.controller })
.javaScriptAccess(true)
.domStorageAccess(true)
.fileAccess(false)
.geolocationAccess(false)
.onPageVisible(() => {
this.inputMethodController.showTextInput((err: BusinessError) => {
if (err) {
console.error(`Failed to showTextInput: ${err.code}, ${err.message}`);
return;
}
console.info('Succeeded in showing the inputMethod.');
});
});
}.width('100%');
}.height('100%');
}
} <!DOCTYPE html>
<html>
<head>
<title>输入框获焦</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<script type="text/javascript">
function handleFocus() {
console.info("输入框已获焦");
}
</script>
</head>
<body>
<input type="text" autofocus="true" placeholder="点击我" />
</body>
</html> 在HarmonyOS代码中添加JS函数调用,确保键盘回收后输入框自动失焦。
import { webview } from '@kit.ArkWeb';
import { window } from '@kit.ArkUI';
@Entry
@Component
struct WebFocus2 {
lastWindow: window.Window | undefined = undefined;
onPageShow(): void {
console.info('onPageShow');
window.getLastWindow(this.getUIContext().getHostContext()).then((lastWindow) => {
this.lastWindow = lastWindow;
lastWindow.on('keyboardHeightChange', (data) => {
console.info('开始监听');
if (data == 0) {
this.webController.runJavaScript('lostFocus()');
}
console.info(`Succeeded in enabling the listener for keyboard height changes. Data: ${data}`);
});
});
}
onPageHide(): void {
console.info('onPageHide');
this.lastWindow?.off('keyboardHeightChange');
}
webController: webview.WebviewController = new webview.WebviewController();
build() {
RelativeContainer() {
Web({
src: $rawfile('index2.html'),
controller: this.webController
})
.javaScriptAccess(true)
.domStorageAccess(true)
.fileAccess(false)
.geolocationAccess(false);
}
.height('100%')
.width('100%');
}
} html示例代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>H5侧</h1>
<div>
<input id="myInput" type="text" value="" style="margin:10px 0" onload /><br />
</div>
</body>
<script>
function test() {
document.getElementById("myInput").focus();
}
function lostFocus() {
console.info('HTML: lostFocus called');
const inputs = document.querySelectorAll('input');
if (inputs.length === 0) {
console.info('HTML: no input selected');
}
// 遍历并失焦
inputs.forEach(input => {
input.blur();
});
}
// 当文档加载时立即执行函数
document.addEventListener('DOMContentLoaded', test);
</script>
</html>
<style>
body {
width: 100%;
height: auto;
margin: 50px auto;
text-align: center;
}
</style> 解决方案二:
若想关闭软键盘时触发Input文本框的失焦事件可设置Web组件的blurOnKeyboardHideMode属性为BlurOnKeyboardHideMode.BLUR。
ArkTS示例代码如下:
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebFocus3 {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Row() {
Column() {
Web({ src: $rawfile('index3.html'), controller: this.controller })
.javaScriptAccess(true)
.domStorageAccess(true)
.fileAccess(false)
.geolocationAccess(false)
.blurOnKeyboardHideMode(BlurOnKeyboardHideMode.BLUR)
}.width('100%');
}.height('100%');
}
} html示例代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>H5侧</h1>
<div>
<input id="myInput" type="text" value="" style="margin:10px 0" onload /><br />
</div>
</body>
<script>
function test() {
document.getElementById("myInput").focus();
}
// 当文档加载时立即执行函数
document.addEventListener('DOMContentLoaded', test);
</script>
</html>
<style>
body {
width: 100%;
height: auto;
margin: 50px auto;
text-align: center;
}
</style>