简易计算器的实现

1.简易计算器的页面效果图:

2.页面布局activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:gravity="center"
            android:hint="@string/initNum" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/num1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num1" />

        <Button
            android:id="@+id/num2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num2" />

        <Button
            android:id="@+id/num3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/num4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num4" />

        <Button
            android:id="@+id/num5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num5" />

        <Button
            android:id="@+id/num6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num6" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/num7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num7" />

        <Button
            android:id="@+id/num8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num8" />

        <Button
            android:id="@+id/num9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/num9" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/num0"
            android:layout_width="105dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/num0" />

        <Button
            android:id="@+id/clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:text="@string/clear" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="@string/add" />

        <Button
            android:id="@+id/sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:text="@string/sub" />

        <Button
            android:id="@+id/multi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:text="@string/multi" />

        <Button
            android:id="@+id/div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:text="@string/div" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/equal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:text="@string/equal" />
    </LinearLayout>

</LinearLayout>

3.功能的实现

(1)业务逻辑层类CalculatorBiz

package cn.edu.bzu.calculator.model;

/**
 * 模型层M:运算器业务逻辑类
 * @author 
 */
public class CalculatorBiz {
	public double add(double num1, double num2) {
		return num1 + num2;
	}

	public double sub(double num1, double num2) {
		return num1 - num2;
	}

	public double mulitply(double num1, double num2) {
		return num1 * num2;
	}

	public double divide(double num1, double num2) {
		return num1 / num2;
	}
}

(2)功能的实现MainActivity

package cn.edu.bzu.calculator;

import cn.edu.bzu.calculator.model.CalculatorBiz;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	// 数字按钮
	private Button num0, num1, num2, num3, num4, num5, num6, num7, num8, num9;
	// 运算符号按钮
	private Button add, sub, multi, div, equal, clear;
	// 操作运算
	private EditText opText;

	// 输入 数字及显示结果
	private Double number1;
	private Double number2;
	private String result;
	private String operate = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 初始化组件
		initialComponent();
		// 注册监听
		setListener();
	}

	// 菜单 创建menu
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);
		return true;
	}

	// 为运算符号按钮注册监听
	private class OperateOnClickListener implements OnClickListener {

		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.add:
				operate = "+";
				setFirstNum();
				break;
			case R.id.sub:
				operate = "-";
				setFirstNum();
				break;
			case R.id.multi:
				operate = "*";
				setFirstNum();
				break;
			case R.id.div:
				operate = "/";
				setFirstNum();
				break;
			case R.id.clear:
				opText.setText("");
				break;
			}
		}

	}

	// 为等号注册监听
	private class EqualOnClickListener implements OnClickListener {
		public void onClick(View v) {
			// 调用业务逻辑层中的CalculatorBiz
			CalculatorBiz calculatorBiz = new CalculatorBiz();
			number2 = Double.parseDouble(opText.getText().toString().trim());
			char op = operate.charAt(0);
			switch (op) {
			case '+':
				Double add = calculatorBiz.add(number1, number2);
				setResultString(number1, number2, add, operate);
				break;
			case '-':
				Double sub = calculatorBiz.sub(number1, number2);
				setResultString(number1, number2, sub, operate);
				break;
			case '*':
				Double multi = calculatorBiz.mulitply(number1, number2);
				setResultString(number1, number2, multi, operate);
				break;
			default:
				if (number2 != 0) {
					Double div = calculatorBiz.divide(number1, number2);
					setResultString(number1, number2, div, operate);
				} else {
					Toast.makeText(MainActivity.this, "0不能作为除数",
							Toast.LENGTH_LONG).show();
				}
				break;
			}
		}

	}

	// 获取第一个数字
	public void setFirstNum() {
		number1 = Double.parseDouble(opText.getText().toString());
		opText.setText("");
	}

	// 得到结果
	private void setResultString(Double firstNum, double secondNum,
			Double result, String operate) {
		String result0 = firstNum + operate + new Double(secondNum).toString()
				+ "=" + result.toString();
		opText.setText(result0);
	}

	// 为各数字键注册监听
	public void onClick(View v) {
		if (!(opText.getText().equals(""))) {
			result = opText.getText().toString();
			if (v.equals(num1)) {
				result = result + "1";
				opText.setText(result);
			}
			if (v.equals(num2)) {
				result = result + "2";
				opText.setText(result);
			}
			if (v.equals(num3)) {
				result = result + "3";
				opText.setText(result);
			}
			if (v.equals(num4)) {
				result = result + "4";
				opText.setText(result);
			}
			if (v.equals(num5)) {
				result = result + "5";
				opText.setText(result);
			}
			if (v.equals(num6)) {
				result = result + "6";
				opText.setText(result);
			}
			if (v.equals(num7)) {
				result = result + "7";
				opText.setText(result);
			}
			if (v.equals(num8)) {
				result = result + "8";
				opText.setText(result);
			}
			if (v.equals(num9)) {
				result = result + "9";
				opText.setText(result);
			}
			if (v.equals(num0)) {
				result = result + "0";
				opText.setText(result);
			}
		}
	}

	private void setListener() {
		num1.setOnClickListener(this);
		num2.setOnClickListener(this);
		num3.setOnClickListener(this);
		num4.setOnClickListener(this);
		num5.setOnClickListener(this);
		num6.setOnClickListener(this);
		num7.setOnClickListener(this);
		num8.setOnClickListener(this);
		num9.setOnClickListener(this);
		num0.setOnClickListener(this);
		// 为运算符号注册监听
		OperateOnClickListener onClickListner = new OperateOnClickListener();
		add.setOnClickListener(onClickListner);
		sub.setOnClickListener(onClickListner);
		multi.setOnClickListener(onClickListner);
		div.setOnClickListener(onClickListner);
		clear.setOnClickListener(onClickListner);
		// 计算结果
		equal.setOnClickListener(new EqualOnClickListener());
	}

	// 初始化组件
	private void initialComponent() {
		num0 = (Button) findViewById(R.id.num0);
		num1 = (Button) findViewById(R.id.num1);
		num2 = (Button) findViewById(R.id.num2);
		num3 = (Button) findViewById(R.id.num3);
		num4 = (Button) findViewById(R.id.num4);
		num5 = (Button) findViewById(R.id.num5);
		num6 = (Button) findViewById(R.id.num6);
		num7 = (Button) findViewById(R.id.num7);
		num8 = (Button) findViewById(R.id.num8);
		num9 = (Button) findViewById(R.id.num9);

		add = (Button) findViewById(R.id.add);
		sub = (Button) findViewById(R.id.sub);
		multi = (Button) findViewById(R.id.multi);
		div = (Button) findViewById(R.id.div);
		equal = (Button) findViewById(R.id.equal);
		clear = (Button) findViewById(R.id.clear);

		opText = (EditText) findViewById(R.id.editText1);
	}

	public void onClick(DialogInterface dialog, int which) {
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值