首页 文章 精选 留言 我的

精选列表

搜索[HarmonyOS NEXT],共6886篇文章
优秀的个人博客,低调大师

HarmonyOS实战—组件的外边距和内边距

边距 组件的位置属性,分为:内边距 和 外边距 1. 外边距 表示组件跟外部其他组件的边距 案例: 如果只设置 margin ,就会把上下左右都给设置了,这是一个整体的设置。 ohos:margin="10vp" 设置第一个文本组件 ohos:top_margin="10vp" 给下面的文本框设置:ohos:top_margin="20vp" 外边距小节: 组件边框外侧距离其他组件的距离。 如果组件外侧没有其他组件,则是到父布局的距离。 2. 内边距 组件边框内侧跟文本之间的间距 一般设置上内边距和左内边距就行了,因为设置了这两个,就可以确定文本的位置了 利用内边距就可以调整组件内部文本的位置 把第二个文本的内边距设置为:ohos:top_padding="20vp" 内边距小节: 组件边框内侧距离内部文字的距离。

优秀的个人博客,低调大师

HarmonyOS实战—滑动事件的三个动作

1. 滑动事件的三个动作 接口名:TouchEventListener 滑动事件里面分为三个动作:按下不松,移动,抬起。 PRIMARY_POINT_DOWN:按下不松。 POINT_MOVE:移动。 PRIMARY_POINT_UP:抬起。 方法返回值: true 表示继续执行后面的动作。 false 表示不会继续执行后面的动作。 涉及到如下三个动作,根据用户按下位置和松下位置,就可以辨别用户是上、下、左、或右滑动。 如:可以辨别出用户是向右滑动(简称:右滑) 如:可以辨别出用户是向下滑动(简称:下滑) 2. 实现案例:按下、移动或松开都要修改文本的内容 因为要在整个屏幕上滑动,所以要给最外面的布局DirectionalLayout设置滑动事件,加个id 按下、移动或抬起都要修改文本的内容 新建项目:ListenerApplication4 代码实现 ability_main 采用默认生成的Text文本内容,在此基础上给DirectionalLayout布局和Text组件分别加上id <?xml version="1.0" encoding="utf-8"?> <DirectionalLayout ohos:id="$+id:dl" xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text1" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="$string:mainability_HelloWorld" ohos:text_size="40vp" /> </DirectionalLayout> MainAbilitySlice 采用当前类作为实现类接口的方式编写 package com.xdr630.listenerapplication.slice; import com.xdr630.listenerapplication.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Component; import ohos.agp.components.DirectionalLayout; import ohos.agp.components.Text; import ohos.multimodalinput.event.TouchEvent; public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener { Text text1 = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1.先找到整个布局对象 DirectionalLayout dl = (DirectionalLayout) findComponentById(ResourceTable.Id_dl); text1 = (Text) findComponentById(ResourceTable.Id_text1); //2.给整个布局添加滑动事件 //当我们在整个布局滑动的时候,就会调用本类中的onTouchEvent方法 //在按下 移动、松开的过程,代码会不断去调用本类中的 onTouchEvent方法 dl.setTouchEventListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override public boolean onTouchEvent(Component component, TouchEvent touchEvent) { //参数1:component表示滑动的组件(布局也是一种组件,所以也可以用component表示布局对象) //实际上此时代表的就是DirectionalLayout布局对象,这个布局是铺满整个屏幕的 //参数2:touchEvent表示动作对象(按下、滑动、抬起) //获取当前手指对屏幕进行操作(按下、滑动、抬起) int action = touchEvent.getAction(); // 1:表示按下操作 // 2:表示松开操作 // 3. 表示滑动/移动操作 if (action == TouchEvent.PRIMARY_POINT_DOWN){ //只要写按下时需要运行的代码即可 text1.setText("按下"); }else if (action == TouchEvent.POINT_MOVE){ //移动或滑动 text1.setText("移动"); }else if (action == TouchEvent.PRIMARY_POINT_UP){ //松开或抬起 text1.setText("松开"); } return true; } } 运行: 按下: 移动: 松开: 3. 按下、滑动、松开 参数说明 可以看到1、2、3数字分别表示PRIMARY_POINT_DOWN(按下)、PRIMARY_POINT_UP(松开)、POINT_MOVE(移动),所以上面代码的参数也可以直接用数字代替,但为了更直观表达,建议使用参数,一目了然。 如:使用数字表示 if (action == 1){ //只要写按下时需要运行的代码即可 text1.setText("按下"); }else if (action == 3){ //移动或滑动 text1.setText("移动"); }else if (action == 2){ //松开或抬起 text1.setText("松开"); } 4. 验证 按下、 移动、松开的过程,代码会不断去调用本类中的 onTouchEvent方法 在上述代码的基础上,定义成员变量计数器 int count = 0 onTouchEvent方法被调用一次,就给加上一次 把count放在每次操作的后面 当按下时,是第一次调用,count应该为1 移动的时候随着鼠标不断移动,也就会不断地调用onTouchEvent方法,count就会递增 当松开后,也会调用一次,count在前面数值的基础上加1 所以,经过验证: 在 按下 、移动、松开的过程,代码会不断去调用本类中的 onTouchEvent方法。

优秀的个人博客,低调大师

HarmonyOS实战—单击事件的四种写法

单击事件的四种写法 1. 自己编写实现类 编写实现类(MyListener)去实现 Component.ClickedListener 接口 在类里面重新下 onClick 方法,把点击代码实现的操作就写在 onClick 方法当中 实现代码: 创建项目名为:ListenerApplication ability_main.xml <!--?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"> <button ohos:id="$+id:but1" ohos:height="match_content" ohos:width="match_content" ohos:text="点我" ohos:text_size="200" ohos:background_element="red"> </button> </directionallayout> MainAbilitySlice package com.example.listenerapplication.slice; import com.example.listenerapplication.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1.找到按钮 //完整写法:this.findComponentById(ResourceTable.Id_but1); //this:本类的对象,指:MainAbilitySlice(子界面对象) // 在子界面当中,通过 id 找到对应的组件 // 用this去调用方法,this可以省略不写 //findComponentById(ResourceTable.Id_but1); //返回一个组件对象(所以组件的父类对象) //那么我们在实际写代码的时候,需要向下转型:强转 Component but1 = (Button) findComponentById(ResourceTable.Id_but1); //2.给按钮绑定单击事件,当点击后,就会执行 MyListener 中的方法,点一次执行一次 // 而方法就是下面点击的内容 but1.setClickedListener(new MyListener()); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } } class MyListener implements Component.ClickedListener{ @Override public void onClick(Component component) { //Component:所有组件的父类 //component参数: 被点击的组件对象,在这里就表示按你的对象 //component.setText(); setText是子类特有的方法,需要向下转型:强转 Button but = (Button) component; but.setText("被点了"); } } 运行: 点击后: 2. 当前类实现接口 ability_main.xml 中把ohos:text_size="50",其他跟上面一样不变 MainAbilitySlice 中只需把上面新建的类 MyListener 给去掉,然后 AbilitySlice 实现 ClickedListener 接口类中的 onClick 方法,给本类的 but1按钮直接绑定单价事件 package com.example.listenerapplication.slice; import com.example.listenerapplication.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); //1.找到按钮 //完整写法:this.findComponentById(ResourceTable.Id_but1); //this:本类的对象,指:MainAbilitySlice(子界面对象) // 在子界面当中,通过 id 找到对应的组件 // 用this去调用方法,this可以省略不写 //findComponentById(ResourceTable.Id_but1); //返回一个组件对象(所以组件的父类对象) //那么我们在实际写代码的时候,需要向下转型:强转 Component but1 = (Button) findComponentById(ResourceTable.Id_but1); //2.给but1绑定单击事件,当事件被触发后,就会执行本类中的onClick方法,this就代表本类 but1.setClickedListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override public void onClick(Component component) { Button but = (Button) component; but.setText("被点了——单击事件的第二种写法"); } } 运行: 点击后: 3. 自己编写实现类 和 当前类实现接口 的区别 如果添加在按钮上面添加一个Text文本内容,当按钮点击后就会修改文本框的内容 改动第一个案例中的代码:添加Text文本框 上面的 onStart 方法中 text1 是局部变量,如果用第一种方法(自己编写实现类)来写, MyListener 不能调用到 text1 变量 如果使用第二种方法(当前类实现接口),就要把 text1 提到成员变量,再把设置点击后的内容添加到 onClick 方法中 如果在点击按钮之后,需要操作其他的组件对象,那么就可以使用第二种方式(当前类实现接口)。 如果在点击按钮之后,不需要操作其他的组件对象,就可以使用第一种方式(自己编写实现类)。 4. 匿名内部类 采用匿名内部类就不需要实现 implement ClickedListener 接口,也不需要再新建一个类了 但使用匿名内部类的代码只能使用一次。当使用代码需要用一次的时候,可以采用匿名内部类的形式来简化代码 直接 new ClickedListener 就能实现了,然后把第一种实现方式(自己编写实现类)中的 onClick 拿过来或第二种方式(当前类实现接口)实现的 onClick 方法拿过来就行了(其实这两者的onClick方法的内容是一样的),如下: but1.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Button but = (Button) component; but.setText("被点了——单击事件的第三种写法"); text1.setText("被点击了"); } }); 运行: 当被点击后,触发了 onClick 方法中两个设置文本的方法(Button和Text文本都发生了变化) 5. 方法引用 这个方法的形参和方法的返回值类型需要跟接口里的抽象方法里的形参和返回值类型要保持一致 代码实现,布局代码不变跟匿名内部类的一致,改动如下: 直接编写 onClick 方法,不带 @Override ,然后在 onStart 方法中直接调用即可 package com.example.listenerapplication.slice; import com.example.listenerapplication.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.agp.components.Text; public class MainAbilitySlice extends AbilitySlice { Text text1 = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Component but1 = (Button) findComponentById(ResourceTable.Id_but1); text1 = (Text) findComponentById(ResourceTable.Id_text1); but1.setClickedListener(this::onClick); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } public void onClick(Component component) { Button but = (Button) component; but.setText("被点了——单击事件的第四种写法"); text1.setText("被点击了"); } } 当按钮被点击后,就要执行this本类中的onClick方法,相当于把下面的public void onClick...方法拿过来,引用了一下,当做抽象方法的方法体。 运行: 6. 小节 当前类作为实现类和方法引用是比较常用的。其他的写法也要掌握了解即可。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册