鸿蒙harmony(2)启动流程简介和获取资源文件内容

1.app的大体结构:

app是由ability包组成的,即多个ability组成。
ability看成是一个窗口容器,每个ability由多个abilitySlice组成,在AbilitySlice中获取布局,显示和操作组件。

app的结构相关数据(应用清单)存放在config.json文件。

启动流程:

2.组件,布局,事件例子:

实现两个界面的跳转:
定义两个ability作为界面,在abilitySlice实现跳转
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button button = findComponentById(ResourceTable.Id_bt);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.example.myapplication")
                        .withAbilityName("com.example.myapplication.Ability2")
                        .build();
                intent1.setOperation(operation);
                startAbility(intent1);
            }
        });
    }

}
View Code

3,单击事件的4种写法

定义实现类:缺点是数据要跨类传递

当前类作为实现类:常用

匿名内部类:缺点是不能复用

方法引用:定义一个和接口方法一样的方法然后引用即可

查看点击源码:
//参数是一个接口
public void setClickedListener(Component.ClickedListener listener);

//接口只要一个方法
public interface ClickedListener {
        void onClick(Component var1);
}

实例:

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
    Button button = findComponentById(ResourceTable.Id_bt);
    button.setClickedListener(this::onClick);//方法引用
    }

   //定义一个接口的要实现的同样方法
    public void onClick(Component component) {
            //我是要引用的方法
    }
}

4.常用点击事件

单击双击滑动

Button button = findComponentById(ResourceTable.Id_bt);
        button.setDoubleClickedListener(new Component.DoubleClickedListener() {
            @Override
            public void onDoubleClick(Component component) {

            }
        });
        button.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {

            }
        });

        //通过按下抬起的位置确定滑动方向
        //滑动 , 按下,移动,抬起
        DirectionalLayout dl = findComponentById(ResourceTable.Id_dl);
        dl.setTouchEventListener(new Component.TouchEventListener() {
            float startx = 0;
            float starty = 0;

            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                //按下
                if (action == TouchEvent.PRIMARY_POINT_DOWN) {
                    //数字表示第几个手指按下
                    MmiPoint position = touchEvent.getPointerPosition(0);
                    startx = position.getX();
                    starty = position.getY();

                    //移动
                } else if (action == TouchEvent.POINT_MOVE) {

                    //抬起
                } else if (action == TouchEvent.PRIMARY_POINT_DOWN) {
                    MmiPoint position = touchEvent.getPointerPosition(0);
                    float endx = position.getX();
                    float endy = position.getY();
                    if (endx>startx){
                        //右滑动
                    }
                }
                //返回false,只出现按下事件
                //返回true,才出现按下 滑动,抬起事件变化
                return true;

            }
        });
View Code

5.读取资源文件内容:

从资源目录profile读取一个my.txt文件内容和media读取icon.jpg图片:

            StringBuilder stringBuilder = new StringBuilder();
            Resource resource = this.getResourceManager().getResource(ResourceTable.Profile_my);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource));
            String line;
            while ( (line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }


        //读取media文件中的图片直接
        int media_icon = ResourceTable.Media_icon;
        Image image=findComponentById(ResourceTable.Id_img);
        image.setImageAndDecodeBounds(media_icon);
  

 

posted @ 2022-01-04 23:58  假程序猿  阅读(488)  评论(0编辑  收藏  举报