[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())); }}最后来一发动态运行图:
阅读全文
0 0
- [Android]计算器源代码分析
- Android之一个简单计算器源代码
- Android 之一个简单计算器源代码
- 一款简单的Android计算器源代码
- 计算器源代码
- 计算器源代码
- 计算器源代码
- 计算器,源代码
- 源代码计算器
- 源代码计算器
- 源代码计算器
- 源代码计算器
- Android计算器实现源码分析
- Android源代码简要分析
- Android源代码简要分析
- android源代码结构分析
- Android源代码简要分析
- Android源代码简要分析
- 17.10.14日报
- 99乘法表
- Python学习笔记(五)
- maven的使用
- JAVA Eclipse(2)---关闭tomcat保存文件时自动重启
- [Android]计算器源代码分析
- 初探RESTful——使用Grizzly和Jersey搭建RESTful环境
- bzoj题目总结
- VC++文件操作之最全篇
- Material Design
- Tunnel Warfare(线段树+查找最大值最小值+模板)
- struts2中form表单使用post方式提交参数(键值对)超过10000的限制
- CyclicBarri和 CountDownLatch的使用场景
- 作用域链和原型链