日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
在鴻蒙HarmonyOS智慧屏上實(shí)現(xiàn)一款粗糙的計(jì)算器

 [[374369]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos./#zz

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

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

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

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

 

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

1、整個(gè)布局分為上下兩個(gè)區(qū)域,因此DirectionalLayout布局的方向?yàn)関ertical(垂直)。并且分為兩個(gè)區(qū)域,上下區(qū)域占比為2:3。

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

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

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

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

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

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

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

1)首先對(duì)組件進(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)聽(tīng)事件

 

 
 
 
 
  1. //點(diǎn)擊按鈕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.        }); 

 

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

我的HarmonyOS GitHub庫(kù)

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

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos./#zz

 


分享文章:在鴻蒙HarmonyOS智慧屏上實(shí)現(xiàn)一款粗糙的計(jì)算器
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/djdphii.html