本文介绍在鸿蒙应用中button组件的基本用法。
java
增长按钮组件web
以下代码中红色部分所示,在布局中增长button组件。
编程
<?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:orientation="vertical">
<Component
ohos:height="0vp"
ohos:weight="3"
ohos:width="match_parent"
/>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:image"
ohos:width="match_content"
ohos:height="match_content"
ohos:layout_alignment="center"
ohos:image_src="$media:DevEco"
/>
<Button
ohos:id="$+id:hello_button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="27fp"
ohos:text="Click me!"
ohos:layout_alignment="center"
ohos:background_element="$graphic:background_button"
ohos:margin="15vp"
ohos:right_padding="8vp"
ohos:left_padding="8vp"
/>
</DirectionalLayout>
<Component
ohos:height="0vp"
ohos:weight="5"
ohos:width="match_parent"
/>
</DirectionalLayout>
代码中按钮id被指定为hello_button,这个信息会在下面的响应代码中用到。设计模式
增长操做事件响应代码
微信
以下面代码中红色部分所示,为button组件增长响应代码。
架构
package com.example.helloharmony.slice;
import com.example.helloharmony.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
public class ComponentAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_component);
Button button = (Button) findComponentById(ResourceTable.Id_hello_button);
// 为按钮设置点击事件回调
button.setClickedListener(new Component.ClickedListener() {
public void onClick(Component v) {
new ToastDialog(getContext())
.setText("你好,鸿蒙!")
.show();
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}}app
代码中使用布局文件中指定的组件id选择【Cliek me!】按钮以后,为button组件增长了一段在画面底部显示“你好,鸿蒙!"信息的响应代码。须要注意的是id的格式:ResourceTable.Id_hello_button。ide
参考文档工具
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-component-button-0000001051009585布局
新书介绍
《实战Python设计模式》是做者最近出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,经过可执行的示例对23 个设计模式逐个进行说明。这样一方面能够使读者了解真实的软件开发工做中每一个设计模式的运用场景和想要解决的问题;另外一方面经过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣并且但愿随学随用的读者经过本书能够快速跨越从理解到运用的门槛;但愿学习Python GUI 编程的读者能够将本书中的示例做为设计和开发的参考;使用Python 语言进行图像分析、数据处理工做的读者能够直接以本书中的示例为基础,迅速构建本身的系统架构。
以为本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
本文分享自微信公众号 - 面向对象思考(OOThinkingDalian)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。