电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

android簡易計算器的制作

瀏覽:8日期:2022-09-23 09:57:31

之前有好好完成老師留過的C++大作業,使用MFC制作通訊錄。所以用AS寫一個安卓的計算器并不是很難,但還是想上手操作一下,寫一個只有簡單加減乘除運算的小計算器,后面可能會考慮加一些其他的稍微復雜的計算功能。下面是步驟。

1.首先創建一個empty activity,取名為MyStudyCalculator。

2.打開activity_main.xml文件,創建兩個編輯框(EditText)、四個按鈕(Button)、一個文本框(TextView),并設置相應的id。其中編輯框作用是讓用戶填入兩個數字,四個按鈕分別對應四種不同的運算(需要對按鈕分別添加響應事件),文本框用于顯示運算結果。我另外添加了兩個文本框,一個用于顯示標題,一個顯示作者,對于該計算器來說沒有任何作用。下面給出代碼:

//第一個數字 <EditText android: android:layout_width='85dp' android:layout_height='wrap_content' android:layout_marginEnd='56dp' android:layout_marginStart='8dp' android:layout_marginTop='168dp' android:hint='@string/num1' app:layout_constraintEnd_toStartOf='@+id/second' app:layout_constraintHorizontal_bias='0.621' app:layout_constraintStart_toStartOf='parent' app:layout_constraintTop_toTopOf='parent' /> //第二個數字 <EditText android: android:layout_width='85dp' android:layout_height='wrap_content' android:layout_marginEnd='64dp' android:layout_marginTop='168dp' android:hint='@string/num2' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintTop_toTopOf='parent' /> //第二個數字 <TextView android: android:layout_width='96dp' android:layout_height='33dp' android:layout_marginBottom='84dp' android:layout_marginEnd='8dp' android:layout_marginStart='8dp' android:gravity='center' app:layout_constraintBottom_toBottomOf='parent' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintHorizontal_bias='0.481' app:layout_constraintStart_toStartOf='parent' /> //加法按鈕 <Button android: android:layout_width='50dp' android:layout_height='50dp' android:layout_marginEnd='8dp' android:layout_marginStart='8dp' android:layout_marginTop='260dp' android:onClick='SUM' android:text='+' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintHorizontal_bias='0.391' app:layout_constraintStart_toStartOf='parent' app:layout_constraintTop_toTopOf='parent' /> //減法按鈕 <Button android: android:layout_width='50dp' android:layout_height='50dp' android:layout_marginBottom='30dp' android:layout_marginEnd='148dp' android:layout_marginTop='8dp' android:onClick='SUB' android:text='-' app:layout_constraintBottom_toBottomOf='parent' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintTop_toTopOf='parent' app:layout_constraintVertical_bias='0.595' /> //乘法按鈕 <Button android: android:layout_width='50dp' android:layout_height='50dp' android:layout_marginBottom='152dp' android:layout_marginEnd='8dp' android:layout_marginStart='8dp' android:onClick='MUL' android:text='*' app:layout_constraintBottom_toBottomOf='parent' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintHorizontal_bias='0.393' app:layout_constraintStart_toStartOf='parent' /> //除法按鈕 <Button android: android:layout_width='50dp' android:layout_height='50dp' android:layout_marginBottom='8dp' android:layout_marginEnd='148dp' android:layout_marginTop='8dp' android:onClick='DIV' android:text='/' app:layout_constraintBottom_toBottomOf='parent' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintTop_toTopOf='parent' app:layout_constraintVertical_bias='0.678' /> //標題 <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginEnd='8dp' android:layout_marginStart='8dp' android:layout_marginTop='36dp' android:text='丑陋的而且只能算加減乘除的計算機' android:textSize='20dp' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintStart_toStartOf='parent' app:layout_constraintTop_toTopOf='parent' /> //作者 <TextView android: android:layout_width='wrap_content' android:layout_height='11dp' android:layout_marginEnd='8dp' android:layout_marginStart='8dp' android:layout_marginTop='496dp' android:text='TimberWolf' android:textSize='10dp' app:layout_constraintEnd_toEndOf='parent' app:layout_constraintHorizontal_bias='0.99' app:layout_constraintStart_toStartOf='parent' app:layout_constraintTop_toTopOf='parent' />

3.打開MainActivity.java寫四個按鈕對應的方法,代碼如下:

//加法 public void SUM(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 + num2; res.setText(String.valueOf(ans)); } //減法 public void SUB(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 - num2; res.setText(String.valueOf(ans)); } //乘法 public void MUL(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 * num2; res.setText(String.valueOf(ans)); } //除法 public void DIV(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 / num2; res.setText(String.valueOf(ans)); }

4.看似代碼很長,其實都是一樣的,很簡單就完成了,其中MainActivity.java中的代碼我沒有給出注釋,就是幾種類型的轉換。歡迎大佬指點,初學者不懂的可以給我留言。下面給出仿真機運行效果。

android簡易計算器的制作

android簡易計算器的制作

更多計算器功能實現,請點擊專題: 計算器功能匯總 進行學習

關于Android計算器功能的實現,查看專題:Android計算器 進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 磁力加热搅拌器-多工位|大功率|数显恒温磁力搅拌器-司乐仪器官网 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 防勒索软件_数据防泄密_Trellix(原McAfee)核心代理商_Trellix(原Fireeye)售后-广州文智信息科技有限公司 | 冻干机(冷冻干燥机)_小型|实验型|食品真空冷冻干燥机-松源 | 净化板-洁净板-净化板价格-净化板生产厂家-山东鸿星新材料科技股份有限公司 | 地磅-地秤-江阴/无锡地磅-江阴天亿计量设备有限公司_ | 运动木地板厂家,篮球场木地板品牌,体育场馆木地板安装 - 欧氏运动地板 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | YAGEO国巨电容|贴片电阻|电容价格|三星代理商-深圳市巨优电子有限公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | DAIKIN电磁阀-意大利ATOS电磁阀-上海乾拓贸易有限公司 | 传动滚筒,改向滚筒-淄博建凯机械科技有限公司 | 微型气泵-真空-蠕动-水泵-厂家-深圳市品亚科技有限公司 | 防水套管厂家_刚性防水套管_柔性防水套管_不锈钢防水套管-郑州中泰管道 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司 | 青岛侦探调查_青岛侦探事务所_青岛调查事务所_青岛婚外情取证-青岛狄仁杰国际侦探公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 全国冰箱|空调|洗衣机|热水器|燃气灶维修服务平台-百修家电 | 日本东丽膜_反渗透膜_RO膜价格_超滤膜_纳滤膜-北京东丽阳光官网 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 扒渣机厂家_扒渣机价格_矿用扒渣机_铣挖机_撬毛台车_襄阳永力通扒渣机公司 | PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 天津电机维修|水泵维修-天津晟佳机电设备有限公司 | 低温柔性试验仪-土工布淤堵-沥青车辙试验仪-莱博特(天津)试验机有限公司 | LED灯杆屏_LED广告机_户外LED广告机_智慧灯杆_智慧路灯-太龙智显科技(深圳)有限公司 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 威廉希尔WilliamHill·足球(中国)体育官方网站| 氨水-液氨-工业氨水-氨水生产厂家-辽宁顺程化工 | 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 |