Android学习(七) Android实现计算器
前台页面代码,通过线性布局方式实现计算器页面:如图所示
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实现计算器的更多相关文章
- [置顶] Android学习系列-Android中解析xml(7)
Android学习系列-Android中解析xml(7) 一,概述 1,一个是DOM,它是生成一个树,有了树以后你搜索.查找都可以做. 2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件. ...
- android学习七(创建自己定义控件)
前面学习的是android的基本控件和布局的使用,可是主要的控件和布局有时候并不能实现复杂的布局.我们来看下各种控件和布局的关系. 可见全部的控件都是直接或者间接的继承自View的,全部的布局都是直接 ...
- Android学习七---Hello OpenCV samples
创建一个能够使用OpenCV JavaCameraView的应用程序来了解基于OpenCV java API 的应用程序的开发流程.有了Android的基础,在程序中需要修改的几个地方1.activi ...
- Android学习——在Android中使用OpenCV的第一个程序
刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- Android 学习之--android多线程断点下载
我们平时都用"迅雷"下载软件,当下载到一半的时候突然断网,下次开启的时候能够从上次下载的地方继续下载,而且下载速度很快,那么这是怎么做到的呢! 其实它的“快”其实就是多线程的下载实 ...
- Android——android学习(android目录与AndroidManifest解析)
res目录:存放android项目的各种资源文件 layout:存放界面布局文件 values:存放各种xml格式的资源文件 strings.xml:字符串资源文件: colors.xml:颜色资源文 ...
- Android学习【Android内核编译流程和错误笔记】
博客:http://blog.csdn.net/muyang_ren Ubuntu14.04 LTS(要求是64位长期支持版LTS) Jdk1.8 内核:android4.0 一:jdk 1.解压jd ...
- Android学习路径——Android的四个组成部分activity(一)
一.什么是Activity? Activity简单的说就是一个接口.我们是Android手机上看到的每个界面就是一个activity. 二.Activity的创建 1.定义一个类继承activity, ...
- Android学习之Android 5.0分享动画实现微信点击全屏效果
Android5.0过渡动画,请看 http://blog.csdn.net/qq_16131393/article/details/51112772 今天用分享动画实现微信点击全屏效果 本文源代码下 ...
随机推荐
- 【ZOJ4063】Tournament(构造)
题意:n个人要打m轮比赛 每一轮每个人都要有一个对手.而且每个对手只能打一次.假设a与b打了,c与d打了, 那么后面的任意一轮如果a与c打了,那么b就必须和d打 问是否存在方案,输出字典序最小的一组, ...
- Android四大组件:Service
前言 Service作为Android四大组件之一,应用非常广泛 本文将介绍对Service进行全面介绍(基础认识.生命周期.使用和应用场景) 目录 目录 1. 基础知识 定义:服务,属于Androi ...
- eclipse块编辑
1. Windows > Preferences然后键入keys或浏览到General > Keys.根据该键输入filter text的block selection快速找到快捷方式上市 ...
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- usb驱动---linux ACM驱动详解ACA【转】
转自:http://blog.chinaunix.net/uid-9185047-id-3404684.html DTE提供或接收数据,连接到网络中的用户端机器,主要是计算机和终端设备.与此相对地,在 ...
- [置顶] Linux 虚拟地址与物理地址的映射关系分析【转】
转自:http://blog.csdn.net/ordeder/article/details/41630945 版权声明:本文为博主(http://blog.csdn.net/ordeder)原创文 ...
- commons-lang3中DateUtils类方法介绍
添加commons-lang3的Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <art ...
- centos 部署web项目
Linux下安装Tomcat服务器和部署Web应用 一.上传Tomcat服务器
- poj2104(划分树模板)
poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...
- 洛谷 P4551 最长异或路径
题目描述 给定一棵 nn 个点的带权树,结点下标从 11 开始到 NN .寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有节点权值的异或. 输入输出格式 输入格式: ...