android开发分享android计算器实现两位数的加减乘除

本文实例为大家分享了android计算器实现加减乘除的具体代码,供大家参考,具体内容如下注:以下计算器只注重实现功能,不考虑其他bug,只有两位整数的算法运算,适合新手1、实现思想将从键盘得到的数值放

android开发分享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);   }

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

关于android计算器功能的实现,查看专题:android计算器 进行学习。

以上就是android开发分享android计算器实现两位数的加减乘除的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

www.ctvol.com true Article android开发分享android计算器实现两位数的加减乘除

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/898707.html

(0)
上一篇 2021年10月22日 下午4:24
下一篇 2021年10月22日 下午4:26

精彩推荐