本人的第一篇博客,写了个简单的计算器,开学的第一个作业,适合初学者打发下时间,高手可以飘过,如有错漏之处,还望指出,定当改之


顺手贴上github 仓库地址:https://github.com/liheming/calculator

 




MainActivity.java如下

package com.example.haily.calculator;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.text.DecimalFormat;/**author haily* date :2016.3.26* sample calculator 1.0* contact qq:1325789491*/public class MainActivity extends AppCompatActivity implements View.OnClickListener {private String result,oprating="";//真正的结果, //获取对应的操作符int op1length = 0;//第一个操作数的长度,为了定位第二个操作数的起止位置private boolean isOperate = false;//是否点击过操作符private double op1 = 0, op2 = 0, preRes = 0;//定义操作数1,操作数2,double类型结果private TextView text_show, text_result;//表达式和结果显示textView;private Button one, two, three, four, five, six, seven, eight, nine, zero, add, min, mul, div, delete, dot, equ;//所有按钮@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();//调用初始化函数init()}//初始化各组件private void init() {text_show = (TextView) findViewById(R.id.text_show);text_result = (TextView) findViewById(R.id.text_result);text_show.setText("");text_result.setText("");one = (Button) this.findViewById(R.id.one);two = (Button) this.findViewById(R.id.two);three = (Button) this.findViewById(R.id.three);four = (Button) this.findViewById(R.id.four);five = (Button) this.findViewById(R.id.five);six = (Button) this.findViewById(R.id.six);seven = (Button) this.findViewById(R.id.seven);eight = (Button) this.findViewById(R.id.eight);nine = (Button) this.findViewById(R.id.nine);zero = (Button) this.findViewById(R.id.zero);add = (Button) this.findViewById(R.id.add);min = (Button) this.findViewById(R.id.min);mul = (Button) this.findViewById(R.id.mul);div = (Button) this.findViewById(R.id.div);delete = (Button) this.findViewById(R.id.delete);dot = (Button) this.findViewById(R.id.dot);equ = (Button) this.findViewById(R.id.equ);//实现按钮监听事件one.setOnClickListener(this);two.setOnClickListener(this);three.setOnClickListener(this);four.setOnClickListener(this);five.setOnClickListener(this);six.setOnClickListener(this);seven.setOnClickListener(this);eight.setOnClickListener(this);nine.setOnClickListener(this);zero.setOnClickListener(this);dot.setOnClickListener(this);add.setOnClickListener(this);min.setOnClickListener(this);mul.setOnClickListener(this);div.setOnClickListener(this);equ.setOnClickListener(this);delete.setOnClickListener(this);delete.setOnLongClickListener(new View.OnLongClickListener() {//长按删除监听事件@Overridepublic boolean onLongClick(View v) {text_show.setText("");text_result.setText("");return true;}});}private void clickNuber(String s) {text_show.append(s);if (isOperate) {equ();}}@Overridepublic void onClick(View v) {dot.setEnabled(true);switch (v.getId()) {case R.id.one:clickNuber("1");break;case R.id.two:clickNuber("2");break;case R.id.three:clickNuber("3");break;case R.id.four:clickNuber("4");break;case R.id.five:clickNuber("5");break;case R.id.six:clickNuber("6");break;case R.id.seven:clickNuber("7");break;case R.id.eight:clickNuber("8");break;case R.id.nine:clickNuber("9");break;case R.id.zero:clickNuber("0");break;case R.id.dot:if (!text_show.getText().toString().equals("")) {for (int i = 0; i < text_show.getText().length(); i++) {if (text_show.getText().charAt(i) == '.') {System.out.println(text_show.getText().charAt(i) == '.');// dot.setEnabled(false);return;}}text_show.append(".");}break;case R.id.add://加法compute("+");break;case R.id.min://减法compute("-");break;case R.id.mul://乘法compute("×");break;case R.id.div://除法compute("÷");break;case R.id.equ:// 计算结果if (isOperate) {equ();isOperate = false;text_show.setText(result);text_result.setText("");}break;case R.id.delete://后退和删除if (!text_show.getText().toString().equals("")) {text_show.setText(text_show.getText().subSequence(0, text_show.getText().length() - 1));try {if (isOperate) {equ();}} catch (Exception e) {text_result.setText("");}isOperate = false;} else {isOperate = false;text_show.setText("");}break;default:}}private void compute(String oprate) {if (!text_show.getText().toString().equals("")&&!isOperate) {op1length = text_show.getText().length();try {op1 = Double.parseDouble(text_show.getText().toString());text_show.append(oprate);oprating = oprate;isOperate = true;} catch (Exception e) {Log.e("TAG", "第二次按操作符");}// System.out.println(op1); text_show.setText("");}}private void equ() {if (!text_show.getText().toString().equals("")) {System.out.println("error" + text_show.getText().toString().equals(""));int op2Length = text_show.getText().length();// System.out.println("op1Length"+op1length+"----op2Length"+op2Length);String str = text_show.getText().subSequence(op1length + 1, op2Length).toString();op2 = Double.parseDouble(str);System.out.println(op2);preRes = op2;switch (oprating) {case "+":preRes = add(op1, op2);break;case "-":preRes = min(op1, op2);break;case "×":preRes = mul(op1, op2);break;case "÷":preRes = div(op1, op2);break;}DecimalFormat df = new DecimalFormat("#0.0");result = df.format(preRes);text_result.setText(result);}}//以下4个方法为四则运算public double add(double op1, double op2) {return op1 + op2;}public double min(double op1, double op2) {return op1 - op2;}public double mul(double op1, double op2) {return op1 * op2;}public double div(double op1, double op2) {return op1 / op2;}//以下为菜单初始化@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.about) {AlertDialog builder = new AlertDialog.Builder(this).setTitle("关于").setMessage("简易计算器1.0\n小数点后保留1位小数\n长按删除所有数字\nmade by haily on 2016.3.26").setNegativeButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// finish();}}).show();return true;}return super.onOptionsItemSelected(item);}}



layout资源文件如下


<?xml version="1.0" encoding="utf-8"?>                                                                


更多相关文章

  1. 通过WifiManager可以实现对wifi进行操作,实现wifi自动连接等一些
  2. android studio git 上传代码,分支,tag,回退操作
  3. Android蓝牙开发(三):操作步骤
  4. Android实现步进式录像进度条
  5. Android后台开启服务默默拍照
  6. lobiner 关于android中的sqlite数据库操作
  7. android之AsyncQueryHandler详解
  8. Android(安卓)AAC模式,该与 MVP 分手了!
  9. 更换android的初始化图片

随机推荐

  1. Android(安卓)存储路径浅析
  2. [置顶] android利用jni调用第三方库——
  3. 解决Android(安卓)SDK Manager下载问题和
  4. android:layout_gravity 和 android:grav
  5. Android应用程序请求SurfaceFlinger服务
  6. android单位转换(TypedValue)
  7. Android(安卓)的消息队列模型
  8. android - 为安全而设计 - 1 - 开发文档
  9. Android(安卓)Add-ons
  10. 分享偶的android秘籍