Android实现简单计算器
程序员文章站
2022-06-18 07:53:44
本文实例为大家分享了android实现简单计算器的具体代码,供大家参考,具体内容如下功能1、加减乘除四则运算2、归03、回退4、即时运算配置在build.gradle(app) 中加入下面的代码bui...
本文实例为大家分享了android实现简单计算器的具体代码,供大家参考,具体内容如下
功能
1、加减乘除四则运算
2、归0
3、回退
4、即时运算
配置
在build.gradle(app) 中加入下面的代码
buildfeatures { viewbinding = true }
加入位置如下所示
compileoptions { sourcecompatibility javaversion.version_1_8 targetcompatibility javaversion.version_1_8 } buildfeatures { viewbinding = true }
布局
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" android:layout_margin="5dp" android:text="计算器\nby csdn weixin_44423317 " android:textsize="20sp" android:gravity="right|center" android:padding="6dp" /> <view android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginleft="1dp" android:layout_marginright="1dp" android:background="#adadad"/> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8" android:orientation="vertical" android:gravity="center_horizontal" > <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <button android:id="@+id/ac" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textsize="20sp" android:textcolor="@color/black" android:text="ac" android:background="@color/white"/> <button android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:background="#00ffffff"/> <button android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:background="#00ffffff"/> <imagebutton android:id="@+id/dec" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textsize="20sp" android:textcolor="@color/black" android:layout_gravity="right" android:background="@color/white" android:src="@drawable/ic_baseline_backspace_24"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <button android:id="@+id/qi" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textsize="20sp" android:textcolor="@color/black" android:text="7" android:background="@color/white"/> <button android:id="@+id/ba" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textsize="20sp" android:textcolor="@color/black" android:text="8" android:background="@color/white"/> <button android:id="@+id/jiu" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:text="9" android:textsize="20sp" android:background="@color/white"/> <button android:id="@+id/mul" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textsize="20sp" android:textcolor="@color/black" android:text="x" android:background="@color/white"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <button android:id="@+id/si" android:layout_width="0dp" android:layout_weight="1" android:textsize="20sp" android:layout_height="match_parent" android:textcolor="@color/black" android:text="4" android:background="@color/white"/> <button android:id="@+id/wu" android:textsize="20sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:text="5" android:background="@color/white"/> <button android:id="@+id/liu" android:textsize="20sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:text="6" android:background="@color/white"/> <button android:id="@+id/div" android:layout_width="0dp" android:layout_weight="1" android:textsize="20sp" android:layout_height="match_parent" android:textcolor="@color/black" android:text="/" android:background="@color/white"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <button android:id="@+id/yi" android:layout_width="0dp" android:layout_weight="1" android:textsize="20sp" android:layout_height="match_parent" android:textcolor="@color/black" android:text="1" android:background="@color/white"/> <button android:id="@+id/er" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textsize="20sp" android:textcolor="@color/black" android:text="2" android:background="@color/white"/> <button android:id="@+id/san" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:textsize="20sp" android:text="3" android:background="@color/white"/> <button android:id="@+id/sub" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:textsize="20sp" android:text="-" android:background="@color/white"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <button android:id="@+id/zero" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:textsize="20sp" android:text="0" android:background="@color/white"/> <button android:id="@+id/point" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:textsize="20sp" android:text="." android:background="@color/white"/> <button android:id="@+id/equal" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:textsize="20sp" android:text="=" android:background="@color/white"/> <button android:id="@+id/add" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@color/black" android:textsize="20sp" android:text="+" android:background="@color/white"/> </linearlayout> </linearlayout> </linearlayout>
代码
public class mainactivity extends appcompatactivity implements view.onclicklistener { private string tv="0"; private activitymainbinding inflate; private string count=""; private boolean ismax=false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); inflate = activitymainbinding.inflate(getlayoutinflater()); setcontentview(inflate.getroot()); inflate.zero.setonclicklistener(this); inflate.yi.setonclicklistener(this); inflate.er.setonclicklistener(this); inflate.san.setonclicklistener(this); inflate.si.setonclicklistener(this); inflate.wu.setonclicklistener(this); inflate.liu.setonclicklistener(this); inflate.qi.setonclicklistener(this); inflate.ba.setonclicklistener(this); inflate.jiu.setonclicklistener(this); inflate.add.setonclicklistener(this); inflate.sub.setonclicklistener(this); inflate.mul.setonclicklistener(this); inflate.div.setonclicklistener(this); inflate.point.setonclicklistener(this); inflate.equal.setonclicklistener(this); inflate.dec.setonclicklistener(this); inflate.ac.setonclicklistener(this); } private void jisuanqi(){ list<string> strs=new arraylist<>(); stringbuilder f= new stringbuilder(); for(int i=0;i<tv.length();i++) { if(string.valueof(tv.charat(i)).equals("-")) { if(!f.tostring().equals("")){ strs.add(f.tostring()); strs.add("-"); f = new stringbuilder(); }else{ f.append("-"); } continue; } if(string.valueof(tv.charat(i)).matches("(\\d+)|(\\.)")) { f.append(string.valueof(tv.charat(i))); }else { strs.add(f.tostring()); strs.add(string.valueof(tv.charat(i))); f = new stringbuilder(); } } if(!f.tostring().equals("")) { strs.add(f.tostring()); } if(!strs.get(strs.size()-1).matches("(\\-|)?\\d+(\\.\\d*)?$")){ strs.remove(strs.size()-1); } double ans=number(strs,0); if(ans==0||ans%(int)ans==0){ count=(int)ans+""; }else { bigdecimal bigdecimal=new bigdecimal(ans); string a=bigdecimal.setscale(12,5).tostring(); if(ans-(int)ans!=0) { while (string.valueof(a.charat(a.length() - 1)).equals("0")){ a=a.substring(0,a.length()-1); } } count=a; } if(ismax){ ismax=false; count="∞"; } showtext(); } private void showtext(){ inflate.tv.settext(tv); spannablestring spantext = new spannablestring(count); spantext.setspan(new absolutesizespan(30, true), 0, spantext.length(), spannable.span_exclusive_exclusive); inflate.tv.append("\n"); inflate.tv.append(spantext); } private double number(list<string> n,int v) { double con = 0; int j=0; con=double.valueof(n.get(0)); if(v==0) { for(int i=0;i<n.size();i++) { if(n.get(i).matches("(\\-|)?\\d+(\\.\\d+)?$")) {//55+5*5/5-5= con=double.valueof(n.get(i)); if(n.size()>i+1) { j=i+1; } } if(n.get(j).equals("x")){ n.set(i, string.valueof(con*double.valueof(n.get(i+2)))); n.remove(i+1); n.remove(i+1); return number(n,0); }else if(n.get(j).equals("/")){ if(double.valueof(n.get(i+2))==0){ ismax=true; return 10; } n.set(i, string.valueof(con/double.valueof(n.get(i+2)))); n.remove(i+1); n.remove(i+1); return number(n,0); } } } for(int i=0;i<n.size();i++) { if(n.get(i).matches("(\\-|)?\\d+(\\.\\d+)?$")) {//55+5*5/5-5= con=double.valueof(n.get(i)); if(n.size()>i+1) { j=i+1; } } if(n.get(j).equals("+")){ n.set(i, string.valueof(con+double.valueof(n.get(i+2)))); n.remove(i+1); n.remove(i+1); return number(n,1); }else if(n.get(j).equals("-")){ n.set(i, string.valueof(con-double.valueof(n.get(i+2)))); n.remove(i+1); n.remove(i+1); return number(n,1); } } return con; } @suppresslint("nonconstantresourceid") @override public void onclick(view v) { switch (v.getid()){ case r.id.point: if(string.valueof(tv.charat(tv.length()-1)).matches("\\d+")){ tv+="."; inflate.tv.settext(tv); } break; case r.id.zero: case r.id.yi: case r.id.er: case r.id.san: case r.id.si: case r.id.wu: case r.id.liu: case r.id.qi: case r.id.ba: case r.id.jiu: if(tv.equals("0")){ tv=""; } button bt_digit=findviewbyid(v.getid()); tv+=bt_digit.gettext(); inflate.tv.settext(tv); count=tv; jisuanqi(); break; case r.id.sub: if(string.valueof(tv.charat(0)).equals("0")){ tv="-"; }else { if(string.valueof(tv.charat(tv.length()-1)).equals(".")){ tv += "0"; } tv+="-"; } showtext(); break; case r.id.add: case r.id.mul: case r.id.div: button bt_operator=findviewbyid(v.getid()); string laststr=string.valueof(tv.charat(tv.length()-1)); if(!laststr.matches("(\\d+)") ) { tv = tv.substring(0, tv.length() - 1) + bt_operator.gettext(); }else { if(laststr.equals(".")) { tv += "0"; } tv+=bt_operator.gettext(); } showtext(); break; case r.id.ac: tv="0"; inflate.tv.settext(tv); break; case r.id.dec: if(tv.length()>1) { tv = tv.substring(0, tv.length() - 1); }else { tv="0"; } jisuanqi(); break; case r.id.equal: if(!string.valueof(tv.charat(tv.length()-1)).matches("(\\d+)")){ tv = tv.substring(0, tv.length() - 1); } count=tv; jisuanqi(); tv=count; inflate.tv.settext(tv); break; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。