1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【鸿蒙 HarmonyOS】UI 组件 ( Button 组件 )

【鸿蒙 HarmonyOS】UI 组件 ( Button 组件 )

时间:2021-12-01 17:45:04

相关推荐

【鸿蒙 HarmonyOS】UI 组件 ( Button 组件 )

文章目录

一、布局文件中设置 Button 组件属性二、代码中修改 Button 组件属性三、Button 点击事件四、完整代码示例五、执行结果六、GitHub 地址

一、布局文件中设置 Button 组件属性

Button 组件是在 UI 界面中的按钮组件 , 重要的用户交互接口 ;

布局文件中设置 Button :

Button 组件在布局文件中的示例 :

<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Buttonohos:id="$+id:button"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#000000"ohos:layout_alignment="horizontal_center"ohos:text="你点啥"ohos:text_size="150"ohos:text_color="#00FF00"/></DirectionalLayout>

id 属性 :ohos:id="$+id:button" , 用于作为当前组件的唯一标识 , 在单个布局文件中不允许 id 标识重复 ;

宽度与高度属性 :可以设置 match_content 和 match_parent 两个值 ;

宽度 :ohos:width=“match_content”高度 :ohos:height=“match_content”

组件位置属性 :ohos:layout_alignment=“horizontal_center” , 上述配置标识组件水平居中 ;

背景设置属性 :ohos:background_element="#000000" , 可以设置一个颜色值 ;

文本设置 :ohos:text=“你点啥” , 设置组件显示的文本为 “你点啥” ;

文本文字大小设置 :ohos:text_size=“150”

文本颜色设置 :ohos:text_color="#00FF00" , 绿色 ;

二、代码中修改 Button 组件属性

代码中设置 Button 属性 :

获取组件 :调用 findComponentById ( ) 方法获取 ;

设置背景 :需要使用 ShapeElement 对象设置 , 下面的代码是设置 Button 组件红色背景 ;

// 修改 Button 背景颜色ShapeElement shapeElement = new ShapeElement();// 设置红色背景shapeElement.setRgbColor(new RgbColor(0xFF, 0x00, 0x00));// 设置 组件 背景button.setBackground(shapeElement);

设置文本 :调用 Button 对象的 setText ( ) 方法设置文本 ;

设置文字大小 :调用 Button 对象的 setTextSize ( ) 方法设置文字大小 ;

设置文字颜色 :调用 Button 对象的 setTextColor ( ) 方法设置文字颜色 ;

完整代码示例 :设置 Button 组件红色背景 , 白色字体 , 180 大小文字 , 以及文本显示内容 ;

// 修改 Button 按钮属性// 修改 Button 背景颜色ShapeElement shapeElement = new ShapeElement();// 设置红色背景shapeElement.setRgbColor(new RgbColor(0xFF, 0x00, 0x00));// 设置 组件 背景button.setBackground(shapeElement);// 设置文本button.setText("点你咋地");// 设置文本颜色button.setTextColor(Color.WHITE);// 设置文本大小button.setTextSize(180);

三、Button 点击事件

点击 Button 按钮事件 :

设置 Component.ClickedListener 点击监听器 , 点击 Button 按钮组件后通过该方法回调 ;

// 获取 XML 布局中的 Button 按钮Button button = (Button) findComponentById(ResourceTable.Id_button);// 设置 Button 按钮点击事件button.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {}}

四、完整代码示例

主界面代码 :

package com.example.button.slice;import com.example.button.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.colors.RgbColor;import ponents.Button;import ponent;import ponents.element.ShapeElement;import ohos.agp.utils.Color;public class MainAbilitySlice extends AbilitySlice {@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);// 获取 XML 布局中的 Button 按钮Button button = (Button) findComponentById(ResourceTable.Id_button);// 设置 Button 按钮点击事件button.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {// 修改 Button 按钮属性// 修改 Button 背景颜色ShapeElement shapeElement = new ShapeElement();// 设置红色背景shapeElement.setRgbColor(new RgbColor(0xFF, 0x00, 0x00));// 设置 组件 背景button.setBackground(shapeElement);// 设置文本button.setText("点你咋地");// 设置文本颜色button.setTextColor(Color.WHITE);// 设置文本大小button.setTextSize(180);}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}}

布局文件代码 :

<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Buttonohos:id="$+id:button"ohos:height="match_content"ohos:width="match_content"ohos:background_element="#000000"ohos:layout_alignment="horizontal_center"ohos:text="你点啥"ohos:text_size="150"ohos:text_color="#00FF00"/></DirectionalLayout>

五、执行结果

点击前 :

点击后 :

六、GitHub 地址

GitHub 主应用 :/han120/HarmonyHelloWorld

Button 组件示例 Module :/han120/HarmonyHelloWorld/tree/master/button

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。