android计算器实现两位数的加减乘除

本文实例为大家分享了android计算器实现加减乘除的具体代码,供大家参考,具体内容如下

注:以下计算器只注重实现功能,不考虑其他BUG,只有两位整数的算法运算,适合新手

1、实现思想

将从键盘得到的数值放在一个字符数组中,以运算符号(+-/)为分割点,将两个数值分割开,进行算法运算。*

2、难点

如何判断是否为符号?+ - ×/
记录符号的位置?

3、步骤:

1、得到键盘输入的值
2、将值存放在一个字符数组中
3、遍历数组中的每个数,如果找到算法符号,记录下算法符号的位置。(要点,从0开始)
4、将算法符号前面的数放在一个定义的int型数中
5、同理
6、判断是加减乘除的哪一个方法,然后进行简单的运算。

4、代码

i:布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:weightSum="1"
 >

 <TextView android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/etResult"
  android:layout_weight="0.05"
  android:textSize="25dp"
  android:paddingTop="10dp"
  android:gravity="bottom"
  android:hint="0.0"
  />
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_weight="0.8">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:weightSum="1">

  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="C"
  android:textSize="25dp"
  android:background="@color/colorWhite"
  android:id="@+id/btnQingchu"
  android:layout_weight="0.5" />
  <Button
  android:layout_width="235dp"
  android:layout_height="wrap_content"
  android:text="←"
  android:textSize="25dp"
  android:background="@color/colorBlue"
  android:id="@+id/btnHuishan"
  android:layout_weight="0.5"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn7"
  android:text="7"
  android:textSize="25dp"
  android:layout_weight="0.25"
  />
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn8"
  android:text="8"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn9"
  android:text="9"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btnChu"
  android:text="÷"
  android:textSize="25dp"
  android:background="@color/colorBlue"
  android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn4"
  android:text="4"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn5"
  android:text="5"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn6"
  android:text="6"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btnCheng"
  android:text="×"
  android:textSize="25dp"
  android:background="@color/colorBlue"
  android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn1"
  android:text="1"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn2"
  android:text="2"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn3"
  android:text="3"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btnJian"
  android:text="-"
  android:textSize="25dp"
  android:background="@color/colorBlue"
  android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn0"
  android:text="0"
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btnDian"
  android:text="."
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btnDengyu"
  android:text="="
  android:textSize="25dp"
  android:layout_weight="0.25"/>
  <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btnJia"
  android:text="+"
  android:textSize="25dp"
  android:background="@color/colorBlue"
  android:layout_weight="0.25"/>
 </LinearLayout>
 </LinearLayout>
</LinearLayout>

ii:获取键盘的值,写监听

public void getButton(){
 //获取按钮组件
 btn0= (Button) findViewById(R.id.btn0);
 btn1= (Button) findViewById(R.id.btn1);
 btn2= (Button) findViewById(R.id.btn2);
 btn3= (Button) findViewById(R.id.btn3);
 btn4= (Button) findViewById(R.id.btn4);
 btn5= (Button) findViewById(R.id.btn5);
 btn6= (Button) findViewById(R.id.btn6);
 btn7= (Button) findViewById(R.id.btn7);
 btn8= (Button) findViewById(R.id.btn8);
 btn9= (Button) findViewById(R.id.btn9);

 btnJia= (Button) findViewById(R.id.btnJia);
 btnJian= (Button) findViewById(R.id.btnJian);
 btnCheng= (Button) findViewById(R.id.btnCheng);
 btnChu= (Button) findViewById(R.id.btnChu);

 btnDian= (Button) findViewById(R.id.btnDian);
 btnDengyu= (Button) findViewById(R.id.btnDengyu);
 btnQingchu= (Button) findViewById(R.id.btnQingchu);
 btnHuishan= (Button) findViewById(R.id.btnHuishan);

 etGet = (TextView) findViewById(R.id.etResult);
 //绑定监听
 btn0.setOnClickListener(this);
 btn1.setOnClickListener(this);
 btn2.setOnClickListener(this);
 btn3.setOnClickListener(this);
 btn4.setOnClickListener(this);
 btn5.setOnClickListener(this);
 btn6.setOnClickListener(this);
 btn7.setOnClickListener(this);
 btn8.setOnClickListener(this);
 btn9.setOnClickListener(this);

 btnJia.setOnClickListener(this);
 btnJian.setOnClickListener(this);
 btnCheng.setOnClickListener(this);
 btnChu.setOnClickListener(this);

 btnDian.setOnClickListener(this);
 btnDengyu.setOnClickListener(this);
 btnQingchu.setOnClickListener(this);
 btnHuishan.setOnClickListener(this);
 }

iii:绑定按钮

 @Override
 public void onClick(View v) {
 str = etGet.getText().toString();
 switch (v.getId()){
  //数字按钮
  case R.id.btn0:
  case R.id.btn1:
  case R.id.btn2:
  case R.id.btn3:
  case R.id.btn4:
  case R.id.btn5:
  case R.id.btn6:
  case R.id.btn7:
  case R.id.btn8:
  case R.id.btn9:
  /* if (b_Clean)
  {
   b_Clean =false;
   etGet.setText("");
  }*/
  etGet.setText(str+((Button)v).getText());
  break;
  //运算按钮
  case R.id.btnJia:
  case R.id.btnJian:
  case R.id.btnCheng:
  case R.id.btnChu:
  case R.id.btnDian:
  /* if (b_Clean)
  {
   b_Clean =false;
   etGet.setText("");
  }*/
  etGet.setText(str+((Button)v).getText());
  break;
  //清除按钮
  case R.id.btnQingchu:
  /* if (b_Clean)
  {
   b_Clean =false;
   etGet.setText("");
  }*/
  etGet.setText("");
  break;
  case R.id.btnDengyu:
  getResult();
  break;
  case R.id.btnHuishan:
  str=etGet.getText().toString();
  try {
   etGet.setText(str.substring(0,str.length()-1));
  }
  catch (Exception e){
   etGet.setText("");
  }

  break;

 }
 }

iV:算法功能实现

public void getResult(){
 str = etGet.getText().toString();
 strArray = new String[str.length()]; //将得到的字符串放在一个字符数组里
 //System.out.println("str"+str);
 int n=0;
 for(int i=0; i<strArray.length;i++){
  strArray[i] = str.substring(i, i+1); //遍历数组的每个元素
  //System.out.print(strArray[i]);
  if(strArray[i].equals("+")||strArray[i].equals("-") //满足条件
   ||strArray[i].equals("×")||strArray[i].equals("÷"))
  {
  n= i; //记录符号存在的位置
  }
 }
 int num1 = Integer.parseInt(str.substring(0,n)); //得到前一串数
 String caculate = str.substring(n,n+1); //得到算法符号,加减乘除
 int num2 = Integer.parseInt(str.substring(n+1)); //得到后一串数

 if (caculate.equals("+"))
 {
  Inputresult = num1+num2;
 }
 else if (caculate.equals("-"))
 {
  Inputresult = num1-num2;
 }
 else if (caculate.equals("×"))
 {
  Inputresult = num1*num2;
 }
 else if (caculate.equals("÷"))
 {
  if (num2==0)
  {
  return ;
  }
  Inputresult = num1/num2;
 }
 etGet.setText(num1+caculate+num2+"="+Inputresult);
 }

更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android实现简单计算器功能

    本文实例为大家分享了android实现简单计算器的具体代码,供大家参考,具体内容如下 // 日期:2014/9/26 // 首先,人们的输入习惯为中缀表达式.为了便于计算,程序会将中缀表达式会转换为后缀表达式 //////////////////////////////////////////////////////////////////////////////////////// // 目前软件还存在 // 1.输入运算数和运算符不匹配时,崩溃的现象.(如:只输入一个操作数) // 2.一个

  • 从零开始学android实现计算器功能示例分享(计算器源码)

    下面是效果展示: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match

  • Android实现简单加法计算器

    本文实例为大家分享了Android实现简单加法计算器的具体代码,供大家参考,具体内容如下 package com.example.calculator; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; impo

  • Android实现加法计算器

    本文实例为大家分享了Android实现加法计算器的具体代码,供大家参考,具体内容如下 布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layo

  • Android studio实现简单计算器

    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析 在Android studio中设计并实现一个简单的计算器,实现连续的加减乘除运算. 界面设计 采用网格GridLayout布局,设计了一个6行4列的网格,最上边是一个EditText用来显示用户输入的运算数字和运算符,以及相关的运算结果,其占4列,文本框大小为50dip:依次往下的界面分别设置了数字和运算符以及操作的按钮,各行各列的每个按钮的大小均设为26sp. 编程分析 设计了两个文

  • Android实现简易计算器小程序

    本文实例为大家分享了Android实现简易计算器小程序的具体代码,供大家参考,具体内容如下 目标效果: 通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能. 1.页面中Button使用的是线性布局,最外边一个是父布局,第一行C,DEL,/,*为第一个子布局,第二行7,8,9,-为第二个子布局,第三行4,5,6,+为第三个子布局,第四五行为第四个子布局,第四个子布局中还有两个相当于是孙布局的级别,1,2,3为第一个孙布局,0和.为第二个孙布局,=在两个孙布局之外第四个子布局以内.

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

    本文实例讲述了Android开发实现的简单计算器功能.分享给大家供大家参考,具体如下: 这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦: 先上图看效果 比较简单,所以我就没怎么写注释,应该一看就能明白的 有不明白的可以发信问我 先贴MainActivity.java代码 package com.example.calculator; import android.app.Activity; import android.os.Bundle; import andro

  • android计算器简单实现代码

    本文实例为大家分享了android计算器的具体实现代码,供大家参考,具体内容如下 java代码: package com.itheima74.simplecalculator4; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; imp

  • Android Studio实现简单计算器APP

    一.简介:用Android Studio 实现一个简单的计算器APP,并在蓝叠模拟器中运行. 该计算器只能实现两位数字的四则运算. 二.代码 activity_main.xml   ---界面设计 <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t

  • Android studio设计简易计算器

    本文实例为大家分享了Android studio设计简易计算器的具体代码,供大家参考,具体内容如下 效果显示: 第一步,简单的界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and

随机推荐