首页

Android简易计算器源码

java

2020-6-27




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);
	}
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
CalculatorSample_V2.0.rar/CalculatorSample/.classpath , 475
CalculatorSample_V2.0.rar/CalculatorSample/.project , 852
CalculatorSample_V2.0.rar/CalculatorSample/.settings/org.eclipse.jdt.core.prefs , 177
CalculatorSample_V2.0.rar/CalculatorSample/AndroidManifest.xml , 898
CalculatorSample_V2.0.rar/CalculatorSample/bin/AndroidManifest.xml , 898
CalculatorSample_V2.0.rar/CalculatorSample/bin/CalculatorSample.apk , 258831
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/BuildConfig.class , 359
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/CalculatorActivity.class , 2499
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/listener/MyOnClickListener.class , 3106
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$attr.class , 361
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$dimen.class , 482
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$drawable.class , 428
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$id.class , 1053
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$layout.class , 430
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$menu.class , 415
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$string.class , 493
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R$style.class , 452
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes/org/fjcc/calculatorsample/R.class , 743
CalculatorSample_V2.0.rar/CalculatorSample/bin/classes.dex , 626636
CalculatorSample_V2.0.rar/CalculatorSample/bin/dexedLibs/android-support-v4-0506587c683c965156df6cef53264c1e.jar , 211326
CalculatorSample_V2.0.rar/CalculatorSample/bin/dexedLibs/android-support-v4-8281aaa1e7718ab3e9ca2c64634dadd9.jar , 211326
CalculatorSample_V2.0.rar/CalculatorSample/bin/jarlist.cache , 120
CalculatorSample_V2.0.rar/CalculatorSample/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
CalculatorSample_V2.0.rar/CalculatorSample/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
CalculatorSample_V2.0.rar/CalculatorSample/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
CalculatorSample_V2.0.rar/CalculatorSample/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
CalculatorSample_V2.0.rar/CalculatorSample/bin/resources.ap_ , 42189
CalculatorSample_V2.0.rar/CalculatorSample/gen/org/fjcc/calculatorsample/BuildConfig.java , 167
CalculatorSample_V2.0.rar/CalculatorSample/gen/org/fjcc/calculatorsample/R.java , 3591
CalculatorSample_V2.0.rar/CalculatorSample/ic_launcher-web.png , 51394
CalculatorSample_V2.0.rar/CalculatorSample/libs/android-support-v4.jar , 556198
CalculatorSample_V2.0.rar/CalculatorSample/proguard-project.txt , 781
CalculatorSample_V2.0.rar/CalculatorSample/project.properties , 563
CalculatorSample_V2.0.rar/CalculatorSample/res/drawable-hdpi/ic_launcher.png , 7658
CalculatorSample_V2.0.rar/CalculatorSample/res/drawable-mdpi/ic_launcher.png , 3777
CalculatorSample_V2.0.rar/CalculatorSample/res/drawable-xhdpi/ic_launcher.png , 12516
CalculatorSample_V2.0.rar/CalculatorSample/res/drawable-xxhdpi/ic_launcher.png , 24777
CalculatorSample_V2.0.rar/CalculatorSample/res/layout/activity_calculator.xml , 6342
CalculatorSample_V2.0.rar/CalculatorSample/res/menu/calculator.xml , 263
CalculatorSample_V2.0.rar/CalculatorSample/res/values/dimens.xml , 220
CalculatorSample_V2.0.rar/CalculatorSample/res/values/strings.xml , 233
CalculatorSample_V2.0.rar/CalculatorSample/res/values/styles.xml , 697
CalculatorSample_V2.0.rar/CalculatorSample/res/values-sw600dp/dimens.xml , 203
CalculatorSample_V2.0.rar/CalculatorSample/res/values-sw720dp-land/dimens.xml , 277
CalculatorSample_V2.0.rar/CalculatorSample/res/values-v11/styles.xml , 334
CalculatorSample_V2.0.rar/CalculatorSample/res/values-v14/styles.xml , 391
CalculatorSample_V2.0.rar/CalculatorSample/src/org/fjcc/calculatorsample/CalculatorActivity.java , 2811
CalculatorSample_V2.0.rar/CalculatorSample/src/org/fjcc/calculatorsample/listener/MyOnClickListener.java , 3129
没有账号? 忘记密码?

社交账号快速登录