前台页面代码,通过线性布局方式实现计算器页面:如图所示

color.xml,自定义颜色values;

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="gray">#CCCCCC</color>
<color name="green">#00ff00</color>
<color name="orange">#FF8040</color>
</resources>

white_bg.xml,自定义的输入文本框样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 设置圆角的幅度 -->
<corners android:radius="5dp" />
<!-- 设置渐变颜色 起始为白色 color是手动添加的颜色枚举 -->
<gradient android:startColor="@color/white" />
<!-- 设置渐变颜色 结束为银灰色 -->
<gradient android:endColor="@color/gray" /> <!-- 边线 -->
<stroke
android:width="1dp"
android:color="@color/black" />
</shape>

gray_by.xml,自定义按钮样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 圆角 -->
<corners android:radius="5dp" />
<!-- 填充颜色 -->
<solid android:color="@color/gray" />
</shape>

orange_bg.xml,自定义点击时按钮的样式  

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 圆角 -->
<corners android:radius="5dp" />
<!-- 填充颜色 -->
<solid android:color="@color/orange" /> </shape>

orange_select.xml:定义按钮默认和点击时的样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 按钮单击时的状态 -->
<item android:drawable="@drawable/orange_bg" android:state_pressed="true"></item>
<!-- 按钮默认状态 -->
<item android:drawable="@drawable/gray_bg"></item>
</selector>

main.xml代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <!-- layout_height:设置文本框高度 -->
<!-- editable:设置文字不可以编辑 -->
<!-- gravity:设置文字靠右下 -->
<!-- background:为自定义的样式,新建drawable文件夹,在drawable下新建white_bg.xml文件 --> <EditText
android:id="@+id/et_Text"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/white_bg"
android:editable="false"
android:ems="10"
android:layout_marginTop="20dp"
android:gravity="right|bottom" > <requestFocus android:layout_width="wrap_content" />
</EditText> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal" > <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="C"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_del"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="del"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_divide"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="÷"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_multiply"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="×"
android:layout_margin="2dp"
android:textSize="20sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="7"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="8"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="9"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_minus"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="-"
android:layout_margin="2dp"
android:textSize="20sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="4"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="5"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="6"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_plus"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="+"
android:layout_margin="2dp"
android:textSize="20sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="1"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="2"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="3"
android:layout_margin="2dp"
android:textSize="20sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="4"
android:text="0"
android:layout_margin="2dp"
android:textSize="20sp" /> <Button
android:background="@drawable/orange_select"
android:id="@+id/btn_point"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
android:text="."
android:layout_margin="2dp"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<Button
android:background="@drawable/orange_select"
android:id="@+id/btn_equals"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_weight="3"
android:text="="
android:layout_margin="2dp"
android:textSize="20sp" />
</LinearLayout> </LinearLayout>

main_activity.java 业务代码

package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener { Button btn_1; // 数字1
Button btn_2; // 数字2
Button btn_3; // 数字3
Button btn_4; // 数字4
Button btn_5; // 数字5
Button btn_6; // 数字6
Button btn_7; // 数字7
Button btn_8; // 数字8
Button btn_9; // 数字9
Button btn_0; // 数字0
Button btn_clear; // 清0
Button btn_del; // 删除健
Button btn_divide; // 除号
Button btn_multiply; // *号
Button btn_minus; // -号
Button btn_plus; // +号
Button btn_point; // 小数点
Button btn_equals; // =
EditText et_Text; // 显示文本 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 获取页面上的控件
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
btn_plus = (Button) findViewById(R.id.btn_plus);
btn_divide = (Button) findViewById(R.id.btn_divide);
btn_multiply = (Button) findViewById(R.id.btn_multiply);
btn_minus = (Button) findViewById(R.id.btn_minus);
btn_point = (Button) findViewById(R.id.btn_point);
btn_equals = (Button) findViewById(R.id.btn_equals);
et_Text = (EditText) findViewById(R.id.et_Text); // 按钮的单击事件
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_0.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_equals.setOnClickListener(this); } //定义第一个操作数和第二个操作数
double d1 = 0, d2 = 0;
//定义运算符
String oprator = ""; @Override
public void onClick(View v) {
String str = et_Text.getText().toString(); switch (v.getId()) {
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_0:
case R.id.btn_point:
// 点击数字按钮和小数点时,在文本内追加内容
et_Text.setText(str + ((Button) v).getText().toString());
break;
case R.id.btn_plus:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
// 点击运算符按钮时,获取前面输入的第一个运算符
d1 = Double.parseDouble(et_Text.getText().toString());
// 添加到文本区域内
et_Text.setText(str + " " + ((Button) v).getText().toString() + " ");
// 获取点击的运算符
oprator = ((Button) v).getText().toString();
break;
case R.id.btn_clear:
// 清空文本内容
et_Text.setText("");
break;
case R.id.btn_del:
// 点击删除按钮,删除一个字符
if (str != null && !str.equals("")) {
str = str.substring(0, str.length() - 1);
et_Text.setText(str);
}
break;
case R.id.btn_equals:
// 计算结果方法,获取第二个输入的数字
int start = str.lastIndexOf(oprator);
d2 = Double.parseDouble(str.substring(start + 1, str.length()));
getResult(d1, d2, oprator);
break;
}
} // 计算结果
private void getResult(double d1, double d2, String oprator) {
// 计算结果
String str = et_Text.getText().toString();
double result = 0;
if (oprator.equals("+")) {
result = d1 + d2;
} else if (oprator.equals("-")) {
result = d1 - d2;
} else if (oprator.equals("×")) {
result = d1 * d2;
} else if (oprator.equals("÷")) {
if (d2 == 0) {
result = 0;
} else {
result = d1 / d2;
}
} // 如果不包含小数点则为小数和除法运算
if (!str.contains(".") && oprator != "÷") {
et_Text.setText(((int) result) + "");
} else {
et_Text.setText(result + "");
} } }

Android学习(七) Android实现计算器的更多相关文章

  1. [置顶] Android学习系列-Android中解析xml(7)

    Android学习系列-Android中解析xml(7) 一,概述 1,一个是DOM,它是生成一个树,有了树以后你搜索.查找都可以做. 2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件.   ...

  2. android学习七(创建自己定义控件)

    前面学习的是android的基本控件和布局的使用,可是主要的控件和布局有时候并不能实现复杂的布局.我们来看下各种控件和布局的关系. 可见全部的控件都是直接或者间接的继承自View的,全部的布局都是直接 ...

  3. Android学习七---Hello OpenCV samples

    创建一个能够使用OpenCV JavaCameraView的应用程序来了解基于OpenCV java API 的应用程序的开发流程.有了Android的基础,在程序中需要修改的几个地方1.activi ...

  4. Android学习——在Android中使用OpenCV的第一个程序

    刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...

  5. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  6. Android 学习之--android多线程断点下载

    我们平时都用"迅雷"下载软件,当下载到一半的时候突然断网,下次开启的时候能够从上次下载的地方继续下载,而且下载速度很快,那么这是怎么做到的呢! 其实它的“快”其实就是多线程的下载实 ...

  7. Android——android学习(android目录与AndroidManifest解析)

    res目录:存放android项目的各种资源文件 layout:存放界面布局文件 values:存放各种xml格式的资源文件 strings.xml:字符串资源文件: colors.xml:颜色资源文 ...

  8. Android学习【Android内核编译流程和错误笔记】

    博客:http://blog.csdn.net/muyang_ren Ubuntu14.04 LTS(要求是64位长期支持版LTS) Jdk1.8 内核:android4.0 一:jdk 1.解压jd ...

  9. Android学习路径——Android的四个组成部分activity(一)

    一.什么是Activity? Activity简单的说就是一个接口.我们是Android手机上看到的每个界面就是一个activity. 二.Activity的创建 1.定义一个类继承activity, ...

  10. Android学习之Android 5.0分享动画实现微信点击全屏效果

    Android5.0过渡动画,请看 http://blog.csdn.net/qq_16131393/article/details/51112772 今天用分享动画实现微信点击全屏效果 本文源代码下 ...

随机推荐

  1. YYH的球盒游戏(NOIP模拟赛Round 6)

    题目描述 YYH有一些总共有种颜色的球,他有颜色的球个.他同样有个盒子,第个盒子能放个球. 他的目标是把这个球按规则放进个盒子里: 对于一个盒子,对于每种颜色的球至多只能放个. 把颜色为的球放进盒子, ...

  2. 推荐几个好用的PHP集成开发环境

    (转自:http://blog.sina.com.cn/s/blog_5bd6b45101011bu2.html ) 分类: PHP PHP新手在准备正式开始写PHP代码的时候,不幸的是被PHP的开发 ...

  3. tcpreplay 流量拆分算法研究

    1.1  算法目的 现在网络架构一般是Client-Server架构,所以网络流量一般是分 C-S 和 S-C 两个方向.tcpdump等抓包工具获取的pcap包,两个流向的数据没有被区分.流量方向的 ...

  4. Windows录音API学习笔记--转

    Windows录音API学习笔记 结构体和函数信息  结构体 WAVEINCAPS 该结构描述了一个波形音频输入设备的能力. typedef struct { WORD      wMid; 用于波形 ...

  5. 关于多态的理解,有助于理解TStream抽象类的多态机制。

    有的时候 不是很明白流的机制,因为有内存流  文件流 图片流 等等 他们之间的相互转化 靠的就是流的多态性.... unit Unit11; interface uses Winapi.Windows ...

  6. poj 1873(枚举所有的状态+凸包)

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6115   Accepted: 1 ...

  7. 大牛教你如何循序渐进,有效的学习JavaScript?

    首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门.谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别急着骂,先听我说! 你叫一个大学生去教小学数学 ...

  8. 牛客网 暑期ACM多校训练营(第二场)D.money-贪心 or 动态规划

    D.money 贪心,直接贴官方的题解吧. 题目大意 你要按照顺序依次经过n个商店,每到达一个商店你可以购买一件商品,也可以出售你手中的商品. 同一时刻你手上最多拿一件商品.在第i个商店购买和出售的代 ...

  9. Southern African 2001 框架折叠 (拓扑序列的应用)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398377.html 题目:考虑五个图片堆叠在一起,比如下面的9 * 8 的矩阵表示的是这些图片的边缘框. 现在上面的图片 ...

  10. Codeforces 581F Zublicanes and Mumocrates(树型DP)

    题目链接  Round 322 Problem F 题意  给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...