[HarmonyOS]——进度条组件

一、ProgressBar进度条 

进度条组件(ProgressBar,又称直线进度条),在开发中也是一项很重要的组件,常用于显示项目或者任务的进度情况。 

<?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:alignment="center"
    ohos:orientation="vertical">
    <!-- progress:进度条内真正的进度值 -->
    <!-- progress_hint_text:设置进度条内的提示文字 -->
    <!-- progress_hint_text_color: 提示文字的颜色 -->
    <!-- progress_width: 进度条粗细   -->
    <ProgressBar
        ohos:id="$+id:pb"
        ohos:height="match_content"
        ohos:width="300vp"
        ohos:progress="0"
        ohos:progress_hint_text="0%"
        ohos:progress_hint_text_color="#000000"
        ohos:progress_width="50fp"
        ohos:progress_color="#FF0000"
        ohos:max="100"
        ohos:min="0"
        />
</DirectionalLayout>

 1、简单案例

不断的点击进度条组件让其数值在每次点击后 + 5,最后到达100%后停止增加

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1、查找组件
        ProgressBar pb = (ProgressBar) findComponentById(ResourceTable.Id_pb);

        //2、给进度条绑定一个单击事件
        pb.setClickedListener(this);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        //每点击一下进度条进度增加5%
        ProgressBar temp_pb = (ProgressBar)component;
        //获取进度条里面原本的值
        int progress = temp_pb.getProgress();
        //将进度条内的值 + 5
        progress += 5;
        if(progress <= temp_pb.getMax()) {
            //再给进度条设置进度
            temp_pb.setProgressValue(progress);
            //再修改上面的提示文字
            temp_pb.setProgressHintText(progress + "%");
        }
    }
}

 二、RoundProgressBar进度条(圆形进度条)

RoundProgressBar是ProgressBar进度条的子类,和普通进度条的用法很类似

<?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:alignment="center"
    ohos:orientation="vertical">
    <!-- progress:进度条内真正的进度值 -->
    <!-- progress_hint_text:设置进度条内的提示文字 -->
    <!-- progress_hint_text_color: 提示文字的颜色 -->
    <!-- progress_width: 进度条粗细   -->
    <RoundProgressBar
        ohos:id="$+id:rpb"
        ohos:height="300vp"
        ohos:width="300vp"
        ohos:progress="80"
        ohos:progress_hint_text="80%"
        ohos:progress_hint_text_size="50fp"
        ohos:progress_hint_text_color="#000000"
        ohos:progress_width="20vp"
        ohos:progress_color="#FF0000"
        ohos:max="100"
        ohos:min="0"
        />
</DirectionalLayout>

1、简单案例

 不断的点击进度条组件让其数值在每次点击后 + 5,最后到达100%后停止增加

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1、找到组件
        RoundProgressBar rpb = (RoundProgressBar) findComponentById(ResourceTable.Id_rpb);

        //2、组件绑定点击事件
        rpb.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        //参数强转获得组件
        RoundProgressBar temp_rpb = (RoundProgressBar) component;
        //获取组件的原本的进度值
        int progress = temp_rpb.getProgress();
        //将进度值 + 5
        progress += 5;
        //判断进度值是否小于等于进度设置的最大值
        if(progress <= temp_rpb.getMax()) {
            //为进度赋新值
            temp_rpb.setProgressValue(progress);
            //修改进度提示文字
            temp_rpb.setProgressHintText(progress + "%");
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star星屹程序设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值