鸿蒙OS Button

2020-10-12 10:05 更新

鸿蒙OS Button

按钮(Button)是一种常见的组件,点击可以触发对应的操作,通常由文本或图标组成,也可以由图标和文本共同组成。

图1 文本按钮 img

图2 图标按钮 img

图3 图标和文本共同组成的按钮 img

创建Button

使用 Button 组件,可以生成形状、颜色丰富的按钮。

  1. <Button
  2. ohos:id="$+id:button_sample"
  3. ohos:width="match_content"
  4. ohos:height="match_content"
  5. ohos:text_size="27fp"
  6. ohos:text="button"
  7. ohos:background_element="$graphic:button_element"
  8. ohos:left_margin="15vp"
  9. ohos:bottom_margin="15vp"
  10. ohos:right_padding="8vp"
  11. ohos:left_padding="8vp"
  12. ohos:element_left="$graphic:ic_btn_reload"

button_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="10"/>
  6. <solid
  7. ohos:color="#FF007DFF"/>
  8. </shape>

img

响应点击事件

按钮的重要作用是当用户单击按钮时,会执行相应的操作或者界面出现相应的变化。实际上用户点击按钮时,Button 对象将收到一个点击事件。 开发者可以自定义响应点击事件的方法。例如,通过创建一个 Component.ClickedListener 对象,然后通过调用 setClickedListener 将其分配给按钮。

  1. //从定义的xml中获取Button对象
  2. Button button = (Button) rootLayout.findComponentById(ResourceTable.Id_button_sample);
  3. // 为按钮设置点击事件回调
  4. button.setClickedListener(new Component.ClickedListener() {
  5. public void onClick(Component v) {
  6. // 此处添加点击按钮后的事件处理逻辑
  7. }
  8. });

不同类型的按钮

按照按钮的形状,按钮可以分为:普通按钮,椭圆按钮,胶囊按钮,圆形按钮等。

  • 普通按钮

img

普通按钮和其他按钮的区别在于不需要设置任何形状,只设置文本和背景颜色即可,例如:

  1. <Button
  2. ohos:width="150vp"
  3. ohos:height="50vp"
  4. ohos:text_size="27fp"
  5. ohos:text="button"
  6. ohos:background_element="$graphic:color_blue_element"
  7. ohos:left_margin="15vp"
  8. ohos:bottom_margin="15vp"
  9. ohos:right_padding="8vp"
  10. ohos:left_padding="8vp"
  11. />

color_blue_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#FF007DFF"/>
  6. </shape>

  • 椭圆按钮

img

椭圆按钮是通过设置 background_element 的来实现的,background_element 的shape 设置为椭圆(oval),例如:

  1. <Button
  2. ohos:width="150vp"
  3. ohos:height="50vp"
  4. ohos:text_size="27fp"
  5. ohos:text="button"
  6. ohos:background_element="$graphic:oval_button_element"
  7. ohos:left_margin="15vp"
  8. ohos:bottom_margin="15vp"
  9. ohos:right_padding="8vp"
  10. ohos:left_padding="8vp"
  11. ohos:element_left="$graphic:ic_btn_reload"
  12. />

oval_button_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="oval">
  4. <solid
  5. ohos:color="#FF007DFF"/>
  6. </shape>

  • 胶囊按钮

img

胶囊按钮是一种常见的按钮,设置按钮背景时将背景设置为矩形形状,并且设置 ShapeElement 的 radius 的半径,例如:

  1. <Button
  2. ohos:id="$+id:button"
  3. ohos:width="match_content"
  4. ohos:height="match_content"
  5. ohos:text_size="27fp"
  6. ohos:text="button"
  7. ohos:background_element="$graphic:capsule_button_element"
  8. ohos:left_margin="15vp"
  9. ohos:bottom_margin="15vp"
  10. ohos:right_padding="15vp"
  11. ohos:left_padding="15vp"
  12. />

capsule_button_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="100"/>
  6. <solid
  7. ohos:color="#FF007DFF"/>
  8. </shape>

  • 圆形按钮

img

圆形按钮和椭圆按钮的区别在于组件本身的宽度和高度需要相同,例如:

  1. <Button
  2. ohos:id="$+id:button4"
  3. ohos:width="50vp"
  4. ohos:height="50vp"
  5. ohos:text_size="27fp"
  6. ohos:background_element="$graphic:circle_button_element"
  7. ohos:text="+"
  8. ohos:left_margin="15vp"
  9. ohos:bottom_margin="15vp"
  10. ohos:right_padding="15vp"
  11. ohos:left_padding="15vp"
  12. />

circle_button_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="oval">
  4. <solid
  5. ohos:color="#FF007DFF"/>
  6. </shape>

场景示例

利用圆形按钮,胶囊按钮,文本组件可以绘制出如下拨号盘的UI界面。

图4 界面效果

img

源码示例:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:width="match_parent"
  5. ohos:height="match_parent"
  6. ohos:background_element="$graphic:color_light_gray_element"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:width="match_content"
  10. ohos:height="match_content"
  11. ohos:text_size="20fp"
  12. ohos:text="0123456789"
  13. ohos:background_element="$graphic:green_text_element"
  14. ohos:text_alignment="center"
  15. ohos:layout_alignment="horizontal_center"
  16. />
  17. <DirectionalLayout
  18. ohos:width="match_parent"
  19. ohos:height="match_content"
  20. ohos:alignment="horizontal_center"
  21. ohos:orientation="horizontal"
  22. ohos:top_margin="5vp"
  23. ohos:bottom_margin="5vp">
  24. <Button
  25. ohos:width="40vp"
  26. ohos:height="40vp"
  27. ohos:text_size="15fp"
  28. ohos:background_element="$graphic:green_circle_button_element"
  29. ohos:text="1"
  30. ohos:text_alignment="center"
  31. />
  32. <Button
  33. ohos:width="40vp"
  34. ohos:height="40vp"
  35. ohos:text_size="15fp"
  36. ohos:background_element="$graphic:green_circle_button_element"
  37. ohos:text="2"
  38. ohos:left_margin="5vp"
  39. ohos:right_margin="5vp"
  40. ohos:text_alignment="center"
  41. />
  42. <Button
  43. ohos:width="40vp"
  44. ohos:height="40vp"
  45. ohos:text_size="15fp"
  46. ohos:background_element="$graphic:green_circle_button_element"
  47. ohos:text="3"
  48. ohos:text_alignment="center"
  49. />
  50. </DirectionalLayout>
  51. <DirectionalLayout
  52. ohos:width="match_parent"
  53. ohos:height="match_content"
  54. ohos:alignment="horizontal_center"
  55. ohos:orientation="horizontal"
  56. ohos:bottom_margin="5vp">
  57. <Button
  58. ohos:width="40vp"
  59. ohos:height="40vp"
  60. ohos:text_size="15fp"
  61. ohos:background_element="$graphic:green_circle_button_element"
  62. ohos:text="4"
  63. ohos:text_alignment="center"
  64. />
  65. <Button
  66. ohos:width="40vp"
  67. ohos:height="40vp"
  68. ohos:text_size="15fp"
  69. ohos:left_margin="5vp"
  70. ohos:right_margin="5vp"
  71. ohos:background_element="$graphic:green_circle_button_element"
  72. ohos:text="5"
  73. ohos:text_alignment="center"
  74. />
  75. <Button
  76. ohos:width="40vp"
  77. ohos:height="40vp"
  78. ohos:text_size="15fp"
  79. ohos:background_element="$graphic:green_circle_button_element"
  80. ohos:text="6"
  81. ohos:text_alignment="center"
  82. />
  83. </DirectionalLayout>
  84. <DirectionalLayout
  85. ohos:width="match_parent"
  86. ohos:height="match_content"
  87. ohos:alignment="horizontal_center"
  88. ohos:orientation="horizontal"
  89. ohos:bottom_margin="5vp">
  90. <Button
  91. ohos:width="40vp"
  92. ohos:height="40vp"
  93. ohos:text_size="15fp"
  94. ohos:background_element="$graphic:green_circle_button_element"
  95. ohos:text="7"
  96. ohos:text_alignment="center"
  97. />
  98. <Button
  99. ohos:width="40vp"
  100. ohos:height="40vp"
  101. ohos:text_size="15fp"
  102. ohos:left_margin="5vp"
  103. ohos:right_margin="5vp"
  104. ohos:background_element="$graphic:green_circle_button_element"
  105. ohos:text="8"
  106. ohos:text_alignment="center"
  107. />
  108. <Button
  109. ohos:width="40vp"
  110. ohos:height="40vp"
  111. ohos:text_size="15fp"
  112. ohos:background_element="$graphic:green_circle_button_element"
  113. ohos:text="9"
  114. ohos:text_alignment="center"
  115. />
  116. </DirectionalLayout>
  117. <DirectionalLayout
  118. ohos:width="match_parent"
  119. ohos:height="match_content"
  120. ohos:alignment="horizontal_center"
  121. ohos:orientation="horizontal"
  122. ohos:bottom_margin="5vp">
  123. <Button
  124. ohos:width="40vp"
  125. ohos:height="40vp"
  126. ohos:text_size="15fp"
  127. ohos:background_element="$graphic:green_circle_button_element"
  128. ohos:text="*"
  129. ohos:text_alignment="center"
  130. />
  131. <Button
  132. ohos:width="40vp"
  133. ohos:height="40vp"
  134. ohos:text_size="15fp"
  135. ohos:left_margin="5vp"
  136. ohos:right_margin="5vp"
  137. ohos:background_element="$graphic:green_circle_button_element"
  138. ohos:text="0"
  139. ohos:text_alignment="center"
  140. />
  141. <Button
  142. ohos:width="40vp"
  143. ohos:height="40vp"
  144. ohos:text_size="15fp"
  145. ohos:background_element="$graphic:green_circle_button_element"
  146. ohos:text="#"
  147. ohos:text_alignment="center"
  148. />
  149. </DirectionalLayout>
  150. <Button
  151. ohos:width="match_content"
  152. ohos:height="match_content"
  153. ohos:text_size="15fp"
  154. ohos:text="CALL"
  155. ohos:background_element="$graphic:green_capsule_button_element"
  156. ohos:bottom_margin="5vp"
  157. ohos:text_alignment="center"
  158. ohos:layout_alignment="horizontal_center"
  159. ohos:left_padding="10vp"
  160. ohos:right_padding="10vp"
  161. ohos:top_padding="2vp"
  162. ohos:bottom_padding="2vp"
  163. />
  164. </DirectionalLayout>

color_light_gray_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <solid
  5. ohos:color="#ffeeeeee"/>
  6. </shape>

green_text_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="20"/>
  6. <stroke
  7. ohos:width="2"
  8. ohos:color="#ff008B00"/>
  9. <solid
  10. ohos:color="#ffeeeeee"/>
  11. </shape>

green_circle_button_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="oval">
  4. <stroke
  5. ohos:width="5"
  6. ohos:color="#ff008B00"/>
  7. <solid
  8. ohos:color="#ffeeeeee"/>
  9. </shape>

green_capsule_button_element.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="100"/>
  6. <solid
  7. ohos:color="#ff008B00"/>
  8. </shape>
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号