当前位置: 首页 > news >正文

鸿蒙的@State

鸿蒙是xml和逻辑代码,用@State实则是更新内容

@State实则是刷新页面的内容

猜数字游戏源码:
@Entry
@Component
struct GuessNumberGame {private answer: number = Math.floor(Math.random() * 100) + 1private input: string = ''@State private hint: string = '请输入一个 1~100 的数字'@State private count: number = 0build() {Column({ space: 20 }) {Text('🎯 猜数字游戏').fontSize(24).fontWeight(FontWeight.Bold)TextInput({placeholder: '输入你的猜测',text: this.input,}).onChange((value: string) => {this.input = value}).width(250).height(40).backgroundColor('#F5F5F5').borderRadius(8).padding(10)Button('提交').onClick(() => {this.checkGuess()}).backgroundColor('#007DFF').fontColor(Color.White).borderRadius(8).width(120).height(40)Text(`提示:${this.hint}`).fontSize(18).fontColor('#333')Text(`你已经猜了 ${this.count} 次`).fontSize(16).fontColor('#999')}.padding(20).align(Alignment.Center)}private checkGuess() {const guess = Number(this.input)if (isNaN(guess) || guess < 1 || guess > 100) {this.hint = '请输入 1 到 100 之间的数字'return}this.count++if (guess < this.answer) {this.hint = '太小了!'} else if (guess > this.answer) {this.hint = '太大了!'} else {this.hint = `🎉 恭喜你猜对了!共用了 ${this.count} 次`}}
}
效果:

http://www.xdnf.cn/news/1029223.html

相关文章:

  • 跳出多重循环
  • Power BI Streaming dataset - 模拟监测水库的水位情况
  • 【2025 CVPR-Backbone】Building Vision Models upon Heat Conduction
  • 57、原生组件注入-【源码分析】DispatcherServlet注入原理
  • Windows系统 整个D盘均无法新建文件夹,D盘权限无法直接添加
  • 认识LinkedHashMap
  • catelen数到二叉树节点的联想
  • C语言:字符函数
  • 高低温介电温谱测量系统在实际应用中有哪些具体的挑战?
  • 体系结构论文(八十六):The Dark Side ofComputing: SilentData Corruptions
  • 爱玛乐维CA510至臻版发布,品质跃迁塑造休三天花板
  • 【论文写作参考文献地址】
  • SSH远程连接到Windows服务器
  • 【树合集】
  • 纯免费的零基础建站教程
  • 从Seq2Seq到QKV:深度解析注意力机制的核心逻辑
  • Python|GIF 解析与构建(6):手搓 tk 录制工具
  • 【互联网基础】互联网公司机房怎么设计
  • Python训练营-Day30-模块和库的导入
  • EDW2025|从传统BI到AI Ready:企业数据与分析能力的实施策略演进
  • Java 锁升级机制详解
  • 芯片测试之VIL/VIH(输入电平)Test全解析:从原理到实战
  • 高通IPA硬件加速介绍
  • 03 | 大模型微调 | 从0学习到实战微调 | 玩转Hugging Face与Transformers框架
  • manpath: can‘t set the locale; make sure $LC_* and $LANG are correct
  • 设备管理-Udev(一)
  • E10集成登录三方系统
  • Python基础补漏
  • ESP32服务器端编码
  • SAM分割一切-使用SAM自动生成对象掩码示例