




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 盲盒商品销售市场调研及营销推广合作协议
- 影视后期制作实时渲染农场共享合同
- 航空航天科普教育教材编写与师资培训服务合同
- 动画渲染节点租赁与安全数据存储服务协议
- 高精度工业机器人示教器租赁与智能化升级服务协议
- 电商平台店铺账号及运营团队交接协议书
- 大型体育盛事官方合作伙伴授权书
- 《婚姻忠诚承诺与子女共同抚养权协议书》
- 网红奶茶店品牌合作连锁经营及区域代理合同
- 儿童绘本馆品牌合作运营管理合同
- 2025年丹江口水力发电厂招聘笔试参考题库含答案解析
- 住宅室内装饰装修管理办法
- 外科感染-有芽孢厌氧菌感染(外科课件)
- 物业服务重点难点分析
- 模块项目化活页式教材模板
- 山东省威海市环翠区实验小学2023-2024学年四年级下学期期中测试语文试题
- 储能设备故障诊断技术
- 2024年贵州省公务员考试《行测》真题及答案解析
- 2022-2024北京初二一模生物汇编:实验探究题
- 《重庆钢铁破产重整案例分析》
- 【工程法规】王欣 教材精讲班课件 39-第6章-6.4-施工现场安全防护制度
评论
0/150
提交评论