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 今天用分享动画实现微信点击全屏效果 本文源代码下 ...
随机推荐
- php实现二维码
封装函数 function verifyImage($len=3){ //session_start(); $scr="abcdefghijklmnoqprstuvwxyzABCDEFJHI ...
- OpenGL函数思考-glColor
http://blog.csdn.net/shuaihj/article/details/7231980 OpenGL函数思考-glColor 函数原型: glColor3b,glColor ...
- vifx.y-emu 和 vifx.y 和 tapx.y
xen 启动虚拟机后,domain0 可以看到虚拟网卡设备,但是有几种显示 tapx.y , vifx.y 或者 vifx.y-emu . 在我的实验里,同样的配置,如 vif = ["ty ...
- 如何防止Android反编译
转自: http://my.eoe.cn/sandking/archive/19772.html http://www.cnblogs.com/zdz8207/archive/2012/01/28/d ...
- Appium+python自动化1-环境搭建(上)【转载】
前言 appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.小编擅长Python,所以接下来的教程是appium+py ...
- linux文件名匹配
* 匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...] 匹配[ ]中所包含的任何字符. [!...] 匹配[ ]中非感叹号!之后的字符. 如: s* ...
- 由"软件是干什么的"引发的思考
自工作以来,都只在进行模块的开发,很少站在整个项目的角度思考过.甚至,自己开发的软件,自己都没有去用过,包括开发的一些APP,都没有下载来认真体验过.思考过.却对自己手机上那些用过的A ...
- python 字符串最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow&qu ...
- FZU-2214 Knapsack problem(DP使用)
Problem 2214 Knapsack problem Accept: 863 Submit: 3347Time Limit: 3000 mSec Memory Limit : 327 ...
- luogu P1395 会议
题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会议,村长希望所有村民到会议地点的距离之和最小,那么村长应该要把会议地 ...