当前位置: 首页 > news >正文

【鸿蒙学习笔记】基础组件 Button

官方文档:按钮 (Button)添加链接描述
官方文档:button开发指导

目录标题

  • 属性迭代完善
    • 不含子组件的按钮
    • 包含子组件的按钮
    • ButtonType
    • 添加事件
    • 跳转超链接
    • 提交表单
    • 悬浮按钮

属性迭代完善

不含子组件的按钮

Column({ space: 10 }) {Row() {Button('添加子目标', { type: ButtonType.Normal, stateEffect: true }) // type用于设置Button类型,stateEffect属性设置Button是否开启点击效果。.width(160)// 宽度.height(60)// 高度.backgroundColor(Color.Yellow)// 背景色.margin({ top: 20, left: 20, right: 20, bottom: 20 })// 外边距.padding({ top: 20, left: 20, right: 20, bottom: 20 })// 内边距.borderRadius(1)// 四个角的圆弧度.fontSize(18)//字体大小.fontColor('#007DFF')//字体颜色.fontWeight(800) // 字体宽度.position({ x: '80%', y: 600 }) // 位置.shadow({ radius: 10 }) //重影强度.onClick(() => {// 某些操作}) // 点击事件}.width('90%').height(90).border({ width: 1, style: BorderStyle.Dashed })
}.width('100%').height(100).margin({ top: 30 })

包含子组件的按钮

Column({ space: 10 }) {Button({ type: ButtonType.Normal, stateEffect: true }) {Row() {Image($r('app.media.icon')).width(20).height(40).margin({ left: 12 })Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })}.alignItems(VerticalAlign.Center)}.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
}.width('100%').height(100).margin({ top: 30 })

在这里插入图片描述

ButtonType

Column({ space: 10 }) {Text('Normal').fontSize(20).fontColor(Color.Black).width('90%')Button('Ok', { type: ButtonType.Normal, stateEffect: true }).width(90).height(40)Text('Capsule').fontSize(20).fontColor(Color.Black).width('90%')Button('Ok', { type: ButtonType.Capsule, stateEffect: false }).width(90).height(40)Text('Circle').fontSize(20).fontColor(Color.Black).width('90%')Button('Ok', { type: ButtonType.Circle, stateEffect: false }).width(70).height(70)Text('自定义').fontSize(20).fontColor(Color.Black).width('90%')Button('Ok', { type: ButtonType.Normal, stateEffect: false }).width(70).height(70).borderRadius(20)
}.width('100%').height(100).margin({ top: 30 })

在这里插入图片描述

添加事件

Column({ space: 10 }) {Button('Ok', { type: ButtonType.Normal, stateEffect: true }).onClick(() => {console.info('Button onClick')})
}.width('100%').height(100).margin({ top: 30 })

在这里插入图片描述

跳转超链接

import router from '@ohos.router';@Entry
@Component
struct SizeExample {@State FurL: router.RouterOptions = { 'url': 'pages/FirstPage' }@State SurL: router.RouterOptions = { 'url': 'pages/SecondPage' }build() {List({ space: 4 }) {ListItem() {Button("First").onClick(() => {router.pushUrl(this.FurL)}).width('100%')}ListItem() {Button("Second").onClick(() => {router.pushUrl(this.SurL)}).width('100%')}}.listDirection(Axis.Vertical).backgroundColor(0xDCDCDC).padding(20)}
}

在这里插入图片描述

提交表单

  build() {Column() {TextInput({ placeholder: 'input your username' }).margin({ top: 20 })TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })Button('Register').width(300).margin({ top: 20 }).onClick(() => {// 需要执行的操作})}.padding(20)}
}

在这里插入图片描述

悬浮按钮

private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
build() {Stack() {List({ space: 20, initialIndex: 0 }) {ForEach(this.arr, (item: number) => {ListItem() {Text('' + item).width('100%').height(100).fontSize(16).textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)}}, (item: number) => item.toString())}.width('90%')Button() {Image($r('app.media.icon')).width(50).height(50)}.width(60).height(60).position({ x: '80%', y: 600 }).shadow({ radius: 10 }).onClick(() => {// 需要执行的操作})}.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
}

在这里插入图片描述

相关文章:

  • 可重入锁思想,设计MQ迁移方案
  • 高考文化课|高三这些高效学习方法你了解了吗?
  • adb热更新
  • SpringMVC的基本使用
  • Spring Data与多数据源配置
  • 《昇思25天学习打卡营第9天|onereal》
  • adb shell logcat -b all|grep如何可以grep两个子串?
  • Rust Eq 和 PartialEq
  • 第三节:如何理解Spring的两个特性IOC和AOP(自学Spring boot 3.x第一天)
  • 嵌入式学习(Day 51:ARM指令/汇编与c语言函数相互调用)
  • 红酒SPA:享受放松与奢华的很好结合
  • (四)opengl函数加载和错误处理
  • 【RNN练习】LSTM-火灾温度预测
  • 【软考论文】论基于架构的软件设计方法及应用(ABSD)
  • 【C++】数组、字符串
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 【刷算法】从上往下打印二叉树
  • docker容器内的网络抓包
  • HTTP请求重发
  • Intervention/image 图片处理扩展包的安装和使用
  • Linux CTF 逆向入门
  • php面试题 汇集2
  • 前端代码风格自动化系列(二)之Commitlint
  • 前端临床手札——文件上传
  • 如何使用 JavaScript 解析 URL
  • 入门级的git使用指北
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 微信开源mars源码分析1—上层samples分析
  • 微信小程序:实现悬浮返回和分享按钮
  • 因为阿里,他们成了“杭漂”
  • 最简单的无缝轮播
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • ######## golang各章节终篇索引 ########
  • #考研#计算机文化知识1(局域网及网络互联)
  • #控制台大学课堂点名问题_课堂随机点名
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • #我与Java虚拟机的故事#连载03:面试过的百度,滴滴,快手都问了这些问题
  • (3) cmake编译多个cpp文件
  • (7)STL算法之交换赋值
  • (C语言)共用体union的用法举例
  • (LeetCode C++)盛最多水的容器
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (附源码)ssm智慧社区管理系统 毕业设计 101635
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (译) 函数式 JS #1:简介
  • .axf 转化 .bin文件 的方法
  • .bat批处理(六):替换字符串中匹配的子串
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .net core使用ef 6
  • .NET Core引入性能分析引导优化