Android课程---计算器的实现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.test5"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LongClickActivityActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UIActivity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CalculatorActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity1" /> </application> </manifest>
package com.hanqi.test5; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class CalculatorActivity extends Activity implements View.OnClickListener {
EditText et;
Button bt_clear;
Button bt_goback; //全局变量
Button bt_7;
Button bt_8;
Button bt_9;
Button bt_6;
Button bt_5;
Button bt_4;
Button bt_3;
Button bt_2;
Button bt_1;
Button bt_0;
Button bt_jia;
Button bt_jian;
Button bt_cheng;
Button bt_chu;
Button bt_dian;
Button bt_deng; //存储显式的内容
//StringBuilder:操作字符串的工具类
private StringBuilder str_show = new StringBuilder();//实例化 //当前运算符按钮的id
private int id_yunsf = 0;
//运算符前面的数值 Double:包装类
private Double number_1;
//运算符后面的数值
private Double number_2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator); et = (EditText)findViewById(R.id.et);
bt_clear = (Button)findViewById(R.id.clear);
bt_goback = (Button)findViewById(R.id.goback);
bt_7 = (Button)findViewById(R.id.bt_7);
bt_8 = (Button)findViewById(R.id.bt_8);
bt_9 = (Button)findViewById(R.id.bt_9);
bt_6 = (Button)findViewById(R.id.bt_6);
bt_5 = (Button)findViewById(R.id.bt_5);
bt_4 = (Button)findViewById(R.id.bt_4);
bt_3 = (Button)findViewById(R.id.bt_3);
bt_2 = (Button)findViewById(R.id.bt_2);
bt_1 = (Button)findViewById(R.id.bt_1);
bt_0 = (Button)findViewById(R.id.bt_0);
bt_jia = (Button)findViewById(R.id.bt_jia);
bt_jian = (Button)findViewById(R.id.bt_jian);
bt_cheng = (Button)findViewById(R.id.bt_cheng);
bt_chu = (Button)findViewById(R.id.bt_chu);
bt_dian = (Button)findViewById(R.id.bt_dian);
bt_deng = (Button)findViewById(R.id.bt_deng); bt_clear.setOnClickListener(this);
bt_goback.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_0.setOnClickListener(this);
bt_jia.setOnClickListener(this);
bt_jian.setOnClickListener(this);
bt_cheng.setOnClickListener(this);
bt_chu.setOnClickListener(this);
bt_dian.setOnClickListener(this);
bt_deng.setOnClickListener(this); }
//事件源组件
public void onClick(View v)
{
Button bt = (Button)v; int id = bt.getId(); switch (id)
{
case R.id.clear:
str_show = new StringBuilder(); //重新实例化就可以清除
//设置显示框内容
et.setText(str_show); //et:显示框 number_1 = null;
number_2 = null;
id_yunsf = 0; break; case R.id.goback: if (str_show.length()>0)
{
//删除最后一个字符 索引值减1
str_show.deleteCharAt(str_show.length()-1);
et.setText(str_show);
break;
} case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
case R.id.bt_6:
case R.id.bt_5:
case R.id.bt_4:
case R.id.bt_3:
case R.id.bt_2:
case R.id.bt_1:
case R.id.bt_0: if (id == R.id.bt_0
||(id == R.id.bt_0 && !str_show.toString().equals("0")))
{
str_show.append(bt.getText());
}
et.setText(str_show);
break;
case R.id.bt_deng:
case R.id.bt_jia:
case R.id.bt_jian:
case R.id.bt_cheng:
case R.id.bt_chu: if (number_1 == null)
{
if (str_show.length()>0)
{
//把字符串转成Double
number_1 = new Double(str_show.toString());
id_yunsf = bt.getId();
str_show = new StringBuilder(); //重新实例化就可以清除
//设置显示框内容
//et.setText(str_show); //et:显示框
}
}
else//计算
{
if (str_show.length()>0)
{
//把字符串转成Double 赋值
number_2 = new Double(str_show.toString());
} switch (id_yunsf)
{
case R.id.bt_jia:
//运算
double res = number_1.doubleValue() + number_2.doubleValue();
break; case R.id.bt_jian:
//运算
number_1 = number_1.doubleValue() - number_2.doubleValue();
break;
case R.id.bt_cheng:
//运算
number_1 = number_1.doubleValue() * number_2.doubleValue();
break;
case R.id.bt_chu:
//运算
if (number_2 !=0)
{
number_1 = number_1.doubleValue() / number_2.doubleValue();
}
else
{
Toast.makeText(this,"不能除0",Toast.LENGTH_LONG).show();
}
break;
}
//运算符
id_yunsf = bt.getId();
//显示当前运算结果
et.setText(number_1.toString());
//清空
str_show = new StringBuilder(); }
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4">
<!--行:rowCount 列:columnCount--> <EditText
android:layout_columnSpan="4"
android:layout_gravity="fill_horizontal"
android:layout_rowWeight="1"
android:textSize="30sp"
android:editable="false"
android:gravity="right|center_vertical"/>
<!--水平填充:fill_horizontal 竖直填充:fill_vertical
行号:layout_row 列号:layout_column(行号和列号都可以指定,但注意都是从0开始的)-->
<Button
android:text="清除"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/clear"/>
<Button
android:text="后退"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:textColor="#0ff"
android:id="@+id/goback"/>
<Button
android:text="/"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_chu"/>
<Button
android:text="x"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_cheng"/>
<Button
android:text="7"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_7"/>
<Button
android:text="8"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_8"/>
<Button
android:text="9"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_9"/>
<Button
android:text="-"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_jian"/>
<Button
android:text="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_4"/>
<Button
android:text="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_5"/>
<Button
android:text="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_6"/>
<Button
android:text="+"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_jia"/>
<Button
android:text="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_1"/>
<Button
android:text="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_2"/>
<Button
android:text="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:id="@+id/bt_3"/>
<Button
android:text="="
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20sp"
android:id="@+id/bt_deng"/>
<Button
android:text="0"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="20sp"
android:background="#FF0000"
android:id="@+id/bt_0"/>
<Button
android:text="."
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="20sp"
android:textColor="#FF0000"
android:id="@+id/bt_dian"/> </GridLayout>
这些代码可以实现计算器的基本功能
效果:

Android课程---计算器的实现的更多相关文章
- 第一个Android项目——计算器
第一个Android项目——计算器 效果 开始学Android一两个星期了,学习了一下基本的Activity.简单控件及几个简单布局,打算找个东西来练练手,于是就选择发计算器.关于计算器中用到的四则运 ...
- 一培训机构设计的学习android课程内容:供大家参考
转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...
- Android 简单计算器源码....
PS:今天算是闲着没事做了一个小型的计算器...顺便熟悉一下Android的布局,组件,以及时间监听的方法...就当是做了一个小小的练习吧... 顺便去对比了一下别人写的代码...有的使用到了 ...
- android实现计算器功能
设计一个简单的计算器. 第一个Activity的界面. 第二个Activity显示算式和计算结果. 第一个Activity代码: import android.app.Activity; import ...
- 【留念贴】Android开发——计算器
[过程] 在电商学霸&&代码女神XuFei的影响下,接触到了关于Android客户端的一些开发,第一次在Android平台搞出了一个App,真的是激动不已,所以必须开个留念贴记录一下. ...
- Android 简单计算器实现源码
1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...
- android的计算器
今天我闲着无聊,便想仿照Iphone的计算器自己写一个出来玩玩,于是就开动脑经,来场头脑风暴了!我拿什么了写呢?wp?这是个不错的选择,但是我最近在研究安卓的开发(求各位MSP不要烧我,我买不起MAC ...
- android课程第一节(TextView控件使用)
TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...
- Android学习(七) Android实现计算器
前台页面代码,通过线性布局方式实现计算器页面:如图所示 color.xml,自定义颜色values: <?xml version="1.0" encoding="u ...
随机推荐
- 【hdu3948-不同回文串的个数】后缀数组
题意:求不同回文串的个数 n<=10^5 题解: 先按照manacher的构造方法改造一遍串,然后跑一遍manacher. 如ababa--> $#a#b#a#b#a#@ 然后跑一遍后缀数 ...
- css整理-04 基本视觉格式化
基本框 假定每一个元素都会生成一个火多个矩形框,为元素框 元素框中心有一个内容区,周围有内边距,边距和外边距 内容的背景会应用到内边距,外边距是透明的,可以看到父元素的背景 内边距不能是负值,外边距可 ...
- jQuery-事件以及动画
事件: 1.//方法1 $(window).load(function(){ }) window.onload=function(){ } //方法2 function one(){ alert(&q ...
- TCP/IP详解2 学习笔记---mbuf
1,mbuf就是存储要发送数据的memery buf,类似于skb_buf.不过结构比较简单. /* header at beginning of each mbuf: */ 这个结构用来描述mbuf ...
- EF框架step by step(3)—Code-First
CodeFirst是EF框架的第三种方式,也是最为复杂一种方式,本文将以EF4.1版本为基础,简要讲解一下用法,同时,也介绍DbContext的用法. 本文采用的示例仍然是前两篇采用的博客用户的示例. ...
- 424 - Integer Inquiry
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...
- Java 读取配置文件 Properties
String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new Buff ...
- ACM spiral grid
spiral grid 时间限制:2000 ms | 内存限制:65535 KB 难度:4 描述 Xiaod has recently discovered the grid named &q ...
- 【BZOJ】1110: [POI2007]砝码Odw
题意 给定\(n\)个砝码和\(m(1 \le n, m \le 100000)\)个背包\((1 \le n_i, m_i \le 1000000000)\),保证对于任意两个砝码都有一个是另一个的 ...
- 获取jQuery对象的第N个DOM元素 && table常用css样式
获取jQuery对象的第N个DOM元素 1.$(selector).get(N-1) 2.$(selector)[N-1] 注意:.index()方法返回的是一个数,相当于C#中的IndexOf() ...