【每日学点鸿蒙知识】人脸活体检测、NodeController刷新、自动关闭输入框、Row设置中间最大宽、WebView单例
1、HarmonyOS 人脸活体检测调用?
H5调用应用侧方法可参考以下demo:
index.ets
Web()//注册方法
.javaScriptProxy({
object: this.testObj,
name: "testObjName",
methodList: ["getLocationTS"],
controller: this.webController
})
class testClass {
constructor() {
}
async getLocationTS(): Promise<string> {
//应用侧方法逻辑
}
H5页面
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<title>获取定位信息</title>
<style>
#prize {
border-radius: 16px 16px 16px 16px;
background-image: linear-gradient(180deg, #A2DAFF 0%, #EAF5FF 100%);
margin-left: 1.82%;
margin-top: 1.43%;
width: 96.5%;
height: 96.7%;
}
</style>
</head>
<body>
<div style="width:1000px;height:500px">
<input style="width:1000px;height:100px;font-size:30px" id="inputText"/>
<p style="width:1000px;height:100px;font-size:30px" id="demo">p1</p>
<button style="width:1000px;height:100px" onclick="getLocation()">
<span style="font-size:30px">获取定位</span>
</button>
</div>
<script>
function getLocation() {
<!-- ArtTS侧返回的如果是 promise对象,使用改方法处理 -->
<!-- 具体可参考前端页面调用应用侧函数API,包含简单类型和复杂类型-->
<!-- https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/web-in-page-app-function-invoking-0000001844832802-->
<!-- H5侧方法调用 -->
testObjName.getLocationTS()
.then((param)=>{ document.getElementById("demo").innerHTML = "testObjName.getLocationTS()--"+JSON.stringify(param) })
.catch((param)=>{ document.getElementById("demo").innerHTML = "testObjName.getLocationTS()--"+JSON.stringify(param) })
}
</script>
</body>
</html>
}
2、HarmonyOS NodeController调用this.rebuild()后,画面没有刷新?
使用NodeController,首次进入页面,makeNode被自动回调,绘制出了3个题目框。之后外部调用onSelectBoxChange,更新了nodeArray并手动调用this.rebuild(),makeNode再次被回调,但是页面上并没有出现新绘制的框。
参考demo:
import { RenderNode, DrawContext, FrameNode, NodeController } from "@ohos.ArkUI.node"
import drawing from "@ohos.graphics.drawing"
class TextRenderNode extends RenderNode {
async draw(context: DrawContext) {
const canvas = context.canvas;
const pen = new drawing.Pen();
pen.setStrokeWidth(5);
pen.setColor({alpha: 255, red: 255, green: 0, blue: 0});
let path = new drawing.Path();
path.moveTo(10,10);
path.arcTo(10, 10, 200, 200, 0, 150);
path.close();
canvas.attachPen(pen);
canvas.drawPath(path);
canvas.detachPen();
}
}
class MyNodeController extends NodeController {
private rootNode: FrameNode | null = null;
private nodeArray: Array<TextRenderNode> = []
addMyNode(item:TextRenderNode){
this.nodeArray.push(item)
}
makeNode(uiContext: UIContext): FrameNode | null {
this.rootNode = new FrameNode(uiContext);
const renderNode = this.rootNode.getRenderNode();
if (renderNode === null) {
return this.rootNode;
}
if (renderNode) {
renderNode.clearChildren()
this.nodeArray.forEach((item) => {
renderNode.appendChild(item)
})
}
return this.rootNode;
}
// onTouchEvent(event: TouchEvent): void {
// this.nodeArray.forEach((node, index) => { // this是undefined,无法获取到当前类的一些参数<---------
// console.log(JSON.stringify(this.nodeArray) + '德玛西亚')
// })
// }
}
@Entry
@Component
struct Index {
@State newNode:TextRenderNode = new TextRenderNode();
@State nodeCV:MyNodeController = new MyNodeController()
tempY = 0
build() {
Column() {
Column() {
NodeContainer(this.nodeCV)
.width('100%')
.height(200)
Button('Invalidate')
.onClick(() => {
let newNode = new TextRenderNode()
newNode.frame = { x: 0, y: this.tempY, width: 200, height: 100 }
this.nodeCV.addMyNode(newNode)
this.nodeCV.rebuild()
this.tempY += 100
})
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
3、HarmonyOS TextInput输入的时候,当文字达到一定数量的时候,自动关闭输入框?
设置TextInput输入字符数量达到设定长度时自动关闭demo
@Entry
@Component
struct FirstTest {
@State message1: string = 'Hello World';
private controller:TextInputController = new TextInputController();
build() {
Column() {
Text("FirstTest:" + this.message1)
.fontSize(25)
.fontWeight(FontWeight.Bold)
TextInput({placeholder:'Waiting for input',text:$$this.message1,controller:this.controller})
.fontSize(25)
.fontWeight(FontWeight.Bold)
.onChange(()=>{
if(this.message1.length >= 20){
this.controller.stopEditing()
}
})
}
}
}
4、HarmonyOS Row 怎么设置中间最大宽度?
Row 怎么设置中间最大宽度
使用constraintSize({maxWidth: Infinity})来对尺寸范围限制
5、HarmonyOS 如何创建单例的WebView组件?
只可以创建单例的对象,示例:
export class GlobalContext {
private constructor() {
}
private static instance: GlobalContext;
private _objects = new Map<string, Object>();
public static getContext(): GlobalContext {
if (!GlobalContext.instance) {
GlobalContext.instance = new GlobalContext();
}
return GlobalContext.instance;
}
getObject(value: string): Object | undefined {
return this._objects.get(value);
}
setObject(key: string, objectClass: Object): void {
this._objects.set(key, objectClass);
}
}