其他分享
首页 > 其他分享> > HarmonyOS实战——ProgressBar进度条组件基本使用

HarmonyOS实战——ProgressBar进度条组件基本使用

作者:互联网

目录

1. ProgressBar进度条组件

基本用法:

	<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="50vp" ohos:progress_color="red" ohos:max="100" ohos:min="0">

2. ProgressBar案例——点击进度条增加实际进度值

需求分析:

  1. 每单击一次进度条组件时,进度条就加 5% 的进度
  2. 给进度条组件绑定一个单击事件

ability_main

<!--?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">

    <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="50vp" ohos:progress_color="red" ohos:max="100" ohos:min="0">

</progressbar></directionallayout>
package com.xdr630.progressbarapplication.slice;

import com.xdr630.progressbarapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;

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
        //获取进度条里面原本的值
        //两种获取进度条组件的方式:
        //1.把onStart方法的pb移动到成员位置
        //2.onClick方法的形参,也表示被点击的组件对象
        //下面就使用第二种来实现
        //强转
        ProgressBar pb = (ProgressBar) component;
        //获取进度条里面原本的值
        int progress = pb.getProgress();
        //把进度条里面的值 + 5
        progress = progress + 5;
        //再给进度条设置进度
        pb.setProgressValue(progress);
        //再修改下提示的文字
        pb.setProgressHintText(progress + "%");
    }
}

3. RoundProgressBar进度条

<!--?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">

    <roundprogressbar ohos:height="300vp" ohos:width="300vp" ohos:progress_hint_text="80%" ohos:progress_hint_text_size="50vp" ohos:progress_hint_text_color="#000000" ohos:progress="80" ohos:progress_width="20vp" ohos:progress_color="red" ohos:max="100" ohos:min="0">
</roundprogressbar></directionallayout>

在这里插入图片描述

标签:进度条,ProgressBar,pb,HarmonyOS,ohos,组件,progress
来源: https://www.cnblogs.com/xdr630/p/15343548.html