自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在鴻蒙HarmonyOS智慧屏上實現(xiàn)一款粗糙的計算器

開發(fā) OpenHarmony
本節(jié)我將在HarmonyOS智慧屏上實現(xiàn)常規(guī)的計算器。

 [[374369]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

在學(xué)習(xí)的路上我們不能只是停留在對理論知識的理解,還應(yīng)該將理論和實戰(zhàn)進(jìn)行結(jié)合,這樣才有利于我們能夠更有深度的掌握知識,最終形成自己的知識體系結(jié)構(gòu)。我們在實戰(zhàn)的時候,不僅可以鞏固我們的理論知識,還能夠在實戰(zhàn)中發(fā)現(xiàn)問題,并找到合適的解決方案。

前面的章節(jié)中我們已經(jīng)學(xué)習(xí)了六大布局和常用的組件,我們在學(xué)習(xí)布局和組件的時候也有一些小示例。通過這些小示例我們僅僅是對當(dāng)前的布局和組件的使用有了深刻的認(rèn)識,但UI界面布局中不可能單純只存在某個組件,復(fù)雜的UI界面中會出現(xiàn)多種布局、多種組件進(jìn)行組合。本節(jié)我將在HarmonyOS智慧屏上實現(xiàn)常規(guī)的計算器。

整個計算器是將文本組件和按鈕組件組合起來,然后放在一個容器中。通過監(jiān)聽按鈕的點擊事件并更改文本組件的顯示內(nèi)容,最終達(dá)成計算器(本節(jié)示例中遺忘了小數(shù)點,如果有興趣的話,可以自己嘗試的加上小數(shù)點)的效果。

對于計算器UI界面而言,我將其拆解為上下兩部分,上區(qū)域用于顯示,下區(qū)域用于按鈕組。在上區(qū)域存在兩個Text組件,分別用于顯示數(shù)學(xué)表達(dá)式和按鈕對應(yīng)的數(shù)字值。下區(qū)域是一些按鈕組件,數(shù)字區(qū)域,運算符號以及回退和清除。

 

對于整個示例布局我做了簡單的拆解和介紹,接下來我將以代碼的形式實現(xiàn)上面的UI界面。為了使各個布局中的組件能通過不同的占比顯示,我這里選用DirectionalLayout布局,并設(shè)置它的權(quán)重比,來實現(xiàn)組件在不居中的占比。

1、整個布局分為上下兩個區(qū)域,因此DirectionalLayout布局的方向為vertical(垂直)。并且分為兩個區(qū)域,上下區(qū)域占比為2:3。

  1. <DirectionalLayout 
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:orientation="vertical"
  6.     <DirectionalLayout 
  7.         ohos:height="match_parent" 
  8.         ohos:width="match_parent" 
  9.         ohos:weight="2" 
  10.         ohos:orientation="vertical"
  11.     </DirectionalLayout> 
  12.  
  13.     <DirectionalLayout 
  14.         ohos:height="match_parent" 
  15.         ohos:width="match_parent" 
  16.         ohos:weight="3" 
  17.         ohos:orientation="vertical"
  18.     </DirectionalLayout> 
  19. </DirectionalL 

 2、上區(qū)域是兩個Text組件,布局依舊是DirectionalLayout布局,兩個組件在布局中的權(quán)重比為3:4。

  1. <DirectionalLayout 
  2.       ohos:height="match_parent" 
  3.       ohos:width="match_parent" 
  4.       ohos:weight="2" 
  5.       ohos:orientation="vertical"
  6.       <Text 
  7.           ohos:id="$+id:show_math_expression" 
  8.           ohos:height="match_parent" 
  9.           ohos:width="match_parent" 
  10.           ohos:background_element="#FFFFFF" 
  11.           ohos:weight="3" 
  12.           ohos:text="5+2+7-" 
  13.           ohos:text_size="40fp" 
  14.           ohos:text_alignment="right | vertical_center" 
  15.           ohos:right_padding="20vp"/> 
  16.       <Text 
  17.           ohos:id="$+id:show_input_value" 
  18.           ohos:height="match_parent" 
  19.           ohos:width="match_parent" 
  20.           ohos:background_element="#F2F2F2" 
  21.           ohos:weight="4" 
  22.           ohos:text="1" 
  23.           ohos:text_size="60fp" 
  24.           ohos:text_alignment="right | vertical_center" 
  25.           ohos:right_padding="20vp"/> 
  26.   </DirectionalLayout> 

 3、下區(qū)域為按鈕組區(qū)域,分為三部分,除了等號之外的按鈕都是在各自布局中的占比為1。

  1. <DirectionalLayout 
  2.      ohos:height="match_parent" 
  3.      ohos:width="match_parent" 
  4.      ohos:weight="3" 
  5.      ohos:orientation="vertical"
  6.      <DirectionalLayout 
  7.          ohos:height="match_parent" 
  8.          ohos:width="match_parent" 
  9.          ohos:weight="1" 
  10.          ohos:background_element="#FFFF00" 
  11.          ohos:orientation="horizontal"
  12.          <Button 
  13.              ohos:height="match_parent" 
  14.              ohos:width="match_parent" 
  15.              ohos:weight="1" 
  16.              ohos:text_size="50fp" 
  17.              ohos:background_element="$graphic:background_button_style" 
  18.              ohos:text_alignment="center" 
  19.              ohos:text_color="#455A64" 
  20.              ohos:text_weight="700" 
  21.              ohos:text="7"/> 
  22.          <Button 
  23.              ohos:height="match_parent" 
  24.              ohos:width="match_parent" 
  25.              ohos:weight="1" 
  26.              ohos:text_size="50fp" 
  27.              ohos:background_element="$graphic:background_button_style" 
  28.              ohos:text_alignment="center" 
  29.              ohos:text_color="#455A64" 
  30.              ohos:text_weight="700" 
  31.              ohos:text="8"/> 
  32.          <Button 
  33.              ohos:height="match_parent" 
  34.              ohos:width="match_parent" 
  35.              ohos:weight="1" 
  36.              ohos:text_size="50fp" 
  37.              ohos:background_element="$graphic:background_button_style" 
  38.              ohos:text_alignment="center" 
  39.              ohos:text_color="#455A64" 
  40.              ohos:text_weight="700" 
  41.              ohos:text="9"/> 
  42.          <Button 
  43.              ohos:height="match_parent" 
  44.              ohos:width="match_parent" 
  45.              ohos:weight="1" 
  46.              ohos:text_size="50fp" 
  47.              ohos:background_element="$graphic:background_button_style" 
  48.              ohos:text_alignment="center" 
  49.              ohos:text="+"/> 
  50.          <Button 
  51.              ohos:height="match_parent" 
  52.              ohos:width="match_parent" 
  53.              ohos:weight="1" 
  54.              ohos:text_size="50fp" 
  55.              ohos:background_element="$graphic:background_button_style" 
  56.              ohos:text_alignment="center" 
  57.              ohos:text="-"/> 
  58.          <Image 
  59.              ohos:height="match_parent" 
  60.              ohos:width="match_parent" 
  61.              ohos:weight="1" 
  62.              ohos:background_element="$graphic:background_button_style" 
  63.              ohos:text_alignment="center" 
  64.              ohos:image_src="$media:delete"/> 
  65.      </DirectionalLayout> 
  66.      <DirectionalLayout 
  67.          ohos:height="match_parent" 
  68.          ohos:width="match_parent" 
  69.          ohos:weight="1" 
  70.          ohos:background_element="#FF00FF" 
  71.          ohos:orientation="horizontal"
  72.          <Button 
  73.              ohos:height="match_parent" 
  74.              ohos:width="match_parent" 
  75.              ohos:weight="1" 
  76.              ohos:text_size="50fp" 
  77.              ohos:background_element="$graphic:background_button_style" 
  78.              ohos:text_alignment="center" 
  79.              ohos:text_color="#455A64" 
  80.              ohos:text_weight="700" 
  81.              ohos:text="4"/> 
  82.          <Button 
  83.              ohos:height="match_parent" 
  84.              ohos:width="match_parent" 
  85.              ohos:weight="1" 
  86.              ohos:text_size="50fp" 
  87.              ohos:background_element="$graphic:background_button_style" 
  88.              ohos:text_alignment="center" 
  89.              ohos:text_color="#455A64" 
  90.              ohos:text_weight="700" 
  91.              ohos:text="5"/> 
  92.          <Button 
  93.              ohos:height="match_parent" 
  94.              ohos:width="match_parent" 
  95.              ohos:weight="1" 
  96.              ohos:text_size="50fp" 
  97.              ohos:background_element="$graphic:background_button_style" 
  98.              ohos:text_alignment="center" 
  99.              ohos:text_color="#455A64" 
  100.              ohos:text_weight="700" 
  101.              ohos:text="6"/> 
  102.          <Button 
  103.              ohos:height="match_parent" 
  104.              ohos:width="match_parent" 
  105.              ohos:weight="1" 
  106.              ohos:text_size="50fp" 
  107.              ohos:background_element="$graphic:background_button_style" 
  108.              ohos:text_alignment="center" 
  109.              ohos:text="*"/> 
  110.          <Button 
  111.              ohos:height="match_parent" 
  112.              ohos:width="match_parent" 
  113.              ohos:weight="1" 
  114.              ohos:text_size="50fp" 
  115.              ohos:background_element="$graphic:background_button_style" 
  116.              ohos:text_alignment="center" 
  117.              ohos:text="/"/> 
  118.          <Button 
  119.              ohos:height="match_parent" 
  120.              ohos:width="match_parent" 
  121.              ohos:weight="1" 
  122.              ohos:text_size="50fp" 
  123.              ohos:background_element="$graphic:background_button_style" 
  124.              ohos:text_alignment="center" 
  125.              ohos:text="C"/> 
  126.      </DirectionalLayout> 
  127.      <DirectionalLayout 
  128.          ohos:height="match_parent" 
  129.          ohos:width="match_parent" 
  130.          ohos:weight="1" 
  131.          ohos:background_element="#00FFFF" 
  132.          ohos:orientation="horizontal"
  133.          <Button 
  134.              ohos:height="match_parent" 
  135.              ohos:width="match_parent" 
  136.              ohos:weight="1" 
  137.              ohos:text_size="50fp" 
  138.              ohos:background_element="$graphic:background_button_style" 
  139.              ohos:text_alignment="center" 
  140.              ohos:text_color="#455A64" 
  141.              ohos:text_weight="700" 
  142.              ohos:text="1"/> 
  143.          <Button 
  144.              ohos:height="match_parent" 
  145.              ohos:width="match_parent" 
  146.              ohos:weight="1" 
  147.              ohos:text_size="50fp" 
  148.              ohos:background_element="$graphic:background_button_style" 
  149.              ohos:text_alignment="center" 
  150.              ohos:text_color="#455A64" 
  151.              ohos:text_weight="700" 
  152.              ohos:text="2"/> 
  153.          <Button 
  154.              ohos:height="match_parent" 
  155.              ohos:width="match_parent" 
  156.              ohos:weight="1" 
  157.              ohos:text_size="50fp" 
  158.              ohos:background_element="$graphic:background_button_style" 
  159.              ohos:text_alignment="center" 
  160.              ohos:text_color="#455A64" 
  161.              ohos:text_weight="700" 
  162.              ohos:text="3"/> 
  163.          <Button 
  164.              ohos:height="match_parent" 
  165.              ohos:width="match_parent" 
  166.              ohos:weight="1" 
  167.              ohos:text_size="50fp" 
  168.              ohos:background_element="$graphic:background_button_style" 
  169.              ohos:text_alignment="center" 
  170.              ohos:text_color="#455A64" 
  171.              ohos:text_weight="700" 
  172.              ohos:text="0"/> 
  173.          <Button 
  174.              ohos:height="match_parent" 
  175.              ohos:width="match_parent" 
  176.              ohos:weight="2" 
  177.              ohos:text_size="50fp" 
  178.              ohos:background_element="$graphic:background_button_style" 
  179.              ohos:text_alignment="center" 
  180.              ohos:text="="/> 
  181.      </DirectionalLayout> 
  182.  </DirectionalLayou 

 4、預(yù)覽UI布局界面

5、UI界面布局組件完成后,接下來我將實現(xiàn)具體的操作業(yè)務(wù),這里僅羅列部分,詳情請查閱代碼。

1)首先對組件進(jìn)綁定

  1. //顯示表達(dá)式 
  2. private Text showMathExp = null
  3. //顯示錄入 
  4. private Text showInputValue = null
  5. //數(shù)字按鈕7 
  6. private Button btnServe = null
  7.  
  8. // ... 
  9.  
  10. //回退按鈕 
  11. private Image btnBack = null;  
  12.  
  13. @Override 
  14. public void onStart(Intent intent) { 
  15.     super.onStart(intent); 
  16.     super.setUIContent(ResourceTable.Layout_ability_main); 
  17.     showMathExp = (Text) findComponentById(ResourceTable.Id_show_math_expression); 
  18.     showInputValue = (Text) findComponentById(ResourceTable.Id_show_input_value); 
  19.      
  20.     //按鈕 
  21.     btnServe = (Button) findComponentById(ResourceTable.Id_btn_seven); 
  22.     // ... 
  23.     //回退 
  24.     btnBack = (Image) findComponentById(ResourceTable.Id_btn_back); 

2)按鈕的監(jiān)聽事件

 

  1. //點擊按鈕7的操作 
  2.        btnServe.setClickedListener(l -> { 
  3.            //TODO 現(xiàn)有表達(dá)式顯示區(qū)是否有內(nèi)容 
  4.             
  5.            //更改表達(dá)式顯示區(qū)內(nèi)容 
  6.            //showMathExp.setText(); 
  7.            //更改錄入數(shù)字顯示區(qū)內(nèi)容 
  8.            showInputValue.setText(7); 
  9.        }); 

 

 對于表達(dá)式顯示區(qū)、具體運算等業(yè)務(wù)邏輯可以直接查看代碼,此處不做詳細(xì)贅述。

我的HarmonyOS GitHub庫

©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請注明出處,否則將追究法律責(zé)任

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2022-07-11 16:19:22

css屬性鴻蒙

2022-01-24 11:02:27

PySimpleGUPython計算器

2015-07-08 15:30:59

智慧存儲華為

2024-07-26 16:17:22

2022-02-25 14:57:33

harmonyOSjava心形動畫

2021-02-05 15:29:22

鴻蒙HarmonyOS應(yīng)用

2016-12-12 13:41:37

iOS簡易加法開發(fā)

2014-01-13 15:00:51

InxiLinux硬件

2018-05-15 15:05:15

Linux桌面科學(xué)計算器

2022-08-09 16:01:24

應(yīng)用開發(fā)鴻蒙

2021-11-17 15:36:04

鴻蒙HarmonyOS應(yīng)用

2013-12-02 13:34:55

2011-09-16 14:13:15

Windows7計算器

2021-01-29 15:47:21

鴻蒙HarmonyOS云庫

2010-12-01 12:50:59

2024-01-31 08:33:06

C++編程計算器

2020-10-19 16:40:18

LinuxWindows計算器

2011-01-04 16:16:59

2010-08-18 11:26:56

CISCO路由器

2011-08-30 09:48:07

Ubuntu
點贊
收藏

51CTO技術(shù)棧公眾號