自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenHarmony數(shù)據(jù)轉碼應用開發(fā)實戰(zhàn)(中)

系統(tǒng) OpenHarmony
本文將以一個小項目—數(shù)據(jù)轉碼應用,來講解應用開發(fā)全流程。

??想了解更多關于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎軟件社區(qū)??

??https://ost.51cto.com??

背景

對于剛入門OpenHarmony開發(fā)的小伙伴來說,如果有一個合適的實戰(zhàn)項目來練手,對自身的技術能力提升是非常有幫助的,本文將以一個小項目——數(shù)據(jù)轉碼應用,來講解應用開發(fā)全流程。
在《OpenHarmony數(shù)據(jù)轉碼應用開發(fā)實戰(zhàn)(上)》中我們講述了項目的需求、設計以及項目創(chuàng)建、UI界面開發(fā),本篇將重點講解轉碼工具包的實現(xiàn)和UI組件數(shù)據(jù)綁定。

轉碼工具包

編碼時推薦單獨創(chuàng)建包路徑,不要與頁面UI寫在一起,這樣便于維護和代碼的復用。
我們創(chuàng)建/entry/src/main/ets/MainAbility/utils/numConvertUtil.ets,然后在index.ets頁面中引入。工具包將導出一個工具對象,每個方法實現(xiàn)一個轉碼需求,代碼如下:

export default {
/**
* 10進制轉16進制,并補零
* @param num
* @param len = 2
*/
dec2hex: function (numStr: string, len: number = 2) {
console.log(JS_TAG + 'dec2hex ' + numStr)
let result: string = Number(numStr).toString(16).toUpperCase()
result = this.addZero(result, len)
return result
},
/**
* 16進制轉10進制
* @param num
*/
hex2dex: function (numStr: string) {
console.log(JS_TAG + 'hex2dex ' + numStr)
return parseInt(numStr, 16).toString()
},
/**
* 16進制轉2進制
* @param num
* @param len
*/
hex2bin: function (numStr: string, len: number = 2) {
let hexNum: number = parseInt(numStr, 16)
let result: string = Number(hexNum).toString(2)
result = this.addZero(result, len)
return result
},
/**
* 2進制轉16進制
* @param num
* @param len
*/
bin2hex: function (numStr: string) {
let num: number = parseInt(numStr, 2)
let result: string = Number(num).toString(16)
result = this.addZero(result)
return result
},
/**
* 16進制轉ASCII碼
* @param hexCharCodeStr
*/
hex2ascii: function (hexStr: string) {
const tempStr: string = hexStr.trim()
const rawStr: string = tempStr.substr(0, 2).toLowerCase() === '0x' ? tempStr.substr(2) : tempStr
const len: number = rawStr.length
if (len % 2 !== 0) {
return ''
}
let curCharCode
const resultStr = []
for (let i = 0; i < len; i = i + 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16)
resultStr.push(String.fromCharCode(curCharCode))
}
return resultStr.join('')
},
/**
* ASCII碼轉16進制
* @param str
*/
ascii2hex: function (asciiStr: string) {
if (asciiStr === '') {
return ''
} else {
const hexCharCode = []
hexCharCode.push('0x')
for (let i = 0; i < asciiStr.length; i++) {
hexCharCode.push((asciiStr.charCodeAt(i)).toString(16))
}
return hexCharCode.join('')
}
},
addZero: function (numStr: string, len: number = 2) {
const needFill: number = len - numStr.length
let result: string = numStr
if (needFill > 0) {
for (let i = 0; i < needFill; i++) {
result = '0' + result
}
}
return result
}
}

綁定UI組件的事件輸出結果

引入數(shù)據(jù)轉碼工具包

import numConvertUtil from '../utils/numConvertUtil';

綁定按鈕Click事件

Button($r('app.string.btnDec2hex'), { type: ButtonType.Normal })
.width('50%')
.onClick(() => {
this.dec2hex()
})

Textarea數(shù)據(jù)改變事件是onChange,它無法像VUE組件一樣直接通過value綁定獲取,只能通過onChange事件獲取改變后的值。

TextArea()
.width('100%')
.height(180)
.backgroundColor(0x0ffff)
.borderRadius(0)
.onChange((value) => {
this.strInput = value
console.log(this.strInput)
})

Click事件直接調(diào)用工具包

dec2hex() {
this.strEncode = ''
console.log(JS_TAG + this.strInput)
this.strEncode = numConvertUtil.dec2hex(this.strInput)
console.log(JS_TAG + this.strInput + ' ' + this.strEncode)
}
hex2dex() {
this.strEncode = ''
this.strEncode = numConvertUtil.hex2dex(this.strInput)
console.log(JS_TAG + this.strInput + ' ' + this.strEncode)
}
hex2bin() {
this.strEncode = ''
this.strEncode = numConvertUtil.hex2bin(this.strInput)
console.log(JS_TAG + this.strInput + ' ' + this.strEncode)
}
bin2hex() {
this.strEncode = ''
this.strEncode = numConvertUtil.bin2hex(this.strInput)
console.log(JS_TAG + this.strInput + ' ' + this.strEncode)
}
hex2ascii() {
this.strEncode = ''
this.strEncode = numConvertUtil.hex2ascii(this.strInput)
console.log(JS_TAG + this.strInput + ' ' + this.strEncode)
}
ascii2hex() {
this.strEncode = ''
this.strEncode = numConvertUtil.ascii2hex(this.strInput)
console.log(JS_TAG + this.strInput + ' ' + this.strEncode)
}

總結

在編碼過程中我們要提前規(guī)劃好公用方法,這樣即降低了維護成本,又能做到代碼復用。eTS的組件事件與VUE框架大體相同,但也有略微的差異,比如Textarea的值綁定是通過onChange事件來獲取的,在不確定時定可以多看官方組件文檔。

??想了解更多關于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎軟件社區(qū)??

??https://ost.51cto.com??

責任編輯:jianghua 來源: 51CTO開源基礎軟件社區(qū)
相關推薦

2022-11-02 15:49:45

應用開發(fā)鴻蒙

2022-11-11 09:37:58

數(shù)據(jù)轉碼應用開發(fā)

2022-03-02 16:08:31

Harmony應用開發(fā)鴻蒙

2022-11-04 14:58:59

應用開發(fā)鴻蒙

2022-10-08 16:19:40

智能喂食器鴻蒙

2023-08-17 15:04:22

2022-10-08 16:26:23

APP應用開發(fā)

2022-02-17 18:08:04

OpenHarmon應用開發(fā)鴻蒙

2023-04-07 09:20:55

2023-03-09 15:10:49

應用開發(fā)鴻蒙

2022-02-15 14:06:36

OpenHarmon操作系統(tǒng)鴻蒙

2022-02-24 16:39:41

OpenHarmonNiobe開發(fā)鴻蒙

2023-07-31 17:35:31

ArkTS鴻蒙

2024-07-26 16:39:33

鴻蒙系統(tǒng)開源構建系統(tǒng)

2023-08-10 17:14:52

鴻蒙自定義彈窗

2023-05-16 14:45:42

應用開發(fā)鴻蒙

2014-12-17 10:29:59

混合應用Hybrid App開發(fā)實戰(zhàn)

2022-04-18 10:37:01

鴻蒙操作系統(tǒng)開發(fā)工具

2024-08-08 15:46:34

2022-02-15 14:45:14

OpenHarmo系統(tǒng)鴻蒙
點贊
收藏

51CTO技術棧公眾號