基本信息
源码名称:Android简易计算器源码
源码大小:1.47M
文件格式:.rar
开发语言:Java
更新时间:2018-12-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


package org.fjcc.calculatorsample.listener;

import org.fjcc.calculatorsample.CalculatorActivity;
import org.fjcc.calculatorsample.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

public class MyOnClickListener implements OnClickListener {
	// 运算表达式
	private String expression = "";
	// 运算符
	private String operator = "";
	private CalculatorActivity ca;

	public MyOnClickListener() {

	}

	public MyOnClickListener(Context c) {
		this.ca = (CalculatorActivity) c;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.threeBtn) {
			this.expression = this.expression   "3";
		} else if (arg0.getId() == R.id.oneBtn) {
			this.expression = this.expression   "1";
		}else if (arg0.getId() == R.id.twoBtn) {
			this.expression = this.expression   "2";
		}else if (arg0.getId() == R.id.fourBtn) {
			this.expression = this.expression   "4";
		}else if (arg0.getId() == R.id.sixBtn) {
			this.expression = this.expression   "6";
		}else if (arg0.getId() == R.id.sevenBtn) {
			this.expression = this.expression   "7";
		}else if (arg0.getId() == R.id.eightBtn) {
			this.expression = this.expression   "8";
		}else if (arg0.getId() == R.id.nineBtn) {
			this.expression = this.expression   "9";
		}
		else if (arg0.getId() == R.id.addBtn) {
			this.expression = this.expression   " ";
			this.operator = " ";
		}else if (arg0.getId() == R.id.devideBtn) {
			this.expression = this.expression   "/";
			this.operator = "/";
		}else if (arg0.getId() == R.id.minusBtn) {
			this.expression = this.expression   "-";
			this.operator = "-";
		}else if (arg0.getId() == R.id.zeroBtn) {
			this.expression = this.expression   "0";
		}else if (arg0.getId() == R.id.dotBtn) {
			this.expression = this.expression   ".";
		}
		else if (arg0.getId() == R.id.fiveBtn) {
			this.expression = this.expression   "5";
		} else if (arg0.getId() == R.id.multipleBtn) {
			this.expression = this.expression   "*";
			this.operator = "*";
			
			
			
		} else if (arg0.getId() == R.id.equalBtn) {

			// 找出运算符在字符串中的位置
			int opeIndex = this.expression.indexOf(this.operator);
			// System.out.println("opeIndex="   opeIndex);
			String op1 = this.expression.substring(0, opeIndex);
			String op2 = this.expression.substring(opeIndex   1);
			// System.out.println("op1="   op1   ",op2="   op2);
			double ope1 = Double.parseDouble(op1);
			double ope2 = Double.parseDouble(op2);
			double result = 0;
			if (this.operator.equals(" ")) {
				result = ope1   ope2;
			} else if (this.operator.equals("-")) {
				result = ope1 - ope2;
			} else if (this.operator.equals("*")) {
				result = ope1 * ope2;
			} else if (this.operator.equals("/")) {
				result = ope1 / ope2;
			}
			// System.out.println("result="   result);
			this.expression = this.expression   "="   result;
		} else if (arg0.getId() == R.id.deleteBtn) {
			this.expression = "";
			this.operator = "";
		}
		this.ca.expressionTV.setText(expression);
	}
}