起因:手賤造輪子的老毛病又犯了
基于:鴻蒙SDK 版本3.1.0 API9 stage模型
下載鏈接:https://gitee.com/kaiyuanguo/harmony_-demo/tree/master/
使用說明↓
/*
// 1.簡約方式(只支持傳入顯示類型 + 提示語) ↓
this.hudItem.showHud(BDHudType.loading)
setTimeout(() => { // 最好弄個(gè) 實(shí)例對象控制下 此定時(shí)器完成任務(wù)后 進(jìn)行銷毀,這就不寫了
this.hudItem.close()
},2000)
this.hudItem.showHud(BDHudType.loading,'我正給嫩加載著嘞') // 自定義提示語
// 2.鏈?zhǔn)椒绞剑ㄈδ?+ 方便擴(kuò)展) ↓
this.hudItem.showHudChian()
.setDisplayType(BDHudType.success)
.setDismisTime(2000)
.setMsgStr('提示語')
.setIconSrc('/components/BDHud/testSrc.gif')
*/
/*
* hudItem.showHud(BDHudType.loading)
* 或者自定義提示語 hudItem.showHud(BDHudType.loading,'我正給嫩加載著嘞')
* success提示 和error提示也一樣這樣傳入自定義提示語
*
* success 和 error 默認(rèn)會(huì)自動(dòng)消失 默認(rèn)2s后 自動(dòng)消失
* loading 默認(rèn)不自動(dòng)消失 需要用戶在外部手動(dòng)控制 hudItem.close()
*
* */
代碼實(shí)現(xiàn)↓
export enum BDHudType{
msg, //單提示語
loading, //加載框
success, //成功提示
error //錯(cuò)誤提示
}
export class BDHudClass {
public show: boolean; //是否顯示
public displayType: BDHudType; //顯示類型
public msgStr: string = ''; //提示語
public iconSrc:string = '';
public hudStatus:boolean = false;
public showHud(displayType: BDHudType,msgStr?: string){
this.hudStatus = true;
this.show = true;
this.displayType = displayType;
if(displayType == BDHudType.loading){
this.iconSrc = '/components/BDHud/loading_hud.gif'
this.msgStr = msgStr == undefined ? '加載中':msgStr;
}
if(displayType == BDHudType.success){
this.iconSrc = '/components/BDHud/success_hud.png'
this.msgStr = msgStr == undefined ? '成功':msgStr;
}
if(displayType == BDHudType.error){
this.iconSrc = '/components/BDHud/error_hud.png'
this.msgStr = msgStr == undefined ? '錯(cuò)誤/失敗':msgStr;
}
if(displayType == BDHudType.msg){
this.iconSrc = ''
this.msgStr = msgStr == undefined ? '提示語':msgStr;
}
}
// 外部調(diào)用手動(dòng)銷毀
public close(){
this.hudStatus = false;
this.show = false;
}
// 構(gòu)造函數(shù)
constructor(show:boolean = false, displayType:BDHudType = BDHudType.msg,msgStr: string = '',iconSrc:string = '') {
this.show = show;
this.displayType = displayType;
this.msgStr = msgStr;
this.iconSrc = iconSrc;
}
}
@Component
export default struct BDHud{
@Link @Watch('itemChange') hudItem:BDHudClass
@State ZIndex:number = -1
@State boxShow:boolean = false
@State iconShow:boolean = false
@State textShow:boolean = false
private timeoutID:any
itemChange(){
clearTimeout(this.timeoutID);
this.boxShow = this.hudItem.show
this.iconShow = this.hudItem.iconSrc.length == 0 ? false:true;
this.textShow = this.hudItem.msgStr.length == 0 ? false:true;
if(this.boxShow == true){
this.ZIndex = 999
}else {
this.ZIndex = -1;
}
if(this.hudItem.displayType == BDHudType.success || this.hudItem.displayType == BDHudType.error || this.hudItem.displayType == BDHudType.msg){
var that = this
clearTimeout(this.timeoutID);
this.timeoutID = setTimeout(() => {
that.boxShow = false
that.hudItem.show = false
that.hudItem.hudStatus = false;
},2000)
}
}
build(){
Stack(){
Column(){
Image(this.hudItem.iconSrc)
.iconImg()
.visibility(this.iconShow ? Visibility.Visible : Visibility.None)
Text(this.hudItem.msgStr)
.lineHeight(28)
.padding({top:this.iconShow ? 15:0})
.msgText()
.visibility(this.textShow ? Visibility.Visible : Visibility.None)
}
.padding({top:this.iconShow ? 18:10,bottom:15,left:15,right:15})
.box()
}
.zIndex(this.ZIndex)
.visibility(this.boxShow == true ? Visibility.Visible:Visibility.None)
}
}
@Extend(Text) function msgText(){
.fontColor(Color.White)
.fontSize(15)
}
@Extend(Image) function iconImg(){
.width(60)
.height(60)
}
@Extend(Column) function box() {
.constraintSize({minWidth:135,maxWidth:300})
.backgroundColor('rgba(0,0,0,0.5)')
.borderRadius(5)
}