1.string.xml代码

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="hello">Hello World, CalculatorActivity!</string>
<string name="app_name">计算器</string>
<string name="zero">0</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string> <string name="add">+</string>
<string name="cut">-</string>
<string name="multiply">×</string>
<string name="divide">÷</string>
<string name="spot">.</string>
<string name="equal">=</string>
<string name="reset">清除</string> </resources>

2.main.xml  布局代码

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:textSize="45sp"
android:maxLength="10"
android:textColor="#FFF"
android:layout_gravity="center"
android:cursorVisible="false"
android:gravity="right"
/> <TableLayout
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:layout_width="240dp"
android:layout_height="60dp"
android:background="#222"
android:textColor="#FFF"
android:text="" /> <Button
android:layout_width="80dp"
android:layout_height="60dp"
android:textColor="#FFF"
android:text="@string/reset"
android:id="@+id/reset"
/>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/seven"
android:id="@+id/seven"/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/eight"
android:id="@+id/eight"/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/nine"
android:id="@+id/nine" /> <Button
android:layout_width="80dp"
android:layout_height="fill_parent"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/divide"
android:id="@+id/divide" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/four"
android:id="@+id/four"
/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/five"
android:id="@+id/five"
/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/six"
android:id="@+id/six"
/> <Button
android:layout_width="80dp"
android:layout_height="fill_parent"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/multiply"
android:id="@+id/multiply"
/>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/one"
android:id="@+id/one"
/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/two"
android:id="@+id/two"
/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/three"
android:id="@+id/three"
/> <Button
android:layout_width="80dp"
android:layout_height="fill_parent"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/cut"
android:id="@+id/cut"
/>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/spot"
android:id="@+id/spot"
/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/zero"
android:id="@+id/zero"
/> <Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/equal"
android:id="@+id/equal"
/> <Button
android:layout_width="80dp"
android:layout_height="fill_parent"
android:textSize="40sp"
android:textColor="#FFF"
android:text="@string/add"
android:id="@+id/add"
/>
</LinearLayout> </TableLayout> </LinearLayout>

3.CalculatorActivity.java 代码

 package FosgeIT.Calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; /**
* 计算器
*
* @author YinRQ 2013-07-12 13:25:04
*/ public class CalculatorActivity extends Activity { // 私有变量
private int option = 0;
private boolean newdigital = true;
private double a = 0, b = 0;
private Button btnOne;
private Button btnTwo;
private Button btnThree;
private Button btnFour;
private Button btnFive;
private Button btnSix;
private Button btnSeven;
private Button btnEight;
private Button btnNine;
private Button btnZero;
private Button btnAdd;
private Button btnCut;
private Button btnMultiply;
private Button btnDivide;
private Button btnEqual;
private Button btnSpot;
private Button btnReset; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 初始化控件
btnOne = (Button) findViewById(R.id.one);
btnTwo = (Button) findViewById(R.id.two);
btnThree = (Button) findViewById(R.id.three);
btnFour = (Button) findViewById(R.id.four);
btnFive = (Button) findViewById(R.id.five);
btnSix = (Button) findViewById(R.id.six);
btnSeven = (Button) findViewById(R.id.seven);
btnEight = (Button) findViewById(R.id.eight);
btnNine = (Button) findViewById(R.id.nine);
btnZero = (Button) findViewById(R.id.zero);
btnAdd = (Button) findViewById(R.id.add);
btnCut = (Button) findViewById(R.id.cut);
btnMultiply = (Button) findViewById(R.id.multiply);
btnDivide = (Button) findViewById(R.id.divide);
btnEqual = (Button) findViewById(R.id.equal);
btnSpot = (Button) findViewById(R.id.spot);
btnReset = (Button) findViewById(R.id.reset); btnOne.setOnClickListener(lisenter);
btnTwo.setOnClickListener(lisenter);
btnThree.setOnClickListener(lisenter);
btnFour.setOnClickListener(lisenter);
btnFive.setOnClickListener(lisenter);
btnSix.setOnClickListener(lisenter);
btnSeven.setOnClickListener(lisenter);
btnEight.setOnClickListener(lisenter);
btnNine.setOnClickListener(lisenter);
btnZero.setOnClickListener(lisenter);
btnAdd.setOnClickListener(lisenter);
btnCut.setOnClickListener(lisenter);
btnMultiply.setOnClickListener(lisenter);
btnDivide.setOnClickListener(lisenter);
btnEqual.setOnClickListener(lisenter);
btnReset.setOnClickListener(lisenter);
btnSpot.setOnClickListener(lisenter);
} private OnClickListener lisenter = new OnClickListener() { public void onClick(View v) { TextView text = (TextView) findViewById(R.id.text);
String s = text.getText().toString();
Button btn = (Button) v;
String t = (String) btn.getText();
if (btn.getId() == R.id.zero || btn.getId() == R.id.one
|| btn.getId() == R.id.two || btn.getId() == R.id.three
|| btn.getId() == R.id.four || btn.getId() == R.id.five
|| btn.getId() == R.id.six || btn.getId() == R.id.seven
|| btn.getId() == R.id.eight || btn.getId() == R.id.nine) {
if (newdigital) {
text.setText(s + t);
} else {
text.setText(s);
newdigital = false;
}
return;
} // 加
if (btn.getId() == R.id.add) {
a = Double.parseDouble(s);
option = 1;
text.setText("");
return; } // 减
if (btn.getId() == R.id.cut) {
a = Double.parseDouble(s);
option = 2;
text.setText("");
return;
} // 乘
if (btn.getId() == R.id.multiply) {
a = Double.parseDouble(s);
option = 3;
text.setText("");
return;
} // 除
if (btn.getId() == R.id.divide){
a = Double.parseDouble(s);
option = 4;
text.setText("");
return;
} // 清除
if (btn.getId() == R.id.reset){
a = 0;
b = 0;
option = 0;
newdigital = true;
text.setText("");
return;
} // .
if (btn.getId() == R.id.spot){
if (s.indexOf(".") == -1)
if (s.trim().startsWith("0")) {
text.setText("0.");
newdigital = true;
} else {
text.setText(s + "."); }
return;
} // =
if (btn.getId() == R.id.equal){
b = Double.parseDouble(s);
switch (option) {
case 1:
text.setText(String.valueOf(a + b));
break;
case 2:
text.setText(String.valueOf(a - b));
break;
case 3:
text.setText(String.valueOf(a * b));
break;
case 4: {
if (b != 0) {
text.setText(String.valueOf(a / b));
} else {
Toast.makeText(CalculatorActivity.this, "除数不能为0",
Toast.LENGTH_SHORT).show();
text.setText("");
a = 0;
b = 0;
option = 0;
newdigital = true;
return;
}
break;
}
case 5:
text.setText(String.valueOf(Math.pow(a, b)));
break; }
return;
}
} }; }

4.效果截图

Android 简单计算器实现源码的更多相关文章

  1. 一款非常简单的android音乐播放器源码分享给大家

    一款非常简单的android音乐播放器源码分享给大家,该应用虽然很小,大家常用的播放器功能基本实现了,可能有点还不够完善,大家也可以自己完善一下,源码在源码天堂那里已经有了,大家可以到那里下载学习吧. ...

  2. Android 5.1.1 源码目录结构

    点击打开链接 最近公司培训新同事,我负责整理一点关于android的基础知识,遥想当年,刚接触android,也是一头雾水, 啥都不懂,就是靠看文档和视频,对android有一个初步了解,然后就通过查 ...

  3. (转)Android 5.1.1 源码目录结构

    转自:http://blog.csdn.net/tfslovexizi/article/details/51888458最近公司培训新同事,我负责整理一点关于android的基础知识,遥想当年,刚接触 ...

  4. android 近百个源码项目【转】

    http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 ...

  5. Ubuntu 14.04 LTS 下 android 2.3.5 源码编译过程

    Ubuntu 14.04 LTS 下 android 2.3.5 源码编译过程   在新的Ubuntu 64位系统下去编译早期的安卓源码是会出现很多问题的,因为64位系统在安装完成后,很多32位的兼容 ...

  6. android版猜拳游戏源码分享

    android版猜拳游戏源码分享安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的 ...

  7. Android -- 带你从源码角度领悟Dagger2入门到放弃

    1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...

  8. Android -- 带你从源码角度领悟Dagger2入门到放弃(二)

    1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...

  9. [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法

    博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...

随机推荐

  1. 软件设计模式之代理模式(JAVA)

    貌似停笔了近半个月了,实在不该啊,新的一年,时刻让自己归零. Back To Zero,就从这篇文章拉开今年的序幕吧. 这篇文章准备介绍下有关代理模式的基本概念和静态代理.动态代理的优缺点及使用方法( ...

  2. 038改变状态栏的颜色(扩展知识:关于iOS不同版本的消息通知知识)

    效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @e ...

  3. VSCode升级后启动不了~

    之前安装了VSCode,一直用的好好的,每次启动后后下角总会提示自己有新版本可以更新,然后就手贱点击了更新,然后就出问题了,更新后VSCode无法启动,打不开了,哭死! 于是,我尝试了以下方法,均无济 ...

  4. MySQL自定义排序函数FIELD()

    MySQL可以通过field()函数自定义排序,格式:field(value,str1,str2,str3,str4),value与str1.str2.str3.str4比较,返回1.2.3.4,如遇 ...

  5. mysql中json_remove函数的使用?

    需求描述: 今天看json记录,可以通过json_remove函数对一个key或多个key从个json记录中去掉. 操作过程: 1.查看一个已经存在的json表 mysql> select * ...

  6. linux proc目录介绍

    https://www.cnblogs.com/DswCnblog/p/5780389.html

  7. js判断操作系统与浏览器

    摘要: 对于前端开发我们最重要的工作就是兼容性,系统的兼容性,浏览器的兼容性等等.今天分享一个我在项目中封装的判断操作系统与浏览器的方法. 操作系统: var os = (function() { v ...

  8. python mysql 封装

    封装 观察前面的文件发现,除了sql语句及参数不同,其它语句都是一样的 创建MysqlHelper.py文件,定义类 #encoding=utf8 import MySQLdb class Mysql ...

  9. Linux下tomcat无法启动

    场景:干净的tomcat,刚解压 1 通过./startup.sh,提示启动成功,但查看没有日志 2 通过netstat -tln查看端口,发现找不到8080 3 通过./catalina.sh ru ...

  10. DOS 配置IP地址

    @echo off :startIP set /p source=STATIC Y or N or E: echo source:%source% if "%source%" == ...