Android简单计算器开发实例如图:

====================================================

activity_main.xml 代码如下:

<TableLayout  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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"

/>
    
    <TableRow>
        <EditText android:id="@+id/result" android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:layout_span="4" android:textSize="40sp" android:gravity="right|center_vertical"
            android:cursorVisible="false" android:editable="false" android:lines="1" />
    </TableRow>

<TableRow>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textSize="42sp" android:layout_weight="1">
            <Button android:id="@+id/num7" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="7" android:layout_weight="1" />
            <Button android:id="@+id/num8" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="8" android:layout_weight="1" />
            <Button android:id="@+id/num9" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="9" android:layout_weight="1" />
            <Button android:id="@+id/divide" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="/" android:layout_weight="1" />
        </LinearLayout>
    </TableRow>

<TableRow>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textSize="42sp" android:layout_weight="1">
            <Button android:id="@+id/num4" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="4" android:layout_weight="1" />
            <Button android:id="@+id/num5" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="5" android:layout_weight="1" />
            <Button android:id="@+id/num6" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="6" android:layout_weight="1" />
            <Button android:id="@+id/multiply" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="*" android:layout_weight="1" />
        </LinearLayout>
    </TableRow>

<TableRow>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textSize="42sp" android:layout_weight="1">
            <Button android:id="@+id/num1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="1" android:layout_weight="1" />
            <Button android:id="@+id/num2" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="2" android:layout_weight="1" />
            <Button android:id="@+id/num3" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="3" android:layout_weight="1" />
            <Button android:id="@+id/subtract" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="-" android:layout_weight="1" />
        </LinearLayout>
    </TableRow>

<TableRow>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textSize="42sp" android:layout_weight="1">
            <Button android:id="@+id/num0" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="0" android:layout_weight="1" />
            <Button android:id="@+id/point" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="." android:layout_weight="1" />
            <Button android:id="@+id/add" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="+" android:layout_weight="1" />
            <Button android:id="@+id/equal" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="42sp"
                android:text="=" android:layout_weight="1" />
        </LinearLayout>
    </TableRow>

<TableRow>
        <Button android:id="@+id/clear" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:textSize="30sp"
            android:text="clear" android:layout_span="4" android:gravity="center_vertical|center_horizontal"/>
    </TableRow>

</TableLayout >

====================================================================

MainActivity 类代码如下:

package com.example.jisuanqi;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity {

private Button[] btnNum = new Button[11];// 数值按钮 
     private Button[] btnCommand = new Button[5];// 符号按钮 
     private EditText editText = null;// 显示区域 
     private Button btnClear = null; // clear按钮 
     private String lastCommand; // 用于保存运算符 
     private boolean clearFlag; // 用于判断是否清空显示区域的值,true需要,false不需要 
     private boolean firstFlag; // 用于判断是否是首次输入,true首次,false不是首次 
     private double result; // 计算结果

public void calculator() {
         // 初始化各项值 
         result = 0; // x的值 
         firstFlag = true; // 是首次运算 
         clearFlag = false; // 不需要清空 
         lastCommand = "="; // 运算符 
     }

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  // 获取运算符 
        btnCommand[0] = (Button) findViewById(R.id.add);
        btnCommand[1] = (Button) findViewById(R.id.subtract);
        btnCommand[2] = (Button) findViewById(R.id.multiply);
        btnCommand[3] = (Button) findViewById(R.id.divide);
        btnCommand[4] = (Button) findViewById(R.id.equal);

// 获取数字 
        btnNum[0] = (Button) findViewById(R.id.num0);
        btnNum[1] = (Button) findViewById(R.id.num1);
        btnNum[2] = (Button) findViewById(R.id.num2);
        btnNum[3] = (Button) findViewById(R.id.num3);
        btnNum[4] = (Button) findViewById(R.id.num4);
        btnNum[5] = (Button) findViewById(R.id.num5);
        btnNum[6] = (Button) findViewById(R.id.num6);
        btnNum[7] = (Button) findViewById(R.id.num7);
        btnNum[8] = (Button) findViewById(R.id.num8);
        btnNum[9] = (Button) findViewById(R.id.num9);
        btnNum[10] = (Button) findViewById(R.id.point);
       
     // 初始化显示结果区域 
        editText = (EditText) findViewById(R.id.result);
        editText.setText("0.0");
        // 实例化监听器对象 
        NumberAction na = new NumberAction();
        CommandAction ca = new CommandAction();
        for (Button bc : btnCommand) {
            bc.setOnClickListener(ca);
        }
        for (Button bc : btnNum) {
            bc.setOnClickListener(na);
        }

// clear按钮的动作 
        btnClear = (Button) findViewById(R.id.clear);
        btnClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("0.0");
                // 初始化各项值 
                result = 0; // x的值 
                firstFlag = true; // 是首次运算 
                clearFlag = false; // 不需要清空 
                lastCommand = "="; // 运算符 
            }
        });
 }

// 数字按钮监听器 
    private class NumberAction implements OnClickListener {
        @Override
        public void onClick(View view) {
            Button btn = (Button) view;
            String input = btn.getText().toString();
            if (firstFlag) { // 首次输入 
                // 一上就".",就什么也不做 
                if (input.equals(".")) {
                    return;
                }
                // 如果是"0.0"的话,就清空 
                if (editText.getText().toString().equals("0.0")) {
                    editText.setText("");
                }
                firstFlag = false;// 改变是否首次输入的标记值 
            } else {
                String editTextStr = editText.getText().toString();
                // 判断显示区域的值里面是否已经有".",如果有,输入的又是".",就什么都不做 
                if (editTextStr.indexOf(".") != -1 && input.equals(".")) {
                    return;
                }
                // 判断显示区域的值里面只有"-",输入的又是".",就什么都不做 
                if (editTextStr.equals("-") && input.equals(".")) {
                    return;
                }
                // 判断显示区域的值如果是"0",输入的不是".",就什么也不做 
                if (editTextStr.equals("0") && !input.equals(".")) {
                    return;
                }
            }
            // 如果我点击了运算符以后,再输入数字的话,就要清空显示区域的值 
            if (clearFlag) {
                editText.setText("");
                clearFlag = false;// 还原初始值,不需要清空 
            }
            editText.setText(editText.getText().toString() + input);// 设置显示区域的值 
        }
    }

// 符号按钮监听器 
    private class CommandAction implements OnClickListener {
        @Override
        public void onClick(View view) {
            Button btn = (Button) view;
            String inputCommand = (String) btn.getText();
            if (firstFlag) {// 首次输入"-"的情况 
                if (inputCommand.equals("-")) {
                    editText.setText("-");// 显示区域的内容设置为"-" 
                    firstFlag = false;// 改变首次输入的标记 
                }
            } else {
                if (!clearFlag) {// 如果flag=false不需要清空显示区的值,就调用方法计算 
                    calculate(Double.parseDouble(editText.getText().toString()));// 保存显示区域的值,并计算 
                }
                // 保存你点击的运算符 
                lastCommand = inputCommand;
                clearFlag = true;// 因为我这里已经输入过运算符, 
            }
        }
    }

// 计算用的方法 
    private void calculate(double x) { 
         
        if (lastCommand.equals("+")) {
            result += x;
        } else if (lastCommand.equals("-")) {
            result -= x;
        } else if (lastCommand.equals("*")) {
            result *= x;
        } else if (lastCommand.equals("/")) {
            result /= x;
        } else if (lastCommand.equals("=")) {
            result = x;
        }
        editText.setText("" + result);
    }

}

Android计算器开发实例的更多相关文章

  1. android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)

    如有错漏请不吝拍砖指正,转载请注明出处,很感谢 桌面便签软件是android上经常使用软件的一种,比方比較早的Sticky Note,就曾很流行, Sticky Note的介绍能够參见 http:// ...

  2. Android 应用开发实例之情景模式

    2013-07-01 Android 应用开发实例 1. 情景模式 使用TabHost来实现主界面的布局. 设置一组RadioButton来切换不同的情景模式. 对比普通情景模式,定时情景模式需要加上 ...

  3. Android NDK开发实例教程

    WINDOWS系统+ Eclipse + NDK+Android 最近开始学习Android平台开发,Android还没有玩转,Java也是刚入门,这又要开始在Android中调用C语言,需要利用ND ...

  4. android 浏览器开发实例

    android app需要通过手机显示网页信息还是比较常用的,比如我最近业余开发的 抢商铺游戏,需要对游戏规则做说明,规则会比较多,而且要经常变动,就想到用网页来展示,更新起来方便,不像应用,一旦发布 ...

  5. Android应用开发实例篇(1)-----简易涂鸦板

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378328.html 一.概述 这次要做一个简单的涂鸦板应用,以前在Qt上实现过,突然想 ...

  6. Android网络开发实例(基于抓包实现的网络模拟登录,登出和强制登出)

    学习Android有几个月了,最近喜欢上了网络编程,于是想通过Android写一些一个小程序用于连接外网.在这里非常感谢雪夜圣诞的支持,非常感谢,给我打开新的一扇门. 1.声明,本程序只能用于西南大学 ...

  7. Android 应用开发实例之文件管理器

    2013-07-02 10.2 文件管理器 能够浏览和管理手机/存储卡上的文件和文件夹,包括重命名.删除.新建.复制.粘帖等文件操作. 由于需要浏览大量的文件/文件夹,所以需要使用一个ListView ...

  8. Android NFC开发概述

    NFC手机相比普通手机来说,有以下3个附加功能:  1.可以当成POS机来用,也就是“读取”模式   2.可以当成一张卡来刷,也就是NFC技术最核心的移动支付功能  3.可以像蓝牙.Wi-Fi一样做点 ...

  9. Android音乐播放器的开发实例

    本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的ListView和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...

随机推荐

  1. python2.X和3.X的一些区别【整理中】

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可  以取得很好的优化结果.  Py3.1性能比P ...

  2. 【JavaEE企业应用实战学习记录】MyGetAttributeListener

    package sanglp.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttr ...

  3. github page 和 hexo 搭建在线博客

    目录: 安装node.js与git 常用git命令 安装hexo 配置hexo hexo发布到github 1.安装node.js和git工具 https://nodejs.org/en/ 直接下载安 ...

  4. 第七章 java基础类库

    1. 日期时间: 用Calendar类. 2. 分隔符:空格.tab.回车. 3. Scanner:读取键盘输入.读取文件. 4. 系统类: System  Runtime. 5. 所有的java类都 ...

  5. Ceph Zabbix plugin 插件和模板

    由于Ceph项目中的 Celemeter 缺乏告警功能和监控平台的统一性要求, YY 云平台Ceph集群的监控需求,都是在团队已有的zabbix平台基础上开发完成的. 在已有的git开源项目基础上做了 ...

  6. iOS开发小技巧--TableView中headerView的循环利用,以及自定义的headerView

    一.首先要搞清楚,tableView中有两种headerView,一个是tableHeaderView,另一个是headerView.前者就一个;后者根据session决定个数 headerView的 ...

  7. python之旅【第一篇】

    python简介 python的起源 追溯Python语言的起源,是从20世纪90年代初由Guido van Rossum,在阿姆斯特丹,开发一个新的脚本解释程序.不知道Guido当初有没有想到,Py ...

  8. hibernate DetachedCriteria实现多表关联查询createAlias的使用

    记录本例查询初衷: 有表: 表1,表2,表3 关系 1 many-to-one 2 2 many-to-one 3 结果:要通过表3中的条件反向查询表1中相关的数据 public Page<We ...

  9. java的重写规则

    重写不能破坏父类的访问性和逻辑结构性.对于异常重写方法不能抛出新的异常或者比被重写方法声明的检查异常更广的检查异常.但是可以抛出更少,更有限或者不抛出异常. 重写规则之一:重写方法不能比被重写方法限制 ...

  10. Python 学习笔记9(装饰器,decorator)

    31 装饰器 装饰器可以对一个函数.方法或者类进行加工,是一种高级的python语法. 装饰函数 接收一个可调用对象作为输入参数,并返回一个新的可调用对象. 把函数传递给装饰器,然后增加新的功能,返回 ...