Android开发实现的简单计算器功能【附完整demo源码下载】

 更新时间:2017年11月29日 08:54:36   作者:zml_2015  
这篇文章主要介绍了Android开发实现的简单计算器功能,结合实例形式分析了Android计算器的具体实现步骤与相关操作技巧,并附带完整demo源码供读者下载参考,需要的朋友可以参考下

本文实例讲述了Android开发实现的简单计算器功能。分享给大家供大家参考,具体如下:

这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦;

先上图看效果

比较简单,所以我就没怎么写注释,应该一看就能明白的
有不明白的可以发信问我

先贴MainActivity.java代码

package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
  Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bp, bs, bm, bd, bc, be;
  ImageView delete;
  TextView tv;
  EditText show;
  String showString = "", option = "";
  int showfirst = 0;
  String exception = "";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b0 = (Button) findViewById(R.id.bt_0);
    b1 = (Button) findViewById(R.id.bt_1);
    b2 = (Button) findViewById(R.id.bt_2);
    b3 = (Button) findViewById(R.id.bt_3);
    b4 = (Button) findViewById(R.id.bt_4);
    b5 = (Button) findViewById(R.id.bt_5);
    b6 = (Button) findViewById(R.id.bt_6);
    b7 = (Button) findViewById(R.id.bt_7);
    b8 = (Button) findViewById(R.id.bt_8);
    b9 = (Button) findViewById(R.id.bt_9);
    bp = (Button) findViewById(R.id.bt_plus);
    bs = (Button) findViewById(R.id.bt_sub);
    bm = (Button) findViewById(R.id.bt_mutilate);
    bd = (Button) findViewById(R.id.bt_div);
    bc = (Button) findViewById(R.id.bt_c);
    be = (Button) findViewById(R.id.bt_equ);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    b4.setOnClickListener(this);
    b5.setOnClickListener(this);
    b6.setOnClickListener(this);
    b7.setOnClickListener(this);
    b8.setOnClickListener(this);
    b9.setOnClickListener(this);
    b0.setOnClickListener(this);
    bp.setOnClickListener(this);
    bs.setOnClickListener(this);
    bm.setOnClickListener(this);
    bd.setOnClickListener(this);
    bc.setOnClickListener(this);
    be.setOnClickListener(this);
    show = (EditText) findViewById(R.id.et_show);
    delete = (ImageView) findViewById(R.id.iv_delete);
    delete.setOnClickListener(this);
    tv=(TextView) findViewById(R.id.author);
    tv.setOnClickListener(this);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bt_0:
      showString += "0";
      break;
    case R.id.bt_1:
      showString += "1";
      break;
    case R.id.bt_2:
      showString += "2";
      break;
    case R.id.bt_3:
      showString += "3";
      break;
    case R.id.bt_4:
      showString += "4";
      break;
    case R.id.bt_5:
      showString += "5";
      break;
    case R.id.bt_6:
      showString += "6";
      break;
    case R.id.bt_7:
      showString += "7";
      break;
    case R.id.bt_8:
      showString += "8";
      break;
    case R.id.bt_9:
      showString += "9";
      break;
    case R.id.bt_plus:
      if (showString.equals(""))
        exception = "先输入数值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "+";
      }
      break;
    case R.id.bt_sub:
      if (showString.equals(""))
        exception = "先输入数值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "-";
      }
      break;
    case R.id.bt_mutilate:
      if (showString.equals(""))
        exception = "先输入数值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "*";
      }
      break;
    case R.id.bt_div:
      if (showString.equals(""))
        exception = "先输入数值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "/";
      }
      break;
    case R.id.bt_equ:
      if (option.equals("+"))
        showString = showfirst + Integer.parseInt(showString) + "";
      else if (option.equals("-")) {
        showString = showfirst - Integer.parseInt(showString) + "";
      } else if (option.equals("*")) {
        showString = showfirst * Integer.parseInt(showString) + "";
      } else if (option.equals("/")) {
        if (showString.equals("0")) {
          exception = "除数不能为0!";
        } else
          showString = showfirst / Integer.parseInt(showString) + "";
      }
      break;
    case R.id.bt_c:
      showString = "";
      break;
    case R.id.iv_delete:
      Toast.makeText(MainActivity.this, showString + "已被清空",
          Toast.LENGTH_SHORT).show();
      showString = "";
      break;
    case R.id.author:
      Toast.makeText(MainActivity.this, "郑明亮\n软件工程\nQQ:1072307340",
          Toast.LENGTH_SHORT).show();
      break;
    default:
      break;
    }
    if (exception.equals(""))
      show.setText(showString);
    else {
      show.setText(exception);
      exception = "";
    }
    // 设置文本框颜色;
    if (!show.getText().toString().equals("")) {
      delete.setBackgroundColor(R.drawable.delete_gray);
    }
    else {
      delete.setBackgroundResource(R.drawable.delete);
    }
  }
}

再贴布局activity_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"
  tools:context="com.example.calculator.MainActivity" >
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <EditText
      android:id="@+id/et_show"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:hint="请输入数字" />
    <ImageView
      android:id="@+id/iv_delete"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@id/et_show"
      android:src="@drawable/delete_and_deletegray" >
    </ImageView>
  </RelativeLayout>
  <GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_show"
    android:focusable="false"
    android:gravity="center"
    android:layout_marginTop="25dp"
    android:columnCount="4"
    android:horizontalSpacing="0dp"
    android:orientation="horizontal"
    android:stretchMode="none" >
    <Button
      android:id="@+id/bt_1"
      android:layout_height="wrap_content"
      android:text="1" />
    <Button
      android:id="@+id/bt_2"
      android:text="2" />
    <Button
      android:id="@+id/bt_3"
      android:text="3" />
    <Button
      android:id="@+id/bt_div"
      android:text="/" />
    <Button
      android:id="@+id/bt_4"
      android:text="4" />
    <Button
      android:id="@+id/bt_5"
      android:text="5" />
    <Button
      android:id="@+id/bt_6"
      android:text="6" />
    <Button
      android:id="@+id/bt_mutilate"
      android:text="X" />
    <Button
      android:id="@+id/bt_7"
      android:text="7" />
    <Button
      android:id="@+id/bt_8"
      android:text="8" />
    <Button
      android:id="@+id/bt_9"
      android:text="9" />
    <Button
      android:id="@+id/bt_sub"
      android:text="-" />
    <Button
      android:id="@+id/bt_0"
      android:layout_columnSpan="2"
      android:layout_gravity="fill_horizontal"
      android:text="0"
      android:width="2dp" />
    <Button
      android:id="@+id/bt_c"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="C" />
    <Button
      android:id="@+id/bt_plus"
      android:layout_gravity="fill_vertical"
      android:layout_rowSpan="2"
      android:text="+" />
    <Button
      android:id="@+id/bt_equ"
      android:layout_columnSpan="3"
      android:layout_gravity="fill_horizontal"
      android:text="=" />
  </GridLayout>
  <TextView
    android:id="@+id/author"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Author:Bri"
    />
  <TextView
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test:www.jb51.net"
    />
</LinearLayout>

我还写了一个drawable的xml,自己看吧

delete_and_deletegray.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/delete_gray" android:state_focused="false" android:state_pressed="false"></item>
  <item android:drawable="@drawable/delete" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
  <item android:drawable="@drawable/delete" android:state_pressed="true" android:state_selected="false"/>
  <item android:drawable="@drawable/delete" android:state_focused="true" android:state_pressed="true"/>
</selector>

附:完整实例代码点击此处本站下载

PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue

在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • android基本控件ToggleButton&Switch使用指南

    android基本控件ToggleButton&Switch使用指南

    本文给大家汇总介绍了android的2个基本控件ToggleButton和Switch的使用方法,非常的详细,有需要的小伙伴可以参考下。
    2016-01-01
  • Android使用URL读取网络资源的方法

    Android使用URL读取网络资源的方法

    这篇文章主要为大家详细介绍了Android使用URL读取网络资源的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android中gravity与layout_gravity的使用区别分析

    Android中gravity与layout_gravity的使用区别分析

    本篇文章介绍了,在Android中gravity与layout_gravity的使用区别分析。需要的朋友参考下
    2013-04-04
  • 基于Android Flutter编写贪吃蛇游戏

    基于Android Flutter编写贪吃蛇游戏

    贪吃蛇是一款足够经典的游戏。本文将利用Android中的Flutter编写这一经典的小游戏,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-03-03
  • Android实现密码明密文切换(小眼睛)

    Android实现密码明密文切换(小眼睛)

    这篇文章主要为大家详细介绍了Android实现密码明密文切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Android中Glide实现超简单的图片下载功能

    Android中Glide实现超简单的图片下载功能

    本篇文章主要介绍了Android中Glide实现超简单的图片下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • 修改Android中hosts文件的步骤详解

    修改Android中hosts文件的步骤详解

    有朋友问Android怎么修改Hosts?对于这个问题,由于手头并没有Android设备,所以只能从网上搜罗了方法并总结出来,下面这篇文章主要介绍了修改Android中hosts文件的步骤,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • 详解Android studio中正确引入so文件的方法

    详解Android studio中正确引入so文件的方法

    本篇文章主要介绍了Android studio中正确引入so文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Android自定义View实现抖音飘动红心效果

    Android自定义View实现抖音飘动红心效果

    这篇文章主要为大家详细介绍了Android自定义View实现抖音飘动红心效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • 如何正确使用Android线程详解

    如何正确使用Android线程详解

    线程是程序员进阶的一道重要门槛。除了了解各类开线程的API之外,更需要理解线程本身到底是个什么样的存在,并行是否真的高效?系统是怎么样去调度线程的?开线程的方式那么多,什么样的姿势才正确?下面通过本文来好好再学习下。
    2016-08-08

最新评论