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

您的位置:首頁技術(shù)文章
文章詳情頁

Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器

瀏覽:174日期:2022-09-24 18:28:12

這里是用線性布局實現(xiàn)的計算器,為防止以后再回顧知識代碼找不到,特將代碼貼在這里:

xml文件的布局代碼:

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.MainActivity'> <EditText android: android:layout_width='match_parent' android:layout_height='100dp' android:paddingBottom='5dp' android:paddingRight='5dp' android:textSize='50sp' android:gravity='right' android:textColor='#00ff00'/> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_marginTop='30dp' android:orientation='horizontal' android:gravity='center_horizontal'> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='C' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' android:textColor='#ff0000'/> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='Ba' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='+' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='-' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:orientation='horizontal' android:gravity='center_horizontal' > <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='7' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='8' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='9' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='×' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:orientation='horizontal' android:gravity='center_horizontal' > <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='4' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='5' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='6' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='÷' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:orientation='horizontal' android:layout_marginTop='10dp' android:gravity='center_horizontal'> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='vertical' > <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='horizontal' > <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='1' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='2' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='3' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='horizontal' android:layout_marginTop='10dp'> <Button android:layout_width='170dp' android:layout_height='80dp' android: android:text='0' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='.' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> </LinearLayout> <Button android: android:layout_width='80dp' android:layout_height='170dp' android:layout_marginLeft='10dp' android:text='=' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' android:gravity='center' android:background='@color/colorAccent'/> </LinearLayout></LinearLayout>

Activity.java文件實現(xiàn)功能的代碼如下:

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class Main2Activity extends AppCompatActivity implements View.OnClickListener{ //創(chuàng)建Button對象 也就是activity_main.xml里所設(shè)置的ID Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt; Button btn_mul,btn_div,btn_add,btn_sub; Button btn_clr,btn_del,btn_eq; EditText et_input; boolean clr_flag; //判斷et編輯文本框中是否清空 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //實例化對象 setContentView(R.layout.activity_main2); btn_0= (Button) findViewById(R.id.btn_0); btn_1= (Button) findViewById(R.id.btn_1); btn_2= (Button) findViewById(R.id.btn_2); btn_3= (Button) findViewById(R.id.btn_3); btn_4= (Button) findViewById(R.id.btn_4); btn_5= (Button) findViewById(R.id.btn_5); btn_6= (Button) findViewById(R.id.btn_6); btn_7= (Button) findViewById(R.id.btn_7); btn_8= (Button) findViewById(R.id.btn_8); btn_9= (Button) findViewById(R.id.btn_9); btn_pt= (Button) findViewById(R.id.btn_pt); btn_add= (Button) findViewById(R.id.btn_add); btn_sub= (Button) findViewById(R.id.btn_sub); btn_mul= (Button) findViewById(R.id.btn_mul); btn_div= (Button) findViewById(R.id.btn_div); btn_clr= (Button) findViewById(R.id.btn_clr); btn_del= (Button) findViewById(R.id.btn_del); btn_eq= (Button) findViewById(R.id.btn_eq); et_input= (EditText) findViewById(R.id.et_input); //給按鈕設(shè)置的點擊事件 btn_0.setOnClickListener(this); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_pt.setOnClickListener(this); btn_add.setOnClickListener(this); btn_sub.setOnClickListener(this); btn_mul.setOnClickListener(this); btn_div.setOnClickListener(this); btn_clr.setOnClickListener(this); btn_del.setOnClickListener(this); btn_eq.setOnClickListener(this); } @Override public void onClick(View v) { String str=et_input.getText().toString(); switch (v.getId()){ case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_pt: if(clr_flag){ clr_flag=false; str=''; et_input.setText(''); } et_input.setText(str+((Button)v).getText()); break; case R.id.btn_add: case R.id.btn_sub: case R.id.btn_mul: case R.id.btn_div: if(clr_flag){ clr_flag=false; str=''; et_input.setText(''); } if(str.contains('+')||str.contains('-')||str.contains('×')||str.contains('÷')) { str=str.substring(0,str.indexOf(' ')); } et_input.setText(str+' '+((Button)v).getText()+' '); break; case R.id.btn_clr: if(clr_flag) clr_flag=false; str=''; et_input.setText(''); break; case R.id.btn_del: //判斷是否為空,然后在進行刪除 if(clr_flag){ clr_flag=false; str=''; et_input.setText(''); } else if(str!=null&&!str.equals('')){ et_input.setText(str.substring(0,str.length()-1)); } break; case R.id.btn_eq: //單獨運算最后結(jié)果 getResult();//調(diào)用下面的方法 break; } } private void getResult() { String exp=et_input.getText().toString(); if(exp==null||exp.equals('')) return ; //因為沒有運算符所以不用運算 if(!exp.contains(' ')){ return ; } if(clr_flag){ clr_flag=false; return; } clr_flag=true; //截取運算符前面的字符串 String s1=exp.substring(0,exp.indexOf(' ')); //截取的運算符 String op=exp.substring(exp.indexOf(' ')+1,exp.indexOf(' ')+2); //截取運算符后面的字符串 String s2=exp.substring(exp.indexOf(' ')+3); double cnt=0; if(!s1.equals('')&&!s2.equals('')){ double d1=Double.parseDouble(s1); double d2=Double.parseDouble(s2); if(op.equals('+')){ cnt=d1+d2; } if(op.equals('-')){ cnt=d1-d2; } if(op.equals('×')){ cnt=d1*d2; } if(op.equals('÷')){ if(d2==0) cnt=0; else cnt=d1/d2; } if(!s1.contains('.')&&!s2.contains('.')&&!op.equals('÷')) { int res = (int) cnt; et_input.setText(res+''); }else { et_input.setText(cnt+'');} } //如果s1是空 s2不是空 就執(zhí)行下一步 else if(!s1.equals('')&&s2.equals('')){ double d1=Double.parseDouble(s1); if(op.equals('+')){ cnt=d1; } if(op.equals('-')){ cnt=d1; } if(op.equals('×')){ cnt=0; } if(op.equals('÷')){ cnt=0; } if(!s1.contains('.')) { int res = (int) cnt; et_input.setText(res+''); }else { et_input.setText(cnt+'');} } //如果s1是空 s2不是空 就執(zhí)行下一步 else if(s1.equals('')&&!s2.equals('')){ double d2=Double.parseDouble(s2); if(op.equals('+')){ cnt=d2; } if(op.equals('-')){ cnt=0-d2; } if(op.equals('×')){ cnt=0; } if(op.equals('÷')){ cnt=0; } if(!s2.contains('.')) { int res = (int) cnt; et_input.setText(res+''); }else { et_input.setText(cnt+'');} } else { et_input.setText(''); } }}

結(jié)果圖如下所示:

Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器

更多計算器功能實現(xiàn),請點擊專題: 計算器功能匯總 進行學(xué)習(xí)

關(guān)于Android計算器功能的實現(xiàn),查看專題:Android計算器 進行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 暴风影音| 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | 防爆正压柜厂家_防爆配电箱_防爆控制箱_防爆空调_-盛通防爆 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 辐射色度计-字符亮度测试-反射式膜厚仪-苏州瑞格谱光电科技有限公司 | 一体化污水处理设备-一体化净水设备-「山东梦之洁水处理」 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 钣金加工厂家-钣金加工-佛山钣金厂-月汇好 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 红外光谱仪维修_二手红外光谱仪_红外压片机_红外附件-天津博精仪器 | 东莞办公家具厂家直销-美鑫【免费3D效果图】全国办公桌/会议桌定制 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | 电动百叶窗,开窗器,电动遮阳百叶,电动开窗机生产厂家-徐州鑫友工控科技发展有限公司 | 预制围墙_工程预制围墙_天津市瑞通建筑材料有限公司 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 泰安塞纳春天装饰公司【网站】 | 工装定制/做厂家/公司_工装订做/制价格/费用-北京圣达信工装 | 德国进口电锅炉_商用电热水器_壁挂炉_电采暖器_电热锅炉[德国宝] | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 整车VOC采样环境舱-甲醛VOC预处理舱-多舱法VOC检测环境仓-上海科绿特科技仪器有限公司 | RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 打孔器,打孔钳厂家【温州新星德牌五金工具】 | 山东信蓝建设有限公司官网 | LOGO设计_品牌设计_VI设计 - 特创易 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 制氮设备_PSA制氮机_激光切割制氮机_氮气机生产厂家-苏州西斯气体设备有限公司 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 必胜高考网_全国高考备考和志愿填报信息平台 |