今天室友说网上找了好多Android计算器发现不能使用,就将自己写的计算器发一份上来。不过布局比较简单,而且只有+,-,*,/的功能。
布局文件activity_main.xml(采用的是线性布局)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--显示器-->
<TextView
android:id="@+id/show_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e4e3e3"
android:textSize="12pt"/>
<!--输入按钮-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<!--第一行-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:id="@+id/divide_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="12pt"
/>
<Button
android:id="@+id/multiply_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="12pt"
/>
<Button
android:id="@+id/minus_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="12pt"
/>
<Button
android:id="@+id/delete_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="x"
android:textSize="12pt"
/>
</LinearLayout>
<!--第二行-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/seven_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="12pt"
/>
<Button
android:id="@+id/eight_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="12pt"
/>
<Button
android:id="@+id/nine_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="12pt"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/four_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="12pt"
/>
<Button
android:id="@+id/five_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="12pt"
/>
<Button
android:id="@+id/six_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="12pt"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/plus_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="+"
android:textSize="12pt"
/>
</LinearLayout>
</LinearLayout>
<!--第三行-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/one_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="12pt"
/>
<Button
android:id="@+id/two_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="12pt"
/>
<Button
android:id="@+id/three_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="12pt"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/front_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="(-"
android:textSize="12pt"
/>
<Button
android:id="@+id/zero_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="12pt"
/>
<Button
android:id="@+id/point_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="12pt"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/equal_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="="
android:textSize="12pt"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--输入按钮结束-->
</LinearLayout>
</android.support.constraint.ConstraintLayout>
效果图
Main_activity.java 包含获取点击事件,进行计算的主函数
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //判断小数点 private static final Integer flag=-2; private List<Button> btnList=new ArrayList<Button>(); private TextView show_text; private String msg=""; private Float result=null; //是否按过等号键 private Boolean isEauql=false; //存放数字 private static List<Float>digitList=new ArrayList<Float>(); //存放运算符 private static List<Entity> operatorList=new ArrayList<Entity>(); @Override public void onClick(View v){ if(isEauql){ msg=""; } isEauql=false; switch (v.getId()){ case R.id.divide_btn: msg+="/"; break; case R.id.multiply_btn: msg+="*"; break; case R.id.minus_btn: msg+="-"; break; case R.id.plus_btn: msg+="+"; break; case R.id.point_btn: msg+="."; break; case R.id.nine_btn: msg+="9"; break; case R.id.eight_btn: msg+="8"; break; case R.id.seven_btn: msg+="7"; break; case R.id.six_btn: msg+="6"; break; case R.id.five_btn: msg+="5"; break; case R.id.four_btn: msg+="4"; break; case R.id.three_btn: msg+="3"; break; case R.id.two_btn: msg+="2"; break; case R.id.one_btn: msg+="1"; break; case R.id.zero_btn: msg+="0"; break; case R.id.equal_btn: isEauql=true; try{ result=calculate(msg); msg=String.format("%.2f",result); }catch(Exception ex){ ex.printStackTrace(); msg="Error"; } System.out.println("result:"+result); digitList.clear(); operatorList.clear(); break; case R.id.delete_btn: if(!msg.equals("")){ msg=msg.substring(0,msg.length()-1); } } show_text.setText(msg); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show_text=(TextView)findViewById(R.id.show_text); Button divide_btn=findViewById(R.id.divide_btn); Button multiply_btn=findViewById(R.id.multiply_btn); Button minus_btn=findViewById(R.id.minus_btn); Button plus_btn=findViewById(R.id.plus_btn); Button nine_btn=findViewById(R.id.nine_btn); Button eight_btn=findViewById(R.id.eight_btn); Button seven_btn=findViewById(R.id.seven_btn); Button six_btn=findViewById(R.id.six_btn); Button five_btn=findViewById(R.id.five_btn); Button four_btn=findViewById(R.id.four_btn); Button three_btn=findViewById(R.id.three_btn); Button two_btn=findViewById(R.id.two_btn); Button one_btn=findViewById(R.id.one_btn); Button zero_btn=findViewById(R.id.zero_btn); Button equal_btn=findViewById(R.id.equal_btn); Button point_btn=findViewById(R.id.point_btn); Button delete_btn=findViewById(R.id.delete_btn); btnList.add(divide_btn); btnList.add(multiply_btn); btnList.add(minus_btn); btnList.add(plus_btn); btnList.add(point_btn); btnList.add(nine_btn); btnList.add(eight_btn); btnList.add(seven_btn); btnList.add(six_btn); btnList.add(five_btn); btnList.add(four_btn); btnList.add(three_btn); btnList.add(two_btn); btnList.add(one_btn); btnList.add(zero_btn); btnList.add(equal_btn); btnList.add(delete_btn); for(int i=0;i<btnList.size();i++){ Button btn=btnList.get(i); btn.setOnClickListener(this); } } //对等式进行计算 public static Float calculate(String msg){ List<Integer> integerList=new ArrayList<Integer>(); float number=0; for(int i=0;i<msg.length();i++){ switch (msg.charAt(i)){ case '*': number=change(integerList); digitList.add(number); operatorList.add(Entity.MULTIPLY); break; case '/': number=change(integerList); digitList.add(number); operatorList.add(Entity.DIVIDE); break; case '+': number=change(integerList); digitList.add(number); operatorList.add(Entity.PLUS); break; case '-': number=change(integerList); digitList.add(number); operatorList.add(Entity.MINUS); break; default: Integer num=msg.charAt(i)-'0'; integerList.add(num); break; } } number=change(integerList); digitList.add(number); float value=getValue(digitList,operatorList); return value; } //将字符串转化为数字 public static Float change(List<Integer> list){ String result=""; for(int i=0;i<list.size();i++){ if (list.get(i).equals(flag)){ result=result+"."; }else{ result=result+list.get(i); } } list.clear(); return Float.parseFloat(result); } //计算最终结果 public static float getValue(List<Float> digitList,List<Entity> operatorList){ float value=0; int index=0; while(operatorList.size()>0){ if(index>=operatorList.size()){ index=0; } //如果当前运算符的优先级大于下一个运算符的优先级或只有一个计算符,则进行计算 if(index==operatorList.size()-1||operatorList.get(index).getPriority()>=operatorList.get(index+1).getPriority()){ switch (operatorList.get(index).getOperator()){ case "*": value=digitList.get(index)*digitList.get(index+1); break; case "/": value=digitList.get(index)/digitList.get(index+1); break; case "+": value=digitList.get(index)+digitList.get(index+1); break; case "-": value=digitList.get(index)-digitList.get(index+1); break; } digitList.set(index,value); digitList.remove(index+1); operatorList.remove(index); index=0; }else{ index++; } } return digitList.get(0); } }
枚举类型:存储运算符
public enum Entity { DIVIDE("/",2),MULTIPLY("*",2),MINUS("-",1),PLUS("+",1); //操作符 private String operator; //运行优先级 private int priority; private Entity(String operator,int priority){ this.operator=operator; this.priority=priority; } public String getOperator() { return operator; } public int getPriority() { return priority; } @Override public String toString() { return operator; } }以上就是计算器的全部代码。当然代码确实不够简洁,算法也不够优化,不过也希望能够帮助对于急需代码的小伙伴。