/** * 二维码 */ Entry Component struct QrcodeTest { /*创建变量用来设置二维码的宽度*/ State qrcodeWidth: number 200 /*创建变量用来设置二维码的颜色*/ State qrcodeColor: Color Color.Black /*创建变量用来设置二维码生成的内容*/ State qrcodeContent: string /*创建变量用来设置二维码的最小值宽度*/ State qrcodeMinWidth: number 50 /*创建变量用来设置二维码的最大值宽度*/ State qrcodeMaxWidth: number 300 /*构造界面*/ build() { /*添加列*/ Column(){ /*添加Stack布局*/ Stack(){ /*添加二维码*/ QRCode(this.qrcodeContent) .width(this.qrcodeWidth)/*设置二维码宽度*/ .height(this.qrcodeWidth)/*设置二维码高度*/ .color(this.qrcodeColor) /*添加logo图片*/ Image($r(app.media.startIcon)) .width(this.qrcodeWidth/5)/*设置logo图片宽度*/ }/*添加Stack布局*/ .width(100%) .height(50%) /*添加行 用来设置二维码的大小*/ Row({space: 5}){ /*添加文本*/ Text(大小: ) .fontSize(25) /*添加滑块组件*/ Slider({ min: 0,/*最小值*/ max: 100,/*最大值*/ step: 1,/*步进值*/ value: (this.qrcodeWidth - this.qrcodeMinWidth)/(this.qrcodeMaxWidth - this.qrcodeMinWidth)*100/*默认值*/ })/*设置滑块组件*/ .width(50%)/*设置滑块宽度*/ .onChange((value: number) {/*滑块值改变时*/ /*设置二维码宽度*/ this.qrcodeWidth this.qrcodeMinWidth (this.qrcodeMaxWidth - this.qrcodeMinWidth)*value/100 }) } /*添加行 用来设置二维码的颜色*/ Row({space: 10}){ /*添加文本*/ Text(颜色: ) .fontSize(25) /*添加行 用来设置二维码的颜色*/ this.createColorButton(Color.Red)/*添加红色按钮*/ this.createColorButton(Color.Green)/*添加绿色按钮*/ this.createColorButton(Color.Blue)/*添加蓝色按钮*/ this.createColorButton(Color.Yellow)/*添加黄色按钮*/ this.createColorButton(Color.Black)/* 添加黑色按钮*/ } /*添加列 用来生成二维码的内容*/ Column({space: 10}){ /*添加文本*/ Text(输入二维码的生成内容: ) .fontSize(25) .fontWeight(FontWeight.Bold) .height(40) /*添加文本域*/ TextArea({placeholder: 请输入......}) .width(70%)/*设置文本域宽度*/ .height(180)/*设置文本域高度*/ .foregroundColor(Color.Black) .backgroundColor(Color.Gray) .onChange((value){ this.qrcodeContent value/*设置二维码内容*/ }) } }/*添加列布局*/ .width(100%) .height(100%) } /** * 创建函数用来生成颜色按钮 */ Builder createColorButton(color: Color) { /*添加按钮*/ Button() .width(30) .height(40) .borderRadius(25) .backgroundColor(color)/*设置按钮颜色*/ .onClick(() { /*点击按钮时*/ this.qrcodeColor color }) } }