楼主你好
import { inputMethod } from '@kit.IMEKit'
import { BusinessError } from '@kit.BasicServicesKit'
@CustomDialog
export struct TestDialogController {
private controller: CustomDialogController
build() {
Text('测试信息')
}
}
@Entry
@Component
struct TextInputTest1 {
@State text: string = ''
@State positionInfo: CaretOffset = { index: 0, x: 0, y: 0 }
@State passwordState: boolean = false
@State flag: boolean = false
// controller: TextInputController = new TextInputController()
private inputController: inputMethod.InputMethodController = inputMethod.getController();
@State keyboardStatus: number = 0; // 默认是none, 1是hide,2是show
@State codeTxt: string = '';
async detach() {
await this.inputController.detach();
console.log('Succeeded in detaching inputMethod.');
}
async aboutToAppear(): Promise<void> {
await this.attachAndListener();
}
build() {
Column() {
Button('弹窗显示2秒后关闭').onClick((event: ClickEvent) => {
const dialog = new CustomDialogController({
builder: TestDialogController(),
customStyle: true,
autoCancel: false,
isModal: true,
onWillDismiss: () => {
}
})
dialog.open()
setTimeout(() => {
dialog.close()
}, 2000)
})
TextInput({ text: this.codeTxt, placeholder: '内容' })
.width(300)
.focusable(false)
.onClick(async () => {
this.inputController.showTextInput((err: BusinessError) => {
if (err) {
console.error(`Failed to showTextInput: ${JSON.stringify(err)}`);
return;
}
console.log('Succeeded in showing the inputMethod.');
});
})
}
}
// 绑定和设置监听
async attachAndListener() {
// 输入法配置项
let textConfig: inputMethod.TextConfig = {
inputAttribute: {
textInputType: inputMethod.TextInputType.TEXT,
enterKeyType: inputMethod.EnterKeyType.GO
}
};
// 控件绑定输入法
await this.inputController.attach(false, textConfig)
// this.isAttached = true
this.attachListener()
}
/*
* 订阅输入法回调
*/
attachListener(): void {
this.inputController.on('insertText', (text) => {
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 - 1);
console.info('this.inputText', 'deleteLeft this.inputText===', this.codeTxt, 'length' + length)
})
// 订阅输入法应用发送输入法软键盘状态事件
this.inputController.on('sendKeyboardStatus', async (keyboardStatus: inputMethod.KeyboardStatus) => {
this.keyboardStatus = keyboardStatus
console.info('IsaKit this.inputText keyboardStatus= ', this.keyboardStatus)
})
}
}