一个计算器Android程序的源码部分分为主干和细节两部分。

一、主干

1. 主干的构成

  • 计算器的布局
  • 事件(即计算器上的按钮、文本框)监听
  • 实现计算

2. 详细解释

  假设我们的项目名为Calculator,而布局名称(Layout Name)为默认的activity_main 。即设置如下图所示:

  在这种前提下,有:

  • 设置计算器布局的文件:Calculator/app/src/main/res/layout/activity_main.xml
  • 事件监听和计算实现在同一个文件里:Calculator/app/src/main/java/下的一个子目录里的MainActivity.java

  即如下图所示:

3. 主干代码部分

  • 计算器布局代码(写在activity_main.xml文件里):
 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:editable="false"
android:hint="@string/shuru" /> <EditText
android:id="@+id/output"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:editable="true"
android:gravity="right"
android:hint="@string/shuchu" /> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/seven"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:text="@string/seven"
android:textSize="40sp" /> <Button
android:id="@+id/eight"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_toRightOf="@id/seven"
android:text="@string/eight"
android:textSize="40sp" /> <Button
android:id="@+id/nine"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_toRightOf="@id/eight"
android:text="@string/nine"
android:textSize="40sp" /> <Button
android:id="@+id/add"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/nine"
android:text="@string/add"
android:textSize="40sp" /> <Button
android:id="@+id/four"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/seven"
android:text="@string/four"
android:textSize="40sp" /> <Button
android:id="@+id/five"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_below="@id/eight"
android:layout_toRightOf="@id/four"
android:text="@string/five"
android:textSize="40sp" /> <Button
android:id="@+id/six"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_below="@id/nine"
android:layout_toRightOf="@id/five"
android:text="@string/six"
android:textSize="40sp" /> <Button
android:id="@+id/subtract"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:layout_below="@id/add"
android:layout_toRightOf="@id/six"
android:text="@string/subtract"
android:textSize="40sp" /> <Button
android:id="@+id/one"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/four"
android:text="@string/one"
android:textSize="40sp" /> <Button
android:id="@+id/two"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_below="@id/five"
android:layout_toRightOf="@id/one"
android:text="@string/two"
android:textSize="40sp" /> <Button
android:id="@+id/three"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_below="@id/six"
android:layout_toRightOf="@id/two"
android:text="@string/three"
android:textSize="40sp" /> <Button
android:id="@+id/multiply"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:layout_below="@id/subtract"
android:layout_toRightOf="@id/three"
android:text="@string/multiply"
android:textSize="40sp" /> <Button
android:id="@+id/zero"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/one"
android:text="@string/zero"
android:textSize="40sp" /> <Button
android:id="@+id/clear"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_below="@id/two"
android:layout_toRightOf="@id/zero"
android:text="@string/clear"
android:textSize="40sp" /> <Button
android:id="@+id/result"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_below="@id/three"
android:layout_toRightOf="@id/clear"
android:text="@string/result"
android:textSize="40sp" /> <Button
android:id="@+id/divide"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:layout_below="@id/multiply"
android:layout_toRightOf="@id/result"
android:text="@string/divide"
android:textSize="40sp" /> <Button
android:id="@+id/dot"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/zero"
android:text="@string/dot"
android:textSize="40sp" />
<Button
android:id="@+id/writeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/dot"
android:text="@string/write"
android:textSize="40sp" />
<Button
android:id="@+id/readButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/dot"
android:text="@string/read"
android:textSize="40sp" /> <CheckBox
android:id="@+id/appendBox"
android:text="@string/appendBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/divide"
android:layout_toStartOf="@+id/divide"
android:layout_marginBottom="12dp"
/> </RelativeLayout> <EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" /> <EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/displayView" /> <EditText
android:id="@+id/errorzero"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:editable="false"
android:gravity="center"
/>
<EditText
android:id="@+id/resultText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:editable="false"
android:gravity="left"
android:text="@string/resultText"
/>
</LinearLayout>
</ScrollView>
  • 事件监听和实现计算代码(写在MainActivity.java文件里)
 package com.example.lenovo.calculator;

         import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
private EditText output = null;
private EditText input = null;
private Button btn0 = null;
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
private Button btn4 = null;
private Button btn5 = null;
private Button btn6 = null;
private Button btn7 = null;
private Button btn8 = null;
private Button btn9 = null;
private Button btnadd = null;
private Button btnsubtract = null;
private Button btnmultiply = null;
private Button btndivide = null;
private Button btnclear = null;
private Button btnresult = null;
private Button btndot = null; private EditText errorzero = null; private EditText resultText = null;
private Button writeButton = null;
private Button readButton = null;
private CheckBox appendBox = null;
private EditText textView = null;
private EditText displayView = null;
public String FILE_NAME = "fileDemo.txt"; private String str = "";//保存数字
private String strold = "";//原数字
private char act = ' ';//记录“加减乘除等于”符号
private int count = 0;//判断要计算的次数,如果超过一个符号,先算出来一部分
private Float result = null;//计算的输出结果
private Boolean errBoolean = false;//有错误的时候为true,无错为false
private Boolean flagBoolean = false;//一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息
private Boolean flagDot = false; //小数点标志位 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); output = (EditText) findViewById(R.id.output);
input = (EditText) findViewById(R.id.input); errorzero = (EditText) findViewById(R.id.errorzero);
resultText = (EditText) findViewById(R.id.resultText);
writeButton = (Button) findViewById(R.id.writeButton);
readButton = (Button) findViewById(R.id.readButton);
textView = (EditText) findViewById(R.id.textView);
displayView = (EditText) findViewById(R.id.displayView);
appendBox = (CheckBox) findViewById(R.id.appendBox); btn0 = (Button) findViewById(R.id.zero);
btn1 = (Button) findViewById(R.id.one);
btn2 = (Button) findViewById(R.id.two);
btn3 = (Button) findViewById(R.id.three);
btn4 = (Button) findViewById(R.id.four);
btn5 = (Button) findViewById(R.id.five);
btn6 = (Button) findViewById(R.id.six);
btn7 = (Button) findViewById(R.id.seven);
btn8 = (Button) findViewById(R.id.eight);
btn9 = (Button) findViewById(R.id.nine);
btnadd = (Button) findViewById(R.id.add);
btnsubtract = (Button) findViewById(R.id.subtract);
btnmultiply = (Button) findViewById(R.id.multiply);
btndivide = (Button) findViewById(R.id.divide);
btnclear = (Button) findViewById(R.id.clear);
btnresult = (Button) findViewById(R.id.result);
btndot = (Button) findViewById(R.id.dot);
//设置按钮侦听事件
btn0.setOnClickListener(listener);
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
btn4.setOnClickListener(listener);
btn5.setOnClickListener(listener);
btn6.setOnClickListener(listener);
btn7.setOnClickListener(listener);
btn8.setOnClickListener(listener);
btn9.setOnClickListener(listener);
//执行运算
btnadd.setOnClickListener(listener);
btnsubtract.setOnClickListener(listener);
btnmultiply.setOnClickListener(listener);
btndivide.setOnClickListener(listener);
btnclear.setOnClickListener(listener);
btnresult.setOnClickListener(listener); btndot.setOnClickListener(listener); writeButton.setOnClickListener(writelistener);
readButton.setOnClickListener(readlistener); // ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
} private OnClickListener listener = new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//输入数字
case R.id.zero:
num(0);
break;
case R.id.one:
num(1);
break;
case R.id.two:
num(2);
break;
case R.id.three:
num(3);
break;
case R.id.four:
num(4);
break;
case R.id.five:
num(5);
break;
case R.id.six:
num(6);
break;
case R.id.seven:
num(7);
break;
case R.id.eight:
num(8);
break;
case R.id.nine:
num(9);
break; case R.id.dot:
dot();
break;
//执行运算
case R.id.add:
add();
break;
case R.id.subtract:
sub();
break;
case R.id.multiply:
multiply();
break;
case R.id.divide:
divide();
break;
case R.id.clear:
clear();
break;
//计算结果
case R.id.result:
result();
if (!errBoolean && flagBoolean) {
output.setText(String.valueOf(result));
}
resultText.setText(strold + act + str + "=" + result+" ");
break; default:
break; }
input.setText(strold + act + str);
output.setText(String.valueOf(result)); }
}; private OnClickListener writelistener = new OnClickListener() {
@Override
public void onClick(View view) {
//textView.setText(""); FileOutputStream fos = null;
try {
if (appendBox.isChecked()) {
fos = openFileOutput(FILE_NAME, Context.MODE_APPEND);
} else {
fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
}
String text = resultText.getText().toString();
fos.write(text.getBytes());
textView.setText("文件写入成功,写入长度:" + text.length());
//resultText.setText(""); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (fos != null)
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
private OnClickListener readlistener = new OnClickListener() {
@Override
public void onClick(View view) { displayView.setText("");
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
if (fis.available() == 0) {
return;
}
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1) { }
String text = new String(readBytes);
displayView.setText(text);
textView.setText("文件读取成功,写入长度:" + text.length()); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}; private void dot() {
// TODO Auto-generated method stub if (!flagDot) {
str = str + ".";
flagBoolean = false;
flagDot = true;
}
} private void clear() {
// TODO Auto-generated method stub
str = strold = "";
count = 0;
act = ' ';
result = null;
flagBoolean = false;
flagDot = false;
input.setText(strold + act + str);
output.setText("");
errorzero.setText("");
displayView.setText("");
textView.setText("");
resultText.setText("");
} private void divide() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '/';
flagBoolean = false;
}
} private void multiply() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '*';
flagBoolean = false;
}
} private void sub() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '-';
flagBoolean = false;
}
} private void add() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '+';
flagBoolean = false;
}
} private void check() {
// TODO Auto-generated method stub
if (count >= 1) {
result();
str = String.valueOf(result);
}
strold = str;
str = "";
count++;
flagDot = false;
errorzero.setText("");
} //计算输出结果
private void result() {
// TODO Auto-generated method stub
if (flagBoolean) {
Float a, b; a = Float.parseFloat(strold);
b = Float.parseFloat(str); if (b == 0 && act == '/') {
clear();
errorzero.setText("除数不能为零!");
//output.setText("除数不能为零!"); //errBoolean=true;
} if (!errBoolean) {
switch (act) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break; default:
break;
}
} }
} private void num(int i) {
// TODO Auto-generated method stub
str = str + String.valueOf(i);
flagBoolean = true;
errorzero.setText("");
} }

二、细节

  仅仅将主干部分代码copy下来并不能运行程序,因为主干代码调用了一些文件的代码,这些文件就是我们所说的细节。

1. 字符串资源

  • 文件位置:Calculator/app/src/main/res/values/strings.xml
  • 代码部分:
 <resources>
<string name="app_name">Calculator</string>
<string name="action_settings">Settings</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="subtract">-</string>
<string name="multiply">*</string>
<string name="divide">/</string>
<string name="clear">CE</string>
<string name="result">=</string>
<string name="shuru">请按数字键盘输入数字</string>
<string name="shuchu">计算器输出结果</string>
<string name="dot">.</string>
<string name="write">写入</string>
<string name="read">读取</string>
<string name="resultText">计算式</string>
<string name="appendBox">追加模式</string> </resources>

2. 其他可能要改的文件

  • Calculator/app/src/main/res/drawable:设置相关的背景颜色、按钮特效
  • Calculator/app/src/main/AndroidManifest.xml:设置这个项目的整体配置

三、备注

  1. 本文提供的简易计算器仅需要修改前三个文件(布局文件、监听实现文件、字符串资源文件)
  2. 纯复制粘贴是一定会报错的,留意一点将有些地方改动一下

制作一个简易计算器——基于Android Studio实现的更多相关文章

  1. 基于Android Studio搭建hello world工程

    基于Android Studio搭建hello world工程 版本:ANDROID STUDIO V0.4.6 This download includes: ·        Android St ...

  2. 《ArcGIS Runtime SDK for Android开发笔记》——(5)、基于Android Studio构建ArcGIS Android开发环境(离线部署)(转)

    1.前言 在上一篇的内容里我们介绍了基于Android Studio构建ArcGIS Runtime SDK for Android开发环境的基本流程,流程中我们采用的是基于Gradle的构建方式,在 ...

  3. 《ArcGIS Runtime SDK for Android开发笔记》——(6)、基于Android Studio的ArcGIS Android工程结构解析

    1.前言 Android Studio 是第一个Google官方的 Android 开发环境.其他工具,例如 Eclipse,在 Android Studio 发布之前已经有了大规模的使用.为了帮助开 ...

  4. 如何使用Java AWT 创建一个简易计算器

    摘要:手把手教你使用 Java AWT 创建一个简易计算器. 本文分享自华为云社区<手把手教你使用 Java AWT 创建一个简易计算器>,作者:海拥 . 关于AWT AWT (抽象窗口工 ...

  5. 用XMLHttpRequest制作一个简易ajax

    概述 jquery退出历史舞台之后,我们怎么来发送ajax请求呢?可以用相关的库,也可以自己制作一个简易的ajax. 需要说明的是,我们使用的是XMLHttpRequest 2,它几乎兼容所有主流浏览 ...

  6. 前端 JavaScript 实现一个简易计算器

    前端使用 JavaScript 实现一个简易计算器,没有难度,但是里面有些小知识还是需要注意的,算是一次基础知识回顾吧. 题目 实现一个简易版的计算器,需求如下: 1.除法操作时,如果被除数为0,则结 ...

  7. 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具

    查看本章节 查看作业目录 需求说明: 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具 实现思路: 使用history对象中的 forward() 方法和 ...

  8. iOS:制作一个简易的计算器

    初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...

  9. 《ArcGIS Runtime SDK for Android开发笔记》——(4)、基于Android Studio构建ArcGIS Android开发环境

    1.前言 2015年1月15日,发布ArcGIS Runtime SDK for Android v10.2.5版本.从该版本开始默认支持android studio开发环境,示例代码的默认开发环境也 ...

随机推荐

  1. C# http post、get请求

    public string HttpPostJson(string Url, string paramData, Dictionary<string, string> headerDic ...

  2. 你不知道的javaScript笔记(7)

    异步:现在与将来 分块的程序 可以把JavaScript 程序写在单独的js 文件中,这个程序是由多个块组成的,这些块 中只有一个是现在执行,其余在捡来执行,最常见的块单位是函数. 例如: funct ...

  3. LeetCode 中级 - 有序链表转换二叉搜索树(109)

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...

  4. 青蛙的约会(exgcd/扩展欧几里得)

    题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清 ...

  5. 前行记录 - NOIP2018游记

    NOIP2018游记 - 前行记录 NOIP2018 完跪……滚回学校考半期 QwQ 这篇不是题解 awa ,题解之后会发布的,毕竟我还没有AC呢 又及……G2020 陌路笙歌 - 再见(╯▽╰) 感 ...

  6. 搭建简单的hadoop集群(译文)

    本文翻译翻译自http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/ClusterSetup.html 具体的实 ...

  7. Ehcache基于java API实现

    上代码: package com.utils.cacheutils; import com.situopenapi.constant.EhcacheConstants; import com.situ ...

  8. Trie(字典树,前缀树)_模板

    Trie Trie,又经常叫前缀树,字典树等等. Trie,又称前缀树或字典树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的 ...

  9. php 操作RabbitMQ

    本文摘抄自:https://www.cnblogs.com/alin-qu/p/8312874.html php 操作RabbitMQ   基本流程图 如果exchange 没有绑定queue,则消息 ...

  10. pygame小游戏之坦克大战

    以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5  pycharm import pygame,sys,time from random ...