




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、100%运行通过这是自己编写的,通过eclipse运行简易计算器Android版(源码)这是自己整理得,欢迎大家下载说明:每完成一次计算后,需先clear 清除记忆,当然也可以连算XML布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orie
2、ntation="vertical" > <TableLayout android:id="+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="4" > <TableRow android:id="+id/tableRow_et" android:layout_width=&
3、quot;fill_parent" android:layout_height="fill_parent" > <EditText android:id="+id/et" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_span="4" android:focusable="false" android:singleLine=&qu
4、ot;true" android:inputType="text" android:gravity="right"> </EditText> </TableRow> <TableRow android:id="+id/tableRow1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id
5、/bt_7" android:layout_width="40px" android:layout_height="40px" android:text="7" /> <Button android:id="+id/bt_8" android:layout_width="40px" android:layout_height="40px" android:text="8" /> <Button android:id=&qu
6、ot;+id/bt_9" android:layout_width="40px" android:layout_height="40px" android:text="9" /> <Button android:id="+id/bt_back" android:layout_width="40px" android:layout_height="40px" android:text="back" /> </TableRow
7、> <TableRow android:id="+id/tableRow2" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/bt_4" android:layout_width="40px" android:layout_height="40px" android:text="4" /&
8、gt; <Button android:id="+id/bt_5" android:layout_width="40px" android:layout_height="40px" android:text="5" /> <Button android:id="+id/bt_6" android:layout_width="40px" android:layout_height="40px" android:text="6&qu
9、ot; /> <Button android:id="+id/bt_divide" android:layout_width="40px" android:layout_height="40px" android:text="/" /> </TableRow> <TableRow android:id="+id/tableRow3" android:layout_width="fill_parent" android:layout_heig
10、ht="fill_parent" > <Button android:id="+id/bt_1" android:layout_width="40px" android:layout_height="40px" android:text="1" /> <Button android:id="+id/bt_2" android:layout_width="40px" android:layout_height="40px&q
11、uot; android:text="2" /> <Button android:id="+id/bt_3" android:layout_width="40px" android:layout_height="40px" android:text="3" /> <Button android:id="+id/bt_multiply" android:layout_width="40px" android:layout_height
12、="40px" android:text="*" /> </TableRow> <TableRow android:id="+id/tableRow4" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/bt_0" android:layout_width="50px" andr
13、oid:layout_height="40px" android:text="0" /> <Button android:id="+id/bt_point" android:layout_width="50px" android:layout_height="40px" android:text="." /> <Button android:id="+id/bt_add" android:layout_width="50p
14、x" android:layout_height="40px" android:text="+" /> <Button android:id="+id/bt_sub" android:layout_width="50px" android:layout_height="40px" android:text="-" /> </TableRow> <TableRow android:id="+id/tableRow4&quo
15、t; android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="+id/bt_equal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_span="3" android:text="=" /&
16、gt; <Button android:id="+id/bt_clear" android:layout_width="50px" android:layout_height="40px" android:text="clear" /> </TableRow> </TableLayout></LinearLayout>源代码:package android.sdk;import android.app.Activity;import android.os.Bundle
17、;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class Android_calculatorActivity extends Activityprivate Button bt_1;private Button bt_2;private Button bt_3;private Button bt_4;private B
18、utton bt_5;private Button bt_6;private Button bt_7;private Button bt_8;private Button bt_9;private Button bt_0;private Button bt_add;private Button bt_sub; / 减private Button bt_multiply; / 乘private Button bt_divide; / 除private Button bt_back;private Button bt_equal; / 等于private Button bt_point; / 点p
19、rivate Button bt_clear; / 清除private EditText et_play; / 显示private String str_oper = "+" / 运算符private StringBuffer str_display = new StringBuffer(); / 显示private String str_result; / 结果显示private double num1;private double num2;private boolean flag = true; / 小数点个数开关控制;private boolean b_sub, b
20、_mul, b_div; / 运算符开关控制Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);bt_0 = (Button) findViewById(R.id.bt_0);bt_1 = (Button) findViewById(R.id.bt_1);bt_2 = (Button) findViewById(R.id.bt_2);bt_3 = (Button) findViewById(R.id.bt_3
21、);bt_4 = (Button) findViewById(R.id.bt_4);bt_5 = (Button) findViewById(R.id.bt_5);bt_6 = (Button) findViewById(R.id.bt_6);bt_7 = (Button) findViewById(R.id.bt_7);bt_8 = (Button) findViewById(R.id.bt_8);bt_9 = (Button) findViewById(R.id.bt_9);bt_add = (Button) findViewById(R.id.bt_add);bt_sub = (Butt
22、on) findViewById(R.id.bt_sub);bt_multiply = (Button) findViewById(R.id.bt_multiply);bt_divide = (Button) findViewById(R.id.bt_divide);bt_back = (Button) findViewById(R.id.bt_back);bt_equal = (Button) findViewById(R.id.bt_equal);bt_point = (Button) findViewById(R.id.bt_point);bt_clear = (Button) find
23、ViewById(R.id.bt_clear);et_play = (EditText) findViewById(R.id.et);et_play.setText("0.0");bt_0.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("0");et_play.setText(str_display.toString(););bt_1.setOnClickListener(new OnClickListener()
24、Overridepublic void onClick(View v)str_display.append("1");et_play.setText(str_display.toString(););bt_2.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("2");et_play.setText(str_display.toString(););bt_3.setOnClickListener(new OnClick
25、Listener()Overridepublic void onClick(View v)str_display.append("3");et_play.setText(str_display.toString(););bt_4.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("4");et_play.setText(str_display.toString(););bt_5.setOnClickListener(n
26、ew OnClickListener()Overridepublic void onClick(View v)str_display.append("5");et_play.setText(str_display.toString(););bt_6.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("6");et_play.setText(str_display.toString(););bt_7.setOnClick
27、Listener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("7");et_play.setText(str_display.toString(););bt_8.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("8");et_play.setText(str_display.toString(););bt_9.
28、setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append("9");et_play.setText(str_display.toString(););bt_point.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)if (flag)str_display.append(".");flag = false;);bt_back.
29、setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)if (str_display.length() != 0)str_display.deleteCharAt(str_display.length() - 1);et_play.setText(str_display.toString(););bt_add.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_oper = "+&qu
30、ot;if (!(str_display.toString() = "")num1 += Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_sub.setOnClickListener(n
31、ew OnClickListener()Overridepublic void onClick(View v)str_oper = "-"if (!b_sub && !(str_display.toString() = "")num1 = Double.parseDouble(str_display.toString();et_play.setText(String.valueOf(num1);str_display = new StringBuffer("");b_sub = true; elseif (!(str_
32、display.toString() = "")num1 -= Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_multiply.setOnClickListener(new OnCli
33、ckListener()Overridepublic void onClick(View v)str_oper = "*"if (!b_mul && !(str_display.toString() = "")num1 = Double.parseDouble(str_display.toString();et_play.setText(String.valueOf(num1);str_display = new StringBuffer("");b_mul = true; elseif (!(str_display.
34、toString() = "")num1 *= Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_divide.setOnClickListener(new OnClickListener
35、()Overridepublic void onClick(View v)str_oper = "/"if (!b_div && !(str_display.toString() = "")num1 = Double.parseDouble(str_display.toString();et_play.setText(String.valueOf(num1);str_display = new StringBuffer("");b_div = true; elseif (!(str_display.toString()
36、 = "")if (Double.parseDouble(str_display.toString() = 0)Toast.makeText(Android_calculatorActivity.this,"除数不能为0!", Toast.LENGTH_LONG).show(); elsenum1 /= Double.parseDouble(str_display.toString();str_display = new StringBuffer("");if (!(str_result = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;);bt_clear.setOnClickListener(new OnClickListener()Ov
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 垃圾发电厂炉渣扩建项目建议书(模板范文)
- 跨境金融保障的具体执行方案
- 交通噪声屏障工程实施方案
- 供水管网老旧设施更换工程可行性研究报告(范文参考)
- 工业园区水环境综合整治项目建议书(模板范文)
- 手抄报设计教学
- 室内设计原理讲解
- 郑州经贸学院《高层建筑设计专题》2023-2024学年第二学期期末试卷
- 广东南方职业学院《体育市场营销与策划》2023-2024学年第二学期期末试卷
- 西安翻译学院《外汇实务》2023-2024学年第二学期期末试卷
- 新乡市欣丰瑞拓天然资源有限公司 现代化环保型骨料生产线项目环境影响报告
- 小区业委会工作情况汇报及下一步工作计划
- 个人借条电子版模板
- 2023年广东省中考物理试卷分析
- 团体体检报告格式模板范文
- 成人经鼻胃管喂养临床实践指南
- 过程控制实验指导书讲解
- 安徽钢结构人行天桥施工方案
- 形势与政策(吉林大学)智慧树知到答案章节测试2023年
- 《表观遗传》教学设计
- 黎民公共管理学考研笔记
评论
0/150
提交评论