收藏 分销(赏)

计算器Android版(源码).doc

上传人:1587****927 文档编号:1660957 上传时间:2024-05-07 格式:DOC 页数:12 大小:265KB
下载 相关 举报
计算器Android版(源码).doc_第1页
第1页 / 共12页
计算器Android版(源码).doc_第2页
第2页 / 共12页
计算器Android版(源码).doc_第3页
第3页 / 共12页
计算器Android版(源码).doc_第4页
第4页 / 共12页
计算器Android版(源码).doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

1、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.widget.Button;import android.widget.EditText;i

2、mport 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 Button bt_9;private Button bt_0;private Button bt

3、_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 = +; / 运算符private StringBuffer str_display = n

4、ew 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(savedInstanceState);setContentView(R.layout.mai

5、n);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 = (Button) findViewById(R.id.bt_6);bt_7 = (Button) f

6、indViewById(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_divide = (Button) findViewById(R.id.bt_divide);bt_ba

7、ck = (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.setOnClickListener(new OnClickListener()Overri

8、depublic 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.setOnClickListener(new OnClickListener()Overridepublic void

9、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 OnClickListener()Overridepublic void onClick(View v

10、)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()Overridepublic void onClick(View v)str_display.a

11、ppend(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(View v)str_display.append(8);et_pl

12、ay.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)str_display.append(.);flag =

13、 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 void onClick(View v)str_

14、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.setOnClickListener(new OnClickListener

15、()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.parseDouble(str_display.t

16、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_multiply.setOnClickListener(new OnClickListener()Overridepublic void onClick(View v)str_oper = *;if (!b_mul & !(str_display

17、.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_result = null)num1 = Do

18、uble.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();et_play.setTex

19、t(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.toString();str_dis

20、play = 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();str_result =

21、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 + num2);et_p

22、lay.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_display.toString()

23、;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(););

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
百度文库年卡

猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服