计算器Android版(源码).doc

上传人:豆**** 文档编号:35289047 上传时间:2022-08-21 格式:DOC 页数:10 大小:91KB
返回 下载 相关 举报
计算器Android版(源码).doc_第1页
第1页 / 共10页
计算器Android版(源码).doc_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《计算器Android版(源码).doc》由会员分享,可在线阅读,更多相关《计算器Android版(源码).doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、如有侵权,请联系网站删除,仅供学习与交流计算器Android版(源码)【精品文档】第 10 页100%运行通过这是自己编写的,通过eclipse运行简易计算器Android版(源码)这是自己整理得,欢迎大家下载说明:每完成一次计算后,需先clear 清除记忆,当然也可以连算XML布局 源代码:package android.sdk;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android

2、.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 Button bt_5;private Button bt_6;private Button bt_7;private Button bt_8;private Bu

3、tton 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; / 点private Button bt_clear; / 清除private EditText et_play; / 显示private String str_oper

4、 = +; / 运算符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_mul, b_div; / 运算符开关控制Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(

5、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);bt_4 = (Button) findViewById(R.id.bt_4);bt_5 = (Button) findViewById(R.id.bt_5);bt_6 = (Bu

6、tton) 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 = (Button) findViewById(R.id.bt_sub);bt_multiply = (Button) findViewById(R.id.bt_multiply);bt_divid

7、e = (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) findViewById(R.id.bt_clear);et_play = (EditText) findViewById(R.id.et);et_play.setText(0.0);bt_0

8、.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append(0);et_play.setText(str_display.toString();bt_1.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append(1);et_play.setText(str_display.toString();bt_2.setOnClickListene

9、r(new OnClickListener()Overridepublic void onClick(View v)str_display.append(2);et_play.setText(str_display.toString();bt_3.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append(3);et_play.setText(str_display.toString();bt_4.setOnClickListener(new OnClickListe

10、ner()Overridepublic void onClick(View v)str_display.append(4);et_play.setText(str_display.toString();bt_5.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_display.append(5);et_play.setText(str_display.toString();bt_6.setOnClickListener(new OnClickListener()Overridepubli

11、c void onClick(View v)str_display.append(6);et_play.setText(str_display.toString();bt_7.setOnClickListener(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(Vie

12、w v)str_display.append(8);et_play.setText(str_display.toString();bt_9.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)s

13、tr_display.append(.);flag = false;bt_back.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 v

14、oid onClick(View v)str_oper = +;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.setOnClickListene

15、r(new 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_display.toString() = )num1 -= Double.pars

16、eDouble(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 OnClickListener()Overridepublic void onClick(View v)str_oper = *;if (!b_

17、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.toString() = )num1 *= Double.parseDouble(str_display.toString();str_display = new StringBuffer();if (!(str_resu

18、lt = null)num1 = Double.parseDouble(str_result);str_result = null;et_play.setText(String.valueOf(num1);flag = true;bt_divide.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_oper = /;if (!b_div & !(str_display.toString() = )num1 = Double.parseDouble(str_display.toString

19、();et_play.setText(String.valueOf(num1);str_display = new StringBuffer();b_div = true; elseif (!(str_display.toString() = )if (Double.parseDouble(str_display.toString() = 0)Toast.makeText(Android_calculatorActivity.this,除数不能为0!, Toast.LENGTH_LONG).show(); elsenum1 /= Double.parseDouble(str_display.t

20、oString();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()Overridepublic void onClick(View v)str_oper = +;str_display = new StringBuffer(

21、);str_result = null;num1 = 0;num2 = 0;flag = true;b_sub = false;b_mul = false;b_div = false;et_play.setText(0.0);bt_equal.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)if (str_oper.equals(+)num2 = Double.parseDouble(str_display.toString();str_result = String.valueOf(num1

22、 + num2);et_play.setText(str_result);str_display = new StringBuffer();if (str_oper.equals(-)num2 = Double.parseDouble(str_display.toString();str_result = String.valueOf(num1 - num2);et_play.setText(str_result);str_display = new StringBuffer();if (str_oper.equals(*)num2 = Double.parseDouble(str_displ

23、ay.toString();str_result = String.valueOf(num1 * num2);et_play.setText(str_result);str_display = new StringBuffer();if (str_oper.equals(/)num2 = Double.parseDouble(str_display.toString();if (!(num2 = 0)str_result = String.valueOf(num1 / num2);et_play.setText(str_result); elseToast.makeText(Android_calculatorActivity.this,除数不能为0!, Toast.LENGTH_LONG).show();str_display = new StringBuffer();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁