赞
踩
最近想着用AS做一个简单好看的计算器拿来玩玩
界面如下:
Android项目目录结构如下图所示:
布局文件activity_main.xml示例图:
布局文件activity_main.xml完整代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#cacaca" android:orientation="vertical" tools:context=".MainActivity"> <!--标题及结果框--> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.23" android:orientation="vertical" android:background="@drawable/frame" android:weightSum="1"> <TextView android:id="@+id/id_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.50" android:textSize="31sp" android:textColor="#2E2E2E" android:gravity="right" /> <TextView android:id="@+id/id_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.50" android:textSize="31sp" android:textColor="#2E2E2E" android:gravity="right" /> </LinearLayout> <!--按键--> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow android:layout_weight="1"> <Button android:id="@+id/btn_clear" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="AC" android:textColor="#C106FF" android:textSize="30sp" /> <Button android:id="@+id/btn_delete" android:layout_width="0dp" android:onClick="xml_OnClick" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="Del" android:textAllCaps="false" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_percent" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="%" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_divide" android:layout_width="0dp" android:onClick="xml_OnClick" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="÷" android:textColor="#6f7480" android:textSize="30sp" /> </TableRow> <TableRow android:layout_weight="1"> <Button android:id="@+id/btn_7" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="7" android:onClick="xml_OnClick" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_8" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="8" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_9" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:onClick="xml_OnClick" android:background="@drawable/selector_1" android:text="9" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_mul" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="×" android:textColor="#6f7480" android:textSize="30sp" /> </TableRow> <TableRow android:layout_weight="1"> <Button android:id="@+id/btn_4" android:layout_width="0dp" android:onClick="xml_OnClick" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="4" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_5" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="5" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_6" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="6" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_sub" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="-" android:textColor="#6f7480" android:textSize="30sp" /> </TableRow> <TableRow android:layout_weight="1"> <Button android:id="@+id/btn_1" android:layout_width="0dp" android:onClick="xml_OnClick" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="1" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_2" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="2" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="3" android:textColor="#6f7480" android:onClick="xml_OnClick" android:textSize="30sp" /> <Button android:id="@+id/btn_add" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="+" android:textColor="#6f7480" android:textSize="30sp" /> </TableRow> <TableRow android:layout_weight="1"> <Button android:id="@+id/re" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="退出" android:textAllCaps="false" android:textColor="#C106FF" android:textSize="25sp" /> <Button android:id="@+id/btn_0" android:layout_width="0dp" android:onClick="xml_OnClick" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_1" android:text="0" android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_dot" android:onClick="xml_OnClick" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_weight="1" android:background="@drawable/selector_1" android:text="." android:textColor="#6f7480" android:textSize="30sp" /> <Button android:id="@+id/btn_equals" android:layout_width="0dp" android:onClick="xml_OnClick" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:background="@drawable/selector_2" android:text="=" android:textColor="#6f7480" android:textSize="30sp" /> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>
MainActivity代码如下:
package com.example.xiaomi_cal; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.text.DecimalFormat; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class MainActivity extends AppCompatActivity { private TextView input; private TextView result; private StringBuilder str = new StringBuilder(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); input = (TextView) findViewById(R.id.id_input); result = (TextView) findViewById(R.id.id_result); Button bt_re=(Button)findViewById(R.id.re); bt_re.setOnClickListener(new MyonClick(){ @Override public void onClick(View v){ AlertDialog.Builder mDialog = new AlertDialog.Builder(MainActivity.this); mDialog.setTitle("退出"); mDialog.setMessage("确定要退出吗?"); mDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { System.exit(0); } }); mDialog.setNegativeButton("取消", null); mDialog.show(); } }); } class MyonClick implements View.OnClickListener{ @Override public void onClick(View view) { } } private void symbolSolve(String s) { int len = str.length(); switch (s) { case "-": if (len == 0) { str.append("-"); return; } if (str.charAt(len - 1) == '*' || str.charAt(len - 1) == '/') { str.append(s); } else if (isOperator(str.charAt(len - 1) + "")) { str.replace(len - 1, len, s); } else { str.append(s); } break; case "+": case "*": case "/": if (len == 0) return; if (isOperator(str.charAt(len - 1) + "")) { str.replace(len - 1, len, s); } else { str.append(s); } break; } } public void xml_OnClick(View view) { try { switch (view.getId()) { case R.id.btn_clear: tvClear(input, result); str.setLength(0); break; case R.id.btn_delete: int len = str.length(); if (len == 0) return; input.setText(str.deleteCharAt(len - 1)); break; case R.id.btn_add: symbolSolve("+"); input.setText(str); return; case R.id.btn_sub: symbolSolve("-"); input.setText(str); return; case R.id.btn_mul: symbolSolve("*"); input.setText(str); return; case R.id.btn_divide: symbolSolve("/"); input.setText(str); return; case R.id.btn_equals: if (str.length() == 0) return; if (str.length() == 1 && str.charAt(0) == '-') return; DecimalFormat df = new DecimalFormat("###.###############"); String num = null; double d = calculate(str.toString()); if (Double.isNaN(d) || Double.isInfinite(d)) { result.setText("不能除以0"); } else { try { num = df.format(d); } catch (Exception e) { System.out.println("错误!"); } result.setText("-0".equals(num) ? "0" : num); } result.setTextColor(Color.parseColor("#C106FF")); result.setTextSize(39); return; case R.id.btn_dot: str.append("."); break; case R.id.btn_0: str.append("0"); break; case R.id.btn_1: str.append("1"); break; case R.id.btn_2: str.append("2"); break; case R.id.btn_3: str.append("3"); break; case R.id.btn_4: str.append("4"); break; case R.id.btn_5: str.append("5"); break; case R.id.btn_6: str.append("6"); break; case R.id.btn_7: str.append("7"); break; case R.id.btn_8: str.append("8"); break; case R.id.btn_9: str.append("9"); break; } input.setText(str); int len = str.length(); if (len != 0) { DecimalFormat df = new DecimalFormat("###.###############"); String num = null; double d = calculate(str.toString()); if (Double.isNaN(d) || Double.isInfinite(d)) { result.setText("不能除以0"); } else { try { num = df.format(d); } catch (Exception e) { System.out.println("错误!"); } result.setText("-0".equals(num) ? "0" : num); } } } catch (NumberFormatException e) { result.setText("错误"); e.printStackTrace(); } } private boolean isOperator(String s) { return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/"); } private void tvClear(TextView input, TextView result) { input.setText(""); result.setText(""); result.setTextColor(Color.parseColor("#FFC0CB")); result.setTextSize(26); } private double calculate(String s) { Queue<String> q = getPostfixExpression(s); // 中缀表达式转为后缀表达式 return calculatePostfixExpression(q); } Stack<Double> stack = new Stack<>(); private double calculatePostfixExpression(Queue<String> queue) { stack.clear(); int len = queue.size(); double num1 = 0.0, num2 = 0.0, num3 = 0.0; for (int i = 0; i < len; ++i) { String s = queue.poll(); if (!isOperator(s)) { stack.push(Double.valueOf(s)); } else { if (stack.isEmpty()) return 0.0; num2 = stack.pop(); if (stack.isEmpty()) return num2; num1 = stack.pop(); switch (s) { case "+": num3 = num1 + num2; break; case "-": num3 = num1 - num2; break; case "*": num3 = num1 * num2; break; case "/": num3 = num1 / num2; break; } stack.push(num3); } } return stack.peek(); } Stack<Character> stack2 = new Stack<>(); Queue<String> queue2 = new LinkedList<>(); StringBuilder strNum = new StringBuilder(); // 获得后缀表达式 public Queue<String> getPostfixExpression(String s) { stack2.clear(); int len = s.length(); strNum.setLength(0); queue2.clear(); char temp = ' '; for (int i = 0; i < len; ++i) { temp = str.charAt(i); if (temp >= '0' && temp <= '9' || temp == '.') { strNum.append(temp); } else { if (i == 0 || isOperator(str.charAt(i - 1) + "")) { // 考虑负数的情况,比如乘以除以负数 strNum.append(temp); continue; } queue2.add(strNum.toString()); // 数字进队列 strNum.setLength(0); if (stack2.isEmpty()) { stack2.push(temp); } else { while (!stack2.isEmpty()) { char top = stack2.peek(); if (getPriority(top) >= getPriority(temp)) { queue2.add(top + ""); stack2.pop(); } else { break; } } stack2.push(temp); } } } if (strNum.length() != 0) { queue2.add(strNum.toString()); // 数字进队列 } if (stack2.isEmpty()) { stack2.push(temp); } else { while (!stack2.isEmpty()) { char top = stack2.peek(); queue2.add(top + ""); stack2.pop(); } } return queue2; } private int getPriority(char top) { if (top == '+' || top == '-') return 1; return 2; // 只有加减乘除 } }
想要完整源码的可以下载,我把整个项目上传了,欢迎大家下载:https://download.csdn.net/download/qq_44394562/19087725
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。