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

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

Android table布局開發實現簡單計算器

瀏覽:5日期:2022-09-24 11:24:19

本文實例為大家分享了Android table布局開發實現簡單計算器的具體代碼,供大家參考,具體內容如下

結果如圖:

Android table布局開發實現簡單計算器

XML文件如下:

<FrameLayout xmlns:android='http://schemas.android.com/apk/res/android'xmlns:tools='http://schemas.android.com/tools'android:id='@+id/container'android:layout_width='match_parent'android:layout_height='match_parent'tools:context='com.example.wxhcalculator.MainActivity'tools:ignore='MergeRootFrame' ><TableLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:stretchColumns='1'android:textSize='42sp' ><TableRow><EditTextandroid:id='@+id/result'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_span='4'android:background='@android:drawable/editbox_background'android:cursorVisible='false'android:editable='false'android:gravity='right|center_vertical'android:lines='1'android:textSize='60sp' /></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num7'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='7'android:textSize='42sp' /><Buttonandroid:id='@+id/num8'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='8'android:textSize='42sp' /><Buttonandroid:id='@+id/num9'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='9'android:textSize='42sp' /><Buttonandroid:id='@+id/divide'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='/'android:textSize='42sp' /></LinearLayout></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num4'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='4'android:textSize='42sp' /><Buttonandroid:id='@+id/num5'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='5'android:textSize='42sp' /><Buttonandroid:id='@+id/num6'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='6'android:textSize='42sp' /><Buttonandroid:id='@+id/multiply'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='*'android:textSize='42sp' /></LinearLayout></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num1'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='1'android:textSize='42sp' /><Buttonandroid:id='@+id/num2'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='2'android:textSize='42sp' /><Buttonandroid:id='@+id/num3'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='3'android:textSize='42sp' /><Buttonandroid:id='@+id/subtract'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='-'android:textSize='42sp' /></LinearLayout></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num0'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='0'android:textSize='42sp' /><Buttonandroid:id='@+id/point'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='.'android:textSize='42sp' /><Buttonandroid:id='@+id/add'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='+'android:textSize='42sp' /><Buttonandroid:id='@+id/equal'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='='android:textSize='42sp' /></LinearLayout></TableRow><TableRow><Buttonandroid:id='@+id/clear'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_span='4'android:gravity='center_vertical|center_horizontal'android:text='clear'android:textSize='30sp' /></TableRow></TableLayout></FrameLayout>

mainActivity主函數如下:

package com.example.wxhcalculator;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends ActionBarActivity {private Button[] btnNum = new Button[11];// 數值按鈕private Button[] btnCommand = new Button[5];// 符號按鈕private EditText editText = null;// 顯示區域private Button btnClear = null; // clear按鈕private String lastCommand; // 用于保存運算符private boolean clearFlag; // 用于判斷是否清空顯示區域的值,true需要,false不需要private boolean firstFlag; // 用于判斷是否是首次輸入,true首次,false不是首次private double result; // 計算結果public MainActivity() {// 初始化各項值result = 0; // x的值firstFlag = true; // 是首次運算clearFlag = false; // 不需要清空lastCommand = '='; // 運算符}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 獲取運算符btnCommand[0] = (Button) findViewById(R.id.add);btnCommand[1] = (Button) findViewById(R.id.subtract);btnCommand[2] = (Button) findViewById(R.id.multiply);btnCommand[3] = (Button) findViewById(R.id.divide);btnCommand[4] = (Button) findViewById(R.id.equal);// 獲取數字btnNum[0] = (Button) findViewById(R.id.num0);btnNum[1] = (Button) findViewById(R.id.num1);btnNum[2] = (Button) findViewById(R.id.num2);btnNum[3] = (Button) findViewById(R.id.num3);btnNum[4] = (Button) findViewById(R.id.num4);btnNum[5] = (Button) findViewById(R.id.num5);btnNum[6] = (Button) findViewById(R.id.num6);btnNum[7] = (Button) findViewById(R.id.num7);btnNum[8] = (Button) findViewById(R.id.num8);btnNum[9] = (Button) findViewById(R.id.num9);btnNum[10] = (Button) findViewById(R.id.point);// 初始化顯示結果區域editText = (EditText) findViewById(R.id.result);editText.setText('0.0');// 實例化監聽器對象NumberAction na = new NumberAction();CommandAction ca = new CommandAction();for (Button bc : btnCommand) {bc.setOnClickListener(ca);}for (Button bc : btnNum) {bc.setOnClickListener(na);}// clear按鈕的動作btnClear = (Button) findViewById(R.id.clear);btnClear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {editText.setText('0.0');// 初始化各項值result = 0; // x的值firstFlag = true; // 是首次運算clearFlag = false; // 不需要清空lastCommand = '='; // 運算符}});}// 數字按鈕監聽器private class NumberAction implements OnClickListener {@Overridepublic void onClick(View view) {Button btn = (Button) view;String input = btn.getText().toString();if (firstFlag) { // 首次輸入// 一上就'.',就什么也不做if (input.equals('.')) {return;}// 如果是'0.0'的話,就清空if (editText.getText().toString().equals('0.0')) {editText.setText('');}firstFlag = false;// 改變是否首次輸入的標記值} else {String editTextStr = editText.getText().toString();// 判斷顯示區域的值里面是否已經有'.',如果有,輸入的又是'.',就什么都不做if (editTextStr.indexOf('.') != -1 && input.equals('.')) {return;}// 判斷顯示區域的值里面只有'-',輸入的又是'.',就什么都不做if (editTextStr.equals('-') && input.equals('.')) {return;}// 判斷顯示區域的值如果是'0',輸入的不是'.',就什么也不做if (editTextStr.equals('0') && !input.equals('.')) {return;}}// 如果我點擊了運算符以后,再輸入數字的話,就要清空顯示區域的值if (clearFlag) {editText.setText('');clearFlag = false;// 還原初始值,不需要清空}editText.setText(editText.getText().toString() + input);// 設置顯示區域的值}}// 符號按鈕監聽器private class CommandAction implements OnClickListener {@Overridepublic void onClick(View view) {Button btn = (Button) view;String inputCommand = (String) btn.getText();if (firstFlag) {// 首次輸入'-'的情況if (inputCommand.equals('-')) {editText.setText('-');// 顯示區域的內容設置為'-'firstFlag = false;// 改變首次輸入的標記}} else {if (!clearFlag) {// 如果flag=false不需要清空顯示區的值,就調用方法計算calculate(Double.parseDouble(editText.getText().toString()));// 保存顯示區域的值,并計算}// 保存你點擊的運算符lastCommand = inputCommand;clearFlag = true;// 因為我這里已經輸入過運算符,}}}// 計算用的方法private void calculate(double x) {if (lastCommand.equals('+')) {result += x;} else if (lastCommand.equals('-')) {result -= x;} else if (lastCommand.equals('*')) {result *= x;} else if (lastCommand.equals('/')) {result /= x;} else if (lastCommand.equals('=')) {result = x;}editText.setText('' + result);}}

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

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

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

標簽: Android
相關文章:
主站蜘蛛池模板: 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | 防爆正压柜厂家_防爆配电箱_防爆控制箱_防爆空调_-盛通防爆 | 水性漆|墙面漆|木器家具漆|水漆涂料_晨阳水漆官网 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 槽钢冲孔机,槽钢三面冲,带钢冲孔机-山东兴田阳光智能装备股份有限公司 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | 岩棉切条机厂家_玻璃棉裁条机_水泥基保温板设备-廊坊鹏恒机械 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | ZHZ8耐压测试仪-上海胜绪电气有限公司 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 北京乾茂兴业科技发展有限公司 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 武汉画册印刷厂家-企业画册印刷-画册设计印刷制作-宣传画册印刷公司 - 武汉泽雅印刷厂 | TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 菏泽知彼网络科技有限公司| TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 济南冷库安装-山东冷库设计|建造|冷库维修-山东齐雪制冷设备有限公司 | 酵素生产厂家_酵素OEM_酵素加盟_酵素ODM_酵素原料厂家_厦门益力康 | 尊享蟹太太美味,大闸蟹礼卡|礼券|礼盒在线预订-蟹太太官网 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 预制直埋蒸汽保温管-直埋管道-聚氨酯发泡保温管厂家 - 唐山市吉祥保温工贸有限公司 | 木材烘干机,木炭烘干机,纸管/佛香烘干设备-河南蓝天机械制造有限公司 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 超声波清洗机-超声波清洗设备定制生产厂家 - 深圳市冠博科技实业有限公司 | 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 安徽合肥项目申报咨询公司_安徽合肥高新企业项目申报_安徽省科技项目申报代理 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 科研ELISA试剂盒,酶联免疫检测试剂盒,昆虫_植物ELISA酶免试剂盒-上海仁捷生物科技有限公司 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | 哈尔滨发电机,黑龙江柴油发电机组-北方星光 |