前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学鸿蒙应用开发(13)- ProgressBar

自学鸿蒙应用开发(13)- ProgressBar

作者头像
面向对象思考
发布2021-01-13 11:19:23
5080
发布2021-01-13 11:19:23
举报
文章被收录于专栏:C++核心准则原文翻译

本文介绍在鸿蒙应用中ProgressBar组件的基本用法。

增加ProgressBar组件

如下代码中35行~42行所示,在布局中增加Progress组件。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">
    <Component
        ohos:height="0vp"
        ohos:weight="3"
        ohos:width="match_parent"
        />
    <DirectionalLayout
        xmlns:ohos="http://schemas.huawei.com/res/ohos"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:layout_alignment="center"
        ohos:orientation="vertical">
        <Image
            ohos:id="$+id:image"
            ohos:width="match_content"
            ohos:height="match_content"
            ohos:layout_alignment="center"
            ohos:image_src="$media:DevEco"
        />
        <TimePicker
            ohos:id="$+id:time_picker"
            ohos:24_hour_mode="false"
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:layout_alignment="horizontal_center"
            ohos:text_am="AM"
            ohos:text_pm="PM"
            ohos:normal_text_size="20fp"
            ohos:selected_text_size="25fp"/>
        <ProgressBar
            ohos:id="$+id:second_progress"
            ohos:progress_width="10vp"
            ohos:height="60vp"
            ohos:width="match_parent"
            ohos:max="100"
            ohos:min="0"
            ohos:progress="60"/>
    </DirectionalLayout>
    <Component
        ohos:height="0vp"
        ohos:weight="5"
        ohos:width="match_parent"
        />
</DirectionalLayout>

代码中组件id被指定为second_progress,会在下面的响应代码中用到。

在代码中使用ProgressBar组件

下面代码中的第18行获取ProgressBar组件后,在第19行根据TimePicker的状态更新Progress的形式,然后在第26行TimerPicker的响应处理中,同样是根据TimePicker的状态更新ProgressBar的值。

代码语言:javascript
复制
package com.example.helloharmony.slice;
import com.example.helloharmony.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class ComponentAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_component);
        //获取TimePicker组件
        TimePicker picker = (TimePicker) findComponentById(ResourceTable.Id_time_picker);
        //获取ProgressBar组件
        ProgressBar secondsBar = (ProgressBar)findComponentById(ResourceTable.Id_second_progress);
        updateProgressValue(picker, secondsBar);
        //为TimePicker设定事件响应
        ComponentAbilitySlice this_slice = this;
        picker.setTimeChangedListener(
            new TimePicker.TimeChangedListener() {
                @Override
                public void onTimeChanged(TimePicker timePicker, int hour, int minute, int second) {
                    this_slice.updateProgressValue(timePicker, secondsBar);
                }
            }
        );
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void updateProgressValue(TimePicker time_picker, ProgressBar progress){
        int seconds = (time_picker.getHour() * 60 + time_picker.getMinute()) * 60 + time_picker.getSecond();
        progress.setProgressValue(progress.getMin() + seconds * (progress.getMax() - progress.getMin()) / 86400);
    }
}
代码语言:javascript
复制

参考文档

ProgressBar组件

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-progressbar-0000001060379891

ProgressBar类

https://developer.harmonyos.com/cn/docs/documentation/doc-references/progressbar-0000001054199992

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档