HarmonyOS鸿蒙Next中如何实现答题结果页面?

HarmonyOS鸿蒙Next中如何实现答题结果页面? 我需要创建一个答题结束后的结果展示页面,显示得分、正确率、用时和操作按钮,如何实现?

3 回复

实现思路:

  1. 接收答题数据,计算得分和正确率:
@State correctCount: number = 8;
@State totalCount: number = 10;

get score(): number {
  return Math.round(this.correctCount / this.totalCount * 100);
}
  1. 根据得分显示不同的结果图标和文案:
Text(this.score >= 60 ? '🎉' : '📚').fontSize(80)
Text(this.score >= 60 ? '恭喜通过!' : '继续加油!').fontSize(24)
  1. 格式化显示用时:
formatTime(seconds: number): string {
  const mins = Math.floor(seconds / 60);
  const secs = seconds % 60;
  return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
  1. 完整示例代码:
import router from '@ohos.router';

@Entry
@Component
struct QuizResultPage {
  @State correctCount: number = 8;
  @State totalCount: number = 10;
  @State quizTime: number = 180;

  get score(): number {
    return Math.round(this.correctCount / this.totalCount * 100);
  }

  formatTime(seconds: number): string {
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  }

  build() {
    Column() {
      Blank().layoutWeight(1)

      Text(this.score >= 60 ? '🎉' : '📚').fontSize(80).margin({ bottom: 16 })
      Text(this.score >= 60 ? '恭喜通过!' : '继续加油!').fontSize(24).fontWeight(FontWeight.Bold)
      Text(`${this.score}分`).fontSize(48).fontWeight(FontWeight.Bold).fontColor(this.score >= 60 ? '#4CAF50' : '#FF9800')
      Text(`答对 ${this.correctCount} 题,共 ${this.totalCount} 题`).fontSize(14).fontColor('#999999')
      Text(`用时:${this.formatTime(this.quizTime)}`).fontSize(13).fontColor('#CCCCCC')

      Blank().layoutWeight(1)

      Column({ space: 12 }) {
        Button('再测一次').fontSize(16).backgroundColor('#1976D2').borderRadius(24).width('80%').height(48)
          .onClick(() => router.back())
        Button('返回首页').fontSize(14).fontColor('#666666').backgroundColor('#F5F5F5').borderRadius(24).width('80%').height(44)
          .onClick(() => router.back())
      }
      .padding({ bottom: 32 })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.White)
    .justifyContent(FlexAlign.Center)
  }
}

更多关于HarmonyOS鸿蒙Next中如何实现答题结果页面?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中实现答题结果页面,需使用ArkTS声明式UI开发。通过状态管理(如@State)绑定答题数据,页面布局使用Column、Row、Text等组件展示分数、正确题数等信息。可结合ForEach循环渲染每道题的答案对比。样式通过通用属性或自定义样式设置。

在HarmonyOS Next中实现答题结果页面,推荐使用ArkUI声明式开发范式。以下是核心实现思路和关键代码示例:

1. 页面结构与数据

// 数据模型
@Observed
class ResultState {
  score: number = 85
  totalQuestions: number = 20
  correctCount: number = 17
  timeUsed: number = 120 // 秒
}

// 页面组件
@Component
struct ResultPage {
  @State result: ResultState = new ResultState()
  
  build() {
    Column({ space: 20 }) {
      // 得分展示
      Text(`得分:${this.result.score}`)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
      
      // 正确率计算
      Text(`正确率:${(this.result.correctCount/this.result.totalQuestions*100).toFixed(1)}%`)
        .fontSize(24)
      
      // 用时转换
      Text(`用时:${Math.floor(this.result.timeUsed/60)}分${this.result.timeUsed%60}秒`)
        .fontSize(24)
      
      // 操作按钮区域
      Row({ space: 15 }) {
        Button('查看解析')
          .onClick(() => this.showAnalysis())
        
        Button('重新开始')
          .type(ButtonType.Normal)
          .onClick(() => this.restartQuiz())
        
        Button('返回首页')
          .type(ButtonType.Normal)
          .onClick(() => this.goHome())
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .margin({ top: 40 })
    }
    .padding(20)
    .width('100%')
    .height('100%')
  }
}

2. 关键功能实现

  • 数据传递:使用路由参数或AppStorage传递答题结果数据
// 跳转到结果页时传递参数
router.pushUrl({
  url: 'pages/ResultPage',
  params: { 
    score: currentScore,
    timeUsed: timeSpent 
  }
})
  • 进度环可视化(可选):
// 使用Canvas绘制正确率环形图
Canvas(this.context)
  .onReady(() => {
    // 绘制环形进度条
    // 计算弧度:correctRatio * 2 * Math.PI
  })

3. 布局优化建议

  • 使用Flex布局确保各元素居中显示
  • 通过@Styles装饰器统一文本样式
  • 为按钮添加交互动效:
Button('查看解析')
  .stateEffect(true) // 启用按压态效果
  .animation({ duration: 100 }) // 微交互动画

4. 注意事项

  • 涉及耗时操作(如数据统计)建议使用异步任务
  • 多设备适配使用响应式布局和资源限定词
  • 按钮点击事件通过自定义组件事件或页面路由实现跳转逻辑

此方案采用标准ArkUI组件,符合HarmonyOS Next开发规范,可直接在Stage模型项目中应用。

回到顶部