Android实现简单计算器
本文实例为大家分享了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; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。
【原URL http://www.yidunidc.com/kt.html 转载请说明出处】