一、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 + "%");
}
}
}