<?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课程---计算器的实现的更多相关文章

  1. 第一个Android项目——计算器

    第一个Android项目——计算器 效果 开始学Android一两个星期了,学习了一下基本的Activity.简单控件及几个简单布局,打算找个东西来练练手,于是就选择发计算器.关于计算器中用到的四则运 ...

  2. 一培训机构设计的学习android课程内容:供大家参考

    转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...

  3. Android 简单计算器源码....

    PS:今天算是闲着没事做了一个小型的计算器...顺便熟悉一下Android的布局,组件,以及时间监听的方法...就当是做了一个小小的练习吧...     顺便去对比了一下别人写的代码...有的使用到了 ...

  4. android实现计算器功能

    设计一个简单的计算器. 第一个Activity的界面. 第二个Activity显示算式和计算结果. 第一个Activity代码: import android.app.Activity; import ...

  5. 【留念贴】Android开发——计算器

    [过程] 在电商学霸&&代码女神XuFei的影响下,接触到了关于Android客户端的一些开发,第一次在Android平台搞出了一个App,真的是激动不已,所以必须开个留念贴记录一下. ...

  6. Android 简单计算器实现源码

    1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...

  7. android的计算器

    今天我闲着无聊,便想仿照Iphone的计算器自己写一个出来玩玩,于是就开动脑经,来场头脑风暴了!我拿什么了写呢?wp?这是个不错的选择,但是我最近在研究安卓的开发(求各位MSP不要烧我,我买不起MAC ...

  8. android课程第一节(TextView控件使用)

    TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...

  9. Android学习(七) Android实现计算器

    前台页面代码,通过线性布局方式实现计算器页面:如图所示 color.xml,自定义颜色values: <?xml version="1.0" encoding="u ...

随机推荐

  1. POJ 2549 二分+HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...

  2. Java类与对象的基础学习

    1. 请输入并运行以下代码,得到什么结果? public class Test{ public static void main(String args[]){ Foo obj1=new Foo(); ...

  3. 怎样增加windows 系统的环境变量Path的默认长度的限制?

    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ...

  4. NumPy 学习(2): 数组的操作

    1. 简单一维数组的操作 一维数组的操作类似于python自身的list类型. In [14]: arr = np.arange(10) In [15]: arr Out[15]: array([0, ...

  5. SlidesJS的使用

    项目中对slideshow要求要有触屏滑动换图功能,就想到了SlidesJS这个Jquery插件 例排,先把静态html写好 <div id="cm_slides"> ...

  6. TCP/IP详解2 学习笔记---mbuf

    1,mbuf就是存储要发送数据的memery buf,类似于skb_buf.不过结构比较简单. /* header at beginning of each mbuf: */ 这个结构用来描述mbuf ...

  7. webvnc利器-noVNC集成实战

    我们在做一个虚拟机管理平台,底层虚拟化使用KVM,我们希望在网页上操作虚拟机. 一开始用applet嵌在网页内实现webvnc,不过由于applet的在浏览器上运行安全性限制,我们需要一个新的方案.此 ...

  8. javaEE基础

    1.拦截器与过滤器 过滤器(filter),过滤器处于客户端与Web资源(Servlet.JSP.HTML)之间,客户端与Web资源之间的请求和响应都要通过过滤器进行过滤.如过滤编码,IP 拦截器(i ...

  9. How to retrieve instance parameters from an uninstantiated (uninserted) family

    The trick to be able to read the default values for instance parameters is to get to the FamilyManag ...

  10. ccc 多点触控2

    经过不断的思考发现,如果是两个sprite都添加触控的时候,往往直接成单点触控, 但是如果是两个node的时候在node上面点击就会变成多点触控的形式 cc.Class({ extends: cc.C ...