背景:
前几篇学习了元服务,后面几期就让我们开发简单的元服务吧,里面丰富的内容大家自己加,本期案例 仅供参考
先上本期效果图 ,里面图片自行替换
效果图1完整代码案例如下:
- Index
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { TypeCommonItem } from './TypeCommonItem';@Entry
@ComponentV2
struct Index {build() {Column() {Text($r('app.string.EntryAbility_label')).fontSize(20)List() {ForEach(['青春跃动:校园时光的旋律', '心灵治愈:温暖旋律抚心伤', '动感节奏:舞池狂欢不停歇','经典怀旧:岁月留声机里的故事'], (item: string) => {ListItem() {TypeCommonItem({ title: item })}})}}.alignItems(HorizontalAlign.Start).height('100%').width('100%').margin({ top: 40 })}aboutToAppear() {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');this.loginWithHuaweiID();}/*** Sample code for using HUAWEI ID to log in to atomic service.* According to the Atomic Service Review Guide, when a atomic service has an account system,* the option to log in with a HUAWEI ID must be provided.* The following presets the atomic service to use the HUAWEI ID silent login function.* To enable the atomic service to log in successfully using the HUAWEI ID, please refer* to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.*/private loginWithHuaweiID() {// Create a login request and set parameterslet loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();// Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI IDloginRequest.forceLogin = false;// Execute login requestlet controller = new authentication.AuthenticationController();controller.executeRequest(loginRequest).then((data) => {let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;// Send authCode to the backend in exchange for unionID, session}).catch((error: BusinessError) => {hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {// HUAWEI ID is not logged in, it is recommended to jump to the login guide page}});}
}
- TypeCommonItem
import { ImageTextComp } from "./ImageTextComp"@Extend(Image)
function imageExtend(width: Length, height: Length) {.width(width).height(height)
}@Preview
@ComponentV2
export struct TypeCommonItem {@Param title: string = ''build() {Column() {Row() {// CheckBoxSquare({// select: true, onChange: (value) => {// ToastUtil.showToast(`${value}`)// }// })Column() {Row() {ImageTextComp({ title: this.title }).layoutWeight(1)Stack() {Image('').imageExtend(96, 64).borderRadius(4).backgroundColor("#DADCDF").margin({ right: 18, top: 4 })Image($r('app.media.startIcon')).imageExtend(108, 72).borderRadius(4).backgroundColor('#ccc').margin({ left: 12 })Image($r('app.media.startIcon')).imageExtend(21, 19)Image($r('app.media.startIcon')).imageExtend(16, 16).backgroundColor("#5090F1").borderRadius(4).padding(1).position({ x: 100, y: 54 })}.height(72).alignContent(Alignment.TopEnd)}.width('100%').alignItems(VerticalAlign.Top)Text(this.getjieshao(this.title)).width('100%').fontSize(13).fontColor('#505050').margin({ top: 8 }).maxLines(2).textOverflow({ overflow: TextOverflow.Ellipsis })Row() {Text('12:35').fontSize(11).fontColor('#505050')Text('华为音乐').fontSize(11).fontColor("#5090F1").margin({ left: 12, right: 12 })Image($r('app.media.startIcon')).width(16).borderRadius(8)Text('微博大V').fontSize(11).fontColor('#505050').margin({ left: 4, right: 4 })Image($r('app.media.startIcon')).imageExtend(44, 17)Blank()Image($r('app.media.ic_item_more')).width(16)}.width('100%').height(40)}.width('100%').layoutWeight(1)}.alignItems(VerticalAlign.Top).margin({ top: 12 })Divider()}.width('100%').padding({ left: 12, right: 12 })// .height('100%').backgroundColor(Color.White)}private getjieshao(title: string) {if (title == "青春跃动:校园时光的旋律") {return '这个歌单宛如一部青春纪念册,收录了诸多陪伴我们度过校园岁月的歌曲。从青涩懵懂的初恋情怀,到与好友在操场追逐嬉戏的欢乐时刻,每一首歌都承载着那些无忧无虑的日子。轻快的旋律如阳光洒在校园小径,带您重温课间十分钟的喧闹、运动会上的激情呐喊,以及教室里的朗朗书声,让您在音符中找回那份纯真与活力。'}if (title == "心灵治愈:温暖旋律抚心伤") {return '介绍:在生活的喧嚣与疲惫中,我们都需要一个角落来安放内心的疲惫。此歌单精选了一系列具有治愈力量的歌曲,如同冬日里的暖阳,轻柔地洒在心田。这些歌曲的歌词富有深意,旋律舒缓动人,能在您失落、迷茫或焦虑时,给予慰藉与陪伴。它们像是无声的朋友,用音乐的语言为您排忧解难,助您重拾勇气与希望,再次拥抱生活的美好。'}if (title == "动感节奏:舞池狂欢不停歇") {return '介绍:准备好释放您的激情了吗?这个歌单是为热爱舞蹈与动感节奏的您量身打造。汇聚了流行、电子、嘻哈等多种风格的嗨曲,强烈的鼓点和富有感染力的旋律,仿佛是舞池的心跳。无论是在夜店尽情摇摆,还是在家中独自释放压力,这些歌曲都能瞬间点燃您的热情,让您的身体不由自主地跟随节奏律动,沉浸在无尽的欢乐与活力之中,忘却一切烦恼与束缚。'}if (title == "经典怀旧:岁月留声机里的故事") {return '介绍:时光流转,经典永恒。本歌单带您穿越回往昔岁月,重温那些经受过时间考验的经典之作。从上个世纪的摇滚传奇到深情款款的华语老歌,每一首都承载着一代人的回忆与情感。这些歌曲见证了音乐发展的历程,它们的旋律和歌词中蕴含着深深的时代烙印,让人在聆听中不禁感叹岁月的变迁,同时也为那些不朽的音乐艺术所折服,是一场不容错过的怀旧音乐之旅。'}return ''}
}
- ImageTextComp
/** 公共图片+文字 */
@Preview
@ComponentV2
export struct ImageTextComp {@Param title: string = ''build() {Row() {Text() {ImgSpan()Span(this.title).fontColor('#222222').fontSize(16)}.width('100%').maxLines(2).textOverflow({ overflow: TextOverflow.Ellipsis })}}
}/** 图片+文字 */
@Builder
export function ImageTextShortVideo() {Text() {ImgSpan()Span('xxxxxxxxxxxxx').fontColor('#222222').fontSize(14).fontWeight(FontWeight.Bold)}.width('100%').maxLines(2).lineHeight(20).textOverflow({ overflow: TextOverflow.Ellipsis }).padding({ left: 8, top: 8, right: 8 }).alignRules({ top: { anchor: 'bg_img_single_top', align: VerticalAlign.Bottom } })
}@Builder
function ImgSpan() {ImageSpan($r('app.media.startIcon')).Image()ImageSpan($r('app.media.startIcon')).Image()
}@Extend(ImageSpan)
function Image(event: () => void = () => {
}) {.height(16).verticalAlign(ImageSpanAlignment.CENTER).onClick(event).padding({ right: 4 })
}
最近文章>>>>>>>>>>>
HarmonyOS NEXT实战:元服务与应用 APP 发布应用市场的详细步骤与流程
若本文对您稍有帮助,诚望您不吝点赞,多谢。
有兴趣的同学可以点击查看源码
- gitee:https://gitee.com/jiaojiaoone/explore-harmony-next/tree/case%2Fwanandroid/
- github:https://github.com/JasonYinH/ExploreHarmonyNext.git