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 ...
随机推荐
- Linux IO模型和网络编程模型
术语概念描述: IO有内存IO.网络IO和磁盘IO三种,通常我们说的IO指的是后两者. 阻塞和非阻塞,是函数/方法的实现方式,即在数据就绪之前是立刻返回还是等待. 以文件IO为例,一个IO读过程是文件 ...
- 【第三方登录】之QQ第三方登录
最近公司做了个网站,需要用到第三方登录的东西.有QQ第三方登录,微信第三方登录.先把QQ第三方登录的代码列一下吧. public partial class QQBack : System.Web.U ...
- js动画实现透明度动画
在本次实例中,由于一般主流的浏览器对于透明度opacity最大值为1,但是在IE6最大值是100,此次例子是按主流浏览器的透明度来算的,所以定义的是小数,也可以定义为整数为单位,在运算的时候遇到主流的 ...
- 期望+DP ZOJ 3929 Deque and Balls
题目链接 题意:给你n个数,按照顺序依次放入一个双端队列(可放在头部,也可以放在尾部),求xi > xi+1的期望 * 2^n mod (1e9 +7) 分析:期望*2^n=出现这种排法的概率* ...
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- Monkeyrunner脚本中component快速定位方法
在编写MonkeyRunner脚本过程中,会出现component这一项内容,很多人可能不知道怎么确认,其实这个主要是为了指定要测试的程序包名和主Activity名,我们可以用以下方法去进行确认: 1 ...
- ccc let
let,其实就是块级作用域申明变量的var.之前JS的var关键字是非块级作用域的,而是函数级的. 例如arr=[0,1,2],我们经常写循环 for(var i=0,len=arr.length; ...
- 厉害了,摩托罗拉发布全球首款支持VR和AR的手机MotoZ
目前支持谷歌daydream移动VR生态系统的手机型号并不多,除了谷歌自家的Pixel系列外,还有华为推出的mate9系列.如今又到了一位新成员,即升级到Android 7.0后的Moto Z. 更为 ...
- font 和 text 字和文本操作
- c++实现des算法
程序分三部分,des头文件,des类实现,main函数调用. //panda //2013-4-13 //des //des.h class DES { private: //public: //明文 ...