Android 简单计算器实现源码
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 简单计算器实现源码的更多相关文章
- 一款非常简单的android音乐播放器源码分享给大家
一款非常简单的android音乐播放器源码分享给大家,该应用虽然很小,大家常用的播放器功能基本实现了,可能有点还不够完善,大家也可以自己完善一下,源码在源码天堂那里已经有了,大家可以到那里下载学习吧. ...
- Android 5.1.1 源码目录结构
点击打开链接 最近公司培训新同事,我负责整理一点关于android的基础知识,遥想当年,刚接触android,也是一头雾水, 啥都不懂,就是靠看文档和视频,对android有一个初步了解,然后就通过查 ...
- (转)Android 5.1.1 源码目录结构
转自:http://blog.csdn.net/tfslovexizi/article/details/51888458最近公司培训新同事,我负责整理一点关于android的基础知识,遥想当年,刚接触 ...
- android 近百个源码项目【转】
http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 ...
- Ubuntu 14.04 LTS 下 android 2.3.5 源码编译过程
Ubuntu 14.04 LTS 下 android 2.3.5 源码编译过程 在新的Ubuntu 64位系统下去编译早期的安卓源码是会出现很多问题的,因为64位系统在安装完成后,很多32位的兼容 ...
- android版猜拳游戏源码分享
android版猜拳游戏源码分享安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的 ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃
1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃(二)
1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...
- [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法
博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...
随机推荐
- 原来找字也可以这样用ElseIf FindStr 手机按键精灵 跟大漠的区别
原来找字也可以这样用ElseIf FindStr(646, 1109, 776, 1261, "公告小叉", "FFFFFF-333333", 0.9, in ...
- 使用 stretchableImageWithLeftCapWidth 方法实现可伸缩图片
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...
- 在AD的环境下,更改计算机名导致TFS,无法连接解决办法
D:\vs2015>tf workspaces /collection:http://10.1.0.104:8080/tfs/dahua.adrms /updateComputerName:WI ...
- selenium+phantomjs渲染网页
from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapa ...
- 下拉刷新 上拉更多 支持ListView GridView WebView【转载】
转载自:http://www.stay4it.com/?p=245 老贴重发,源代码放附件了,需要的下载把. 终于有新货了.昨天改了下,在ListView和GridView加了个返回到顶部的按钮,li ...
- C#------Aspose.cells使用方法
转载: http://www.cnblogs.com/muer/p/yaxle.html 代码: public ActionResult ImportData(HttpPostedFileBase f ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 多协议
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多协议 可以自行扩展协议,参见:协议扩展 (1) 不同服务不同协议 比如:不同服务 ...
- 线程同步 – lock和Monitor
在多线程代码中,多个线程可能会访问一些公共的资源(变量.方法逻辑等等),这些公共资源称为临界区(共享区):临界区的资源是不安全,所以需要通过线程同步对多个访问临界区的线程进行控制. 同样,有些时候我们 ...
- Windows 7 无密码文件共享
Windows7中创建无密码的文件共享的几个步骤: 在“控制面板\所有控制面板项\网络和共享中心\高级共享设置”开启“关闭密码保护共享”和“启用文件和打印机共享”.关闭密码保护共享的操作会启用Gues ...
- grid网格的流动一
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...