HarmonyOS 鸿蒙Next版本下,如何在ArkUI中实现自定义输入验证提示
HarmonyOS 鸿蒙Next版本下,如何在ArkUI中实现自定义输入验证提示
在Harmony Next版本下,如何在ArkUI中实现自定义输入验证提示?
2 回复
import inputMethod from '@ohos.inputMethod';
import { BusinessError } from '@ohos.base';
@Entry
@Component
export struct InputPinCodePage {
//@Consume('pageInfos') pageInfos: NavPathStack;
@State codeTxt: string = '';
@State screenWidth: number = 0;
private inputController: inputMethod.InputMethodController = inputMethod.getController();
private isAttached: boolean = false;
private keyboardStatus: number = 0; // 默认是none, 1是hide,2是show
aboutToAppear(): void {
console.info('ImsaKit aboutToAppear')
this.screenWidth = 1080;
}
@Builder
componentBuilder() {
Column() {
Blank().height(42)
this.buildTitleName()
Blank().height(38)
this.buildNumTip()
this.buildAEnterCodeInput()
Blank().height(20)
}
.margin({ top: 10 })
.borderRadius(8)
.backgroundColor(Color.White)
}
@Builder
buildTitleName() {
Row() {
Image($r('app.media.icon'))
.objectFit(ImageFit.Contain)
.width(24)
.height(24)
Blank().width(15)
Text() {
Span('输入验证码')
.fontSize(18)
.fontWeight(600)
.fontColor(Color.Black)
}
}.width('100%').justifyContent(FlexAlign.Start).padding({ left: 20, right: 20 })
}
@Builder
buildNumTip() {
Row() {
Text() {
Span('已发送至')
.fontSize(12)
.fontWeight(400)
.fontColor(Color.Grey)
}
Blank().width(5)
Text() {
Span("18204079055")
.fontSize(12)
.fontWeight(500)
.fontColor(Color.Black)
}
}.width('100%').justifyContent(FlexAlign.Start).padding({ left: 20, right: 20 })
}
@Styles
fancyUse(){
.width((this.screenWidth - 110) / 6)
.height("100%")
.margin({ left: 5, right: 5 })
.border({
width: { bottom: 0.5 },
color: { bottom: Color.Grey },
style: { bottom: BorderStyle.Solid }
})
}
@Builder
buildAEnterCodeInput() {
Flex({
direction: FlexDirection.Row,
alignItems: ItemAlign.Center,
justifyContent: FlexAlign.SpaceBetween
}) {
Text(this.codeTxt[0])
.fontSize(18)
.fontWeight(600)
.textAlign(TextAlign.Center)
.fancyUse()
Text(this.codeTxt[1])
.fontSize(18)
.fontWeight(600)
.textAlign(TextAlign.Center)
.fancyUse()
Text(this.codeTxt[2])
.fontSize(18)
.fontWeight(600)
.textAlign(TextAlign.Center)
.fancyUse()
Text(this.codeTxt[3])
.fontSize(18)
.fontWeight(600)
.textAlign(TextAlign.Center)
.fancyUse()
Text(this.codeTxt[4])
.fontSize(18)
.fontWeight(600)
.textAlign(TextAlign.Center)
.fancyUse()
Text(this.codeTxt[5])
.fontSize(18)
.fontWeight(600)
.textAlign(TextAlign.Center)
.fancyUse()
}
.backgroundColor(Color.White)
.height(50)
.margin({ left: 15, right: 15 })
.id("customInput")
.defaultFocus(true)
.onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
console.info('ImsaKit Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
if (isVisible && currentRatio >= 1.0) {
console.info('ImsaKit Test Text is fully visible. currentRatio:' + currentRatio)
this.attachAndListener();
}
if (!isVisible && currentRatio <= 0.0) {
console.info('ImsaKit Test Text is completely invisible.')
this.dettach()
}
})
.onClick(async () => {
console.info('ImsaKit keyboardStatus =' + this.keyboardStatus)
if (this.isAttached && this.keyboardStatus != 2) {
// 输入法配置项
let textConfig: inputMethod.TextConfig = {
inputAttribute: {
textInputType: inputMethod.TextInputType.NUMBER,
enterKeyType: inputMethod.EnterKeyType.GO
}
};
// 控件绑定输入法
await this.inputController.attach(true, textConfig)
return
}
})
}
dettach() {
this.inputController.off('insertText');
this.inputController.off('deleteLeft');
this.inputController.off('sendKeyboardStatus');
this.inputController.detach((err: BusinessError) => {
if (err) {
console.error(`Failed to detach: ${JSON.stringify(err)}`);
return;
}
this.isAttached = false
console.log('Succeeded in detaching inputMethod.');
});
}
// 绑定和设置监听
async attachAndListener() {
// 输入法配置项
let textConfig: inputMethod.TextConfig = {
inputAttribute: {
textInputType: inputMethod.TextInputType.NUMBER,
enterKeyType: inputMethod.EnterKeyType.GO
}
};
// 控件绑定输入法
await this.inputController.attach(true, textConfig)
this.isAttached = true
this.attachListener()
}
/**
* 订阅输入法回调
*/
attachListener(): void {
// 订阅输入法应用插入文本事件
this.inputController.on('insertText', (text) => {
if (this.codeTxt.length >= 6) {
return
}
this.codeTxt += text;
console.info("this.inputText", "insertText this.inputText===" + this.codeTxt)
})
// 订阅输入法应用向左删除事件
this.inputController.on('deleteLeft', (length) => {
this.codeTxt = this.codeTxt.substring(0, this.codeTxt.length - length);
console.info("this.inputText", "deleteLeft this.inputText===" + this.codeTxt)
})
// 订阅输入法应用发送输入法软键盘状态事件
this.inputController.on('sendKeyboardStatus', (keyboardStatus: inputMethod.KeyboardStatus) => {
this.keyboardStatus = keyboardStatus
console.info("ImsaKit this.inputText keyboardStatus= " + this.keyboardStatus)
})
}
build() {
NavDestination() {
Column() {
this.componentBuilder()
}
.width('100%')
.height('100%')
.backgroundColor(Color.White)
.padding({
left: 10,
right: 10,
bottom: 60
})
}
.hideTitleBar(false)
.title("验证码")
.size({ width: '100%', height: '100%' })
.backgroundColor('#FFFFFF')
}
}
更多关于HarmonyOS 鸿蒙Next版本下,如何在ArkUI中实现自定义输入验证提示的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS 鸿蒙Next版本下,使用ArkUI实现自定义输入验证提示,可以通过以下步骤进行:
-
创建输入框组件:在ArkUI的
.ets
文件中,使用<input>
组件来创建输入框。 -
定义验证逻辑:在组件的脚本部分(通常是
.ets
文件的<script>
标签内),定义一个函数用于验证输入内容。该函数可以接收输入框的值作为参数,并返回一个布尔值或包含错误信息的对象。 -
绑定验证逻辑:在输入框的
onInput
或onChange
事件中,绑定验证函数。当输入内容变化时,触发验证逻辑,并根据返回值决定是否显示错误提示。 -
显示验证提示:如果验证失败,可以使用
<text>
组件或其他UI元素来显示错误信息。错误信息的内容可以基于验证函数的返回值动态生成。 -
控制提示显示:通过状态管理(如使用
@state
装饰器)来控制错误提示的显示与隐藏。
示例代码片段(简化):
<template>
<div>
<input @input="validateInput" />
<text if="{{error}}">{{error}}</text>
</div>
</template>
<script>
@state error: string = '';
function validateInput(e: Event) {
const value = (e.target as HTMLInputElement).value;
if (!/^[a-zA-Z0-9]+$/.test(value)) {
this.error = '请输入字母或数字';
} else {
this.error = '';
}
}
</script>
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html