TextPicker
滑動(dòng)選擇文本內(nèi)容的組件。
說(shuō)明:
開發(fā)前請(qǐng)熟悉鴻蒙開發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]
該組件從API Version 8開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
子組件
無(wú)
接口
TextPicker(options?: {range: string[] | string[][] | Resource | TextPickerRangeContent[] | TextCascadePickerRangeContent[], selected?: number, value?: string})
根據(jù)range指定的選擇范圍創(chuàng)建文本選擇器。
參數(shù):
| 參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
|---|---|---|---|
| range | string[] | string[][]10+ | [Resource] |
| selected | number | number[]10+ | 否 |
| value | string | string[]10+ | 否 |
TextPickerRangeContent10+類型說(shuō)明
| 參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
|---|---|---|---|
| icon | string | [Resource] | 是 |
| text | string | [Resource] | 否 |
TextCascadePickerRangeContent10+類型說(shuō)明
| 參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
|---|---|---|---|
| text | string | [Resource] | 是 |
| children | [TextCascadePickerRangeContent][] | 否 | 聯(lián)動(dòng)數(shù)據(jù)。 |
屬性
除支持[通用屬性]外,還支持以下屬性:
| 名稱 | 參數(shù)類型 | 描述 |
|---|---|---|
| defaultPickerItemHeight | number | string |
| disappearTextStyle10+ | [PickerTextStyle] | 設(shè)置所有選項(xiàng)中最上和最下兩個(gè)選項(xiàng)的文本顏色、字號(hào)、字體粗細(xì)。 默認(rèn)值: { color: '#ff182431', font: { size: '14fp', weight: FontWeight.Regular } } |
| textStyle10+ | [PickerTextStyle] | 設(shè)置所有選項(xiàng)中除了最上、最下及選中項(xiàng)以外的文本顏色、字號(hào)、字體粗細(xì)。 默認(rèn)值: { color: '#ff182431', font: { size: '16fp', weight: FontWeight.Regular } } |
| selectedTextStyle10+ | [PickerTextStyle] | 設(shè)置選中項(xiàng)的文本顏色、字號(hào)、字體粗細(xì)。 默認(rèn)值: { color: '#ff007dff', font: { size: '20vp', weight: FontWeight.Medium } } |
| selectedIndex10+ | number | number[] |
| canLoop10+ | boolean | 設(shè)置是否可循環(huán)滾動(dòng),true:可循環(huán),false:不可循環(huán),默認(rèn)值:true。 |
事件
除支持[通用事件]外,還支持以下事件:
| 名稱 | 描述 |
|---|---|
| onChange(callback: (value: string | string[]10+, index: number |
HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿 |

示例
示例1
// xxx.ets
class bottom {
bottom:number = 50
}
let bott:bottom = new bottom()
@Entry
@Component
struct TextPickerExample {
private select: number = 1
private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4']
private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4']
private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4']
private multi: string[][] = [this.apfruits, this.orfruits, this.pefruits]
private cascade: TextCascadePickerRangeContent[] = [
{
text: '遼寧省',
children: [{ text: '沈陽(yáng)市', children: [{ text: '沈河區(qū)' }, { text: '和平區(qū)' }, { text: '渾南區(qū)' }] },
{ text: '大連市', children: [{ text: '中山區(qū)' }, { text: '金州區(qū)' }, { text: '長(zhǎng)海縣' }] }]
},
{
text: '吉林省',
children: [{ text: '長(zhǎng)春市', children: [{ text: '南關(guān)區(qū)' }, { text: '寬城區(qū)' }, { text: '朝陽(yáng)區(qū)' }] },
{ text: '四平市', children: [{ text: '鐵西區(qū)' }, { text: '鐵東區(qū)' }, { text: '梨樹縣' }] }]
},
{
text: '黑龍江省',
children: [{ text: '哈爾濱市', children: [{ text: '道里區(qū)' }, { text: '道外區(qū)' }, { text: '南崗區(qū)' }] },
{ text: '牡丹江市', children: [{ text: '東安區(qū)' }, { text: '西安區(qū)' }, { text: '愛(ài)民區(qū)' }] }]
}
]
build() {
Column() {
TextPicker({ range: this.apfruits, selected: this.select })
.onChange((value: string | string[], index: number | number[]) = > {
console.info('Picker item changed, value: ' + value + ', index: ' + index)
}).margin(bott)
TextPicker({ range: this.multi })
.onChange((value: string | string[], index: number | number[]) = > {
console.info('TextPicker 多列:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
}).margin(bott)
TextPicker({ range: this.cascade })
.onChange((value: string | string[], index: number | number[]) = > {
console.info('TextPicker 多列聯(lián)動(dòng):onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
})
}
}
}
示例2

// xxx.ets
@Entry
@Component
struct TextPickerExample {
private select: number = 1
private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
build() {
Column() {
TextPicker({ range: this.fruits, selected: this.select })
.onChange((value: string | string[], index: number | number[]) = > {
console.info('Picker item changed, value: ' + value + ', index: ' + index)
})
.disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
.textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
.selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
}.width('100%').height('100%')
}
}

審核編輯 黃宇
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。
舉報(bào)投訴
-
組件
+關(guān)注
關(guān)注
1文章
573瀏覽量
19023 -
鴻蒙
+關(guān)注
關(guān)注
60文章
2963瀏覽量
45907
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
熱點(diǎn)推薦
HarmonyOS/OpenHarmony應(yīng)用開發(fā)-ArkTS的聲明式開發(fā)范式
軌跡。狀態(tài)與數(shù)據(jù)管理狀態(tài)數(shù)據(jù)管理作為基于ArkTS的聲明式開發(fā)范式的特色,通過(guò)功能不同的裝飾器給開發(fā)者提供了清晰的頁(yè)面更新渲染流程和管道。狀態(tài)管理包括UI組件狀態(tài)和應(yīng)用程序狀態(tài),兩者協(xié)
發(fā)表于 01-17 15:09
HarmonyOS/OpenHarmony應(yīng)用開發(fā)-聲明式開發(fā)范式組件匯總
組件是構(gòu)建頁(yè)面的核心,每個(gè)組件通過(guò)對(duì)數(shù)據(jù)和方法的簡(jiǎn)單封裝,實(shí)現(xiàn)獨(dú)立的可視、可交互功能單元。組件之間相互獨(dú)立,隨取隨用,也可以在需求相同的地方重復(fù)使用。聲明
發(fā)表于 01-19 11:14
鴻蒙ArkTS聲明式組件:Marquee
跑馬燈組件,用于滾動(dòng)展示一段單行文本。僅當(dāng)文本內(nèi)容寬度超過(guò)跑馬燈組件寬度時(shí)滾動(dòng),不超過(guò)時(shí)不滾動(dòng)。
鴻蒙ArkTS聲明式組件:PatternLock
圖案密碼鎖組件,以九宮格圖案的方式輸入密碼,用于密碼驗(yàn)證場(chǎng)景。手指在PatternLock組件區(qū)域按下時(shí)開始進(jìn)入輸入狀態(tài),手指離開屏幕時(shí)結(jié)束輸入狀態(tài)完成密碼輸入。
鴻蒙ArkTS聲明式組件:TextPicker
評(píng)論