鸿蒙OS入门

Hello world教程

设备类型wearable
倒计时demo地址

控制台打印

  • Hilog 类
    参数HilogLabel label String format, Object…
    debug 输出调试信息
    info 输出一般信息
    warn 输出警告信息
    error 输出错误信息
    fatal 输出严重错误信息
简单案例
@Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);
        String printssss = "蓝天";
        /**
         * type HiLogLabel 类型 HiLog.INFO 等等
         * domain  定义服务域( Service Domain )
         * tag 标签名称
         */
        HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "测试");
        HiLog.info(hiLogLabel,"failed to visit%{printssss}s");
        //创建文本对象
        Text text = new Text(this);
        //设置布局参数
        text.setLayoutConfig(config);
        //设置文本内容
        text.setText("Hello 蓝天");
        //设置文本颜色
        text.setTextColor(new Color(0xFF000000));
        //设置问题字体大小
        text.setTextSize(50);
        //设置文字剧中方式
        text.setTextAlignment(TextAlignment.CENTER);
        //在布局中添加文本
        myLayout.addComponent(text);
        //设置布局
        super.setUIContent(myLayout);
    }

线性布局

package com.example.myapplicationjava.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        //设置横向排列 默认纵向 flutter 排列类似
        myLayout.setOrientation(Component.HORIZONTAL);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(100, 100, 100));
        myLayout.setBackground(element);
        //在布局中添加文本
        myLayout.addComponent(createText("llt1"));
        myLayout.addComponent(createText("llt2"));
        myLayout.addComponent(createText("llt3"));
        //设置布局
        super.setUIContent(myLayout);
    }
    private Text createText(String str){
        /**
         * 1 宽
         * 2 高
         */
        //这里高度设置200 会超出屏幕有部分无法显示这里需要配置权重
        LayoutConfig config1 = new LayoutConfig(LayoutConfig.MATCH_CONTENT, 200);
        //HORIZONTAL_CENTER 水平居中
        config1.alignment = LayoutAlignment.HORIZONTAL_CENTER;//横向排列的时候这个时候需要改成垂直居中
        //垂直居中
        config1.alignment = LayoutAlignment.VERTICAL_CENTER;
        config1.weight = 1;//权重 1 这个设置完成就平均分配剧中显示了 相当于html flex :1
        Text text = new Text(this);
        //设置布局参数
        text.setLayoutConfig(config1);
        //设置文本内容
        text.setText(str);
        //设置文本颜色
        text.setTextColor(new Color(0xFFFFFF00));
        //设置问题字体大小
        text.setTextSize(50);
        //设置文字剧中方式
        text.setTextAlignment(TextAlignment.CENTER);
        return  text;
    }
    @Override
    public void onActive() {
        super.onActive();
    }

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

相对布局

package com.example.myapplicationjava.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.*;
//import ohos.agp.components.DirectionalLayout;
//import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.DependentLayout.LayoutConfig;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    //相对布局
    private DependentLayout myLayout = new DependentLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        LayoutConfig configText = new LayoutConfig();
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(100, 100, 100));
        myLayout.setBackground(element);
        //在布局中添加文本
        //这里高度设置200 会超出屏幕有部分无法显示这里需要配置权重
        LayoutConfig config1 = new LayoutConfig(LayoutConfig.MATCH_PARENT,LayoutConfig.MATCH_PARENT);
        config1.addRule(LayoutConfig.CENTER_IN_PARENT);
        Text text = new Text(this);
        //设置布局参数
        text.setLayoutConfig(config1);
        //设置文本内容
        text.setText("计时器文本");
        //设置文本颜色
        text.setTextColor(new Color(0xFFFFFF00));
        //设置问题字体大小
        text.setTextSize(75);
        //设置文字剧中方式
        text.setTextAlignment(TextAlignment.CENTER);
        myLayout.addComponent(text);
        //初始化按钮
        Button button = new Button(this);
        button.setText("暂停");
        button.setTextSize(50);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(141,255,231));
        button.setBackground(shapeElement);
        /**
         * MATCH_PARENT 相对父级尺度
         * MATCH_CONTENT  相对内容尺度
         */
        LayoutConfig config2 = new LayoutConfig(LayoutConfig.MATCH_PARENT,LayoutConfig.MATCH_CONTENT);
        /**
         * ALIGN_PARENT_BOTTOM 相对父级底部
         */
        config2.addRule(LayoutConfig.ALIGN_PARENT_BOTTOM);
        /**
         * 相对父级中心 水平居中默认
         */
//        config2.addRule(LayoutConfig.CENTER_IN_PARENT);
        button.setLayoutConfig(config2);
        //设置布局

        myLayout.addComponent(button);
        super.setUIContent(myLayout);
    }

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

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

生命周期

onStart() 创建Ability时出发 仅触发一次
onActive():进入前台时触发
onForeground():可见时间触发
onBackGround():不可见时间触发
onInActive: 失去交点触发
onStop 销毁Ability 时间触发仅触发一次
AbilitySlice 的生命周期于Ablitiy类似

Ability(功能模块)的跳转

需要类 Intent

Intent secondIntent = new Intent();
Operation operation = new Intent.OperationBuilder( )
.withDeviceId("")//设备id
.withBundleName ( " com. example . customtimer")//包名字
.withAbilityName( " com. examp1e . customtimer . SecondaryAbility")//类名字
.build();//执行上面操作
secondIntent. setoperation(operation);
startAbility( secondIntent);//跳转

多线程于人物分发器

TaskDispatcher 任务分发器
任务等级 HIGH ,DEFAULT,LOW
● GlobalTaskDispatcher :全局并发任务分发器 不确定顺序
● ParallelTaskDispatcher :并发任务分发器 不确定顺序
● SerialTaskDispatcher :串行任务分发器 确定顺序
● SpecTaskDispatcher :专有任务分发器

更新UI界面

  getUITaskDispatcher()
            .asyncDispatch(new Runnable() {
                @Override
                public void run() {
                //button 按钮对象
                 button.setText("dd");
                }
            });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值