日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

harmonyOS智慧屏,在鸿蒙HarmonyOS智慧屏上实现一款粗糙的计算器

發布時間:2025/3/11 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 harmonyOS智慧屏,在鸿蒙HarmonyOS智慧屏上实现一款粗糙的计算器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在學習的路上我們不能只是停留在對理論知識的理解,還應該將理論和實戰進行結合,這樣才有利于我們能夠更有深度的掌握知識,最終形成自己的知識體系結構。我們在實戰的時候,不僅可以鞏固我們的理論知識,還能夠在實戰中發現問題,并找到合適的解決方案。

前面的章節中我們已經學習了六大布局和常用的組件,我們在學習布局和組件的時候也有一些小示例。通過這些小示例我們僅僅是對當前的布局和組件的使用有了深刻的認識,但UI界面布局中不可能單純只存在某個組件,復雜的UI界面中會出現多種布局、多種組件進行組合。本節我將在HarmonyOS智慧屏上實現常規的計算器。

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

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

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

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

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:orientation="vertical">

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="2"

ohos:orientation="vertical">

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="3"

ohos:orientation="vertical">

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

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="2"

ohos:orientation="vertical">

ohos:id="$+id:show_math_expression"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:background_element="#FFFFFF"

ohos:weight="3"

ohos:text="5+2+7-"

ohos:text_size="40fp"

ohos:text_alignment="right?|?vertical_center"

ohos:right_padding="20vp"/>

ohos:id="$+id:show_input_value"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:background_element="#F2F2F2"

ohos:weight="4"

ohos:text="1"

ohos:text_size="60fp"

ohos:text_alignment="right?|?vertical_center"

ohos:right_padding="20vp"/>

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

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="3"

ohos:orientation="vertical">

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:background_element="#FFFF00"

ohos:orientation="horizontal">

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="7"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="8"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="9"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text="+"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text="-"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:image_src="$media:delete"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:background_element="#FF00FF"

ohos:orientation="horizontal">

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="4"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="5"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="6"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text="*"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text="/"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text="C"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:background_element="#00FFFF"

ohos:orientation="horizontal">

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="1"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="2"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="3"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="1"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text_color="#455A64"

ohos:text_weight="700"

ohos:text="0"/>

ohos:height="match_parent"

ohos:width="match_parent"

ohos:weight="2"

ohos:text_size="50fp"

ohos:background_element="$graphic:background_button_style"

ohos:text_alignment="center"

ohos:text="="/>

4、預覽UI布局界面

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

1)首先對組件進綁定

//顯示表達式

private?Text?showMathExp?=?null;

//顯示錄入

private?Text?showInputValue?=?null;

//數字按鈕7

private?Button?btnServe?=?null;

//?...

//回退按鈕

private?Image?btnBack?=?null;

@Override

publicvoid?onStart(Intent?intent)?{

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

showMathExp?=?(Text)?findComponentById(ResourceTable.Id_show_math_expression);

showInputValue?=?(Text)?findComponentById(ResourceTable.Id_show_input_value);

//按鈕

btnServe?=?(Button)?findComponentById(ResourceTable.Id_btn_seven);

//?...

//回退

btnBack?=?(Image)?findComponentById(ResourceTable.Id_btn_back);

}

2)按鈕的監聽事件

//點擊按鈕7的操作

btnServe.setClickedListener(l?->?{

//TODO?現有表達式顯示區是否有內容

//更改表達式顯示區內容

//showMathExp.setText();

//更改錄入數字顯示區內容

showInputValue.setText(7);

});

對于表達式顯示區、具體運算等業務邏輯可以直接查看代碼,此處不做詳細贅述。

我的HarmonyOS GitHub庫

?著作權歸作者和HarmonyOS技術社區共同所有,如需轉載,請注明出處,否則將追究法律責任

【編輯推薦】

【責任編輯:jianghua TEL:(010)68476606】

點贊 0

總結

以上是生活随笔為你收集整理的harmonyOS智慧屏,在鸿蒙HarmonyOS智慧屏上实现一款粗糙的计算器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国内自拍av | 日韩视频一区二区三区在线播放免费观看 | 久久97视频 | 密臀av在线 | 日韩欧美精品在线播放 | 经典三级久久 | 亚洲av色香蕉一区二区三区 | 日韩在线导航 | 亚洲精品国产精品国自 | 午夜精品久久久久久久久久 | 久久久久草| 亚洲视频在线观看网址 | 免费看裸体视频网站 | 美女草逼视频 | 麻豆传媒在线免费 | 日韩欧美小视频 | 免费播放片大片 | 国产区在线 | 欧美成人乱码一二三四区免费 | 免费在线播放视频 | 国产免费观看av | 一区二区精彩视频 | 最新国产露脸在线观看 | 竹菊影视日韩一区二区 | 亚洲成人精品一区二区三区 | 日韩欧美123 | 国产精品人八做人人女人a级刘 | 丁香六月婷婷综合 | 在线观看av的网站 | 人妻av一区二区 | 免费观看黄色网页 | 伊人久久伊人 | 久久久久99精品成人片三人毛片 | 天堂在线观看中文字幕 | 亚洲黄色一区 | av一二区| 大地资源影视在线播放观看高清视频 | 浓精h攵女乱爱av | 色碰视频 | 熟女高潮一区二区三区 | 少妇高潮露脸国语对白 | 欧美日本在线播放 | 激情五月婷婷在线 | 尤物在线免费观看 | 91干视频| 日韩 国产 一区 | 日本伦理一区二区 | 日一区二区 | 欧美播放器| 久久久这里有精品 | 日本不卡视频在线播放 | 91天堂网 | 久久久久久久久久影院 | 男人用嘴添女人下身免费视频 | 欧美成人视屏 | 操操操爽爽爽 | 色综合91| 五月婷六月 | 人人妻人人澡人人爽精品日本 | 天堂中文网在线 | 亚洲熟伦熟女新五十路熟妇 | 色图插插插 | 波多野结衣一区二区三区免费视频 | www夜夜操 | 欧美一区二区三区在线免费观看 | 男女久久久 | 日本美女黄色一级片 | 成人xxx| 亚洲一区二区三区不卡视频 | 婷婷激情六月 | 免费黄色网址观看 | 亚洲国产精品国自产拍av | 中文字幕在线观看av | 久久爰 | 少妇裸体挤奶汁奶水视频 | 97在线免费公开视频 | 日韩xxx视频 | 亚洲 小说 欧美 激情 另类 | 日少妇的逼 | 自拍偷拍中文字幕 | 看毛片网 | 亚洲精品1区| 久久久人妻无码一区二区 | 四虎久久 | 亚洲成人激情av | 五月婷中文字幕 | 高清日韩一区二区 | 国产suv精品一区二区33 | 欧美丰满老熟妇xxxxx性 | 很黄很污的视频 | 亚洲综合91 | 91中文字幕在线播放 | 一区二区三区免费在线 | 日韩中文字幕视频在线 | 久久久久国产精品一区二区 | 天天干天天爱天天射 | 蜜桃视频无码区在线观看 | 亚洲欧洲日本精品 | 欧美成人精品在线观看 |