[Android]计算器源代码分析

来源:互联网 发布:sql视图和表的区别 编辑:程序博客网 时间:2024/06/06 14:21

布局模仿的是Mac自带的计算器,先来一发截图吧!


package cn.task.simplecalc;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import org.w3c.dom.Text;import java.util.Stack;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private int buttons[] = null;    private Stack<String> result = new Stack<>();    private Stack<String> stack = new Stack<>();    private String string = "";    private String textString = "";    private TextView text = null;    private TextView information = null;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //        buttons = new int[]{                R.id.clear, R.id.negative,                R.id.extra, R.id.divide, R.id.multiply, R.id.minus, R.id.add, R.id.equals,                R.id.one, R.id.two, R.id.three, R.id.four, R.id.five,                R.id.six, R.id.seven, R.id.eight, R.id.nine,                R.id.zero, R.id.point        };        //        text = (TextView) findViewById(R.id.result);        information = (TextView) findViewById(R.id.information);        for (int i = 0; i < buttons.length; i++) {            findViewById(buttons[i]).setOnClickListener(this);        }    }    @Override public void onClick(View v)    {        Button button = (Button) v;        switch (v.getId()) {            case R.id.clear:                stack.clear();                result.clear();                string = "";                break;        case R.id.add:        case R.id.minus:        case R.id.multiply:        case R.id.divide:        case R.id.extra:        case R.id.equals:            textString = text.getText().toString();            if (result == null || result.isEmpty()) {                result.push(textString);                text.setText(textString);            } else if (!textString.equals(result.peek())) {                String c = stack.pop();                Double one = Double.parseDouble(result.pop());                Double next = Double.parseDouble(textString);                result.push(String.valueOf(c.equals("+") ? one + next                                                   : c.equals("-") ? one - next                        : c.equals("*") ? one * next                        : c.equals("/") ? one / next                        : one % next));                text.setText(result.peek());            }            if (!((Button) v).getText().equals("=")) {                stack.push(button.getText().toString());            }            string = "";            text.setText((result == null || result.isEmpty()) ? "0" : result.peek());            break;            case R.id.negative:                boolean isBelowZero = Double.parseDouble(text.getText().toString()) < 0;                text.setText(isBelowZero ? text.getText().toString().substring(1) :                                     "-" + text.getText().toString());                break;            default:                string += button.getText().toString();                text.setText(string);        }        information.setText(                "符号栈:Size:" + stack.size() + " " + "Top:" +                        ((stack == null || stack.isEmpty()) ? "Null" : stack.peek()) + "    " +                        "数值栈:Size:" + result.size() + " " + "Top:" +                        ((result == null || result.isEmpty()) ? "Null" : result.peek()));    }}
最后来一发动态运行图: