Android制作的简单的计算器

1.新建一个Android项目,整体的框架图如下所示:


2.activity_main.xml里面的代码如下所示:

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

    <EditText
        android:id="@+id/et_edit"
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:focusable="false"
        android:gravity="right"
        android:inputType="text"
        android:singleLine="true"
        android:textSize="40sp" />

    <TableLayout
        android:id="@+id/tl_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10sp"
        android:collapseColumns="4" >

        <TableRow
            android:id="@+id/tr_one"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/bt_seven"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="7" />

            <Button
                android:id="@+id/bt_eight"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="8" />

            <Button
                android:id="@+id/bt_nine"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="9" />

            <Button
                android:id="@+id/bt_back"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="back" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_two"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/bt_four"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="4" />

            <Button
                android:id="@+id/bt_five"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="5" />

            <Button
                android:id="@+id/bt_six"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="6" />

            <Button
                android:id="@+id/bt_divide"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="/" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_three"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/bt_one"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="1" />

            <Button
                android:id="@+id/bt_two"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="2" />

            <Button
                android:id="@+id/bt_three"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="3" />

            <Button
                android:id="@+id/bt_multiply"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="*" />
        </TableRow>


        <TableRow
            android:id="@+id/tr_four"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/bt_zero"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="0" />

            <Button
                android:id="@+id/bt_point"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="." />

            <Button
                android:id="@+id/bt_add"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="+" />

            <Button
                android:id="@+id/bt_sub"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="-" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_five"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/bt_equal"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_weight="3"
                android:text="=" />

            <Button
                android:id="@+id/bt_clear"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:text="clear" />
        </TableRow>
    </TableLayout>

</LinearLayout>

3.MainActivity.java里面的代码如下所示:
package deu.cn.caculator;

import android.os.Bundle;
import android.app.Activity;
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 {

	private Button bt_zero;
	private Button bt_one;
	private Button bt_two;
	private Button bt_three;
	private Button bt_four;
	private Button bt_five;
	private Button bt_six;
	private Button bt_seven;
	private Button bt_eight;
	private Button bt_nine;
	private Button bt_back;
	private Button bt_multiply;
	private Button bt_divide;
	private Button bt_sub;
	private Button bt_add;
	private Button bt_point;
	private Button bt_equal;
	private Button bt_clear;
	private EditText et_edit;

	private StringBuffer str_display = new StringBuffer();
	private boolean flag = true;
	private String str_oper = "+";
	private String str_result = "0";
	private double num1;
	private double num2;
	private boolean b_sub;
	private boolean b_mul;
	private boolean b_div;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		bt_zero = (Button) findViewById(R.id.bt_zero);
		bt_one = (Button) findViewById(R.id.bt_one);
		bt_two = (Button) findViewById(R.id.bt_two);
		bt_three = (Button) findViewById(R.id.bt_three);
		bt_four = (Button) findViewById(R.id.bt_four);
		bt_five = (Button) findViewById(R.id.bt_five);
		bt_six = (Button) findViewById(R.id.bt_six);
		bt_seven = (Button) findViewById(R.id.bt_seven);
		bt_eight = (Button) findViewById(R.id.bt_eight);
		bt_nine = (Button) findViewById(R.id.bt_nine);
		bt_back = (Button) findViewById(R.id.bt_back);
		bt_multiply = (Button) findViewById(R.id.bt_multiply);
		bt_divide = (Button) findViewById(R.id.bt_divide);
		bt_sub = (Button) findViewById(R.id.bt_sub);
		bt_add = (Button) findViewById(R.id.bt_add);
		bt_point = (Button) findViewById(R.id.bt_point);
		bt_equal = (Button) findViewById(R.id.bt_equal);
		bt_clear = (Button) findViewById(R.id.bt_clear);
		et_edit = (EditText) findViewById(R.id.et_edit);
		et_edit.setText("0.0");

		bt_zero.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("0");
				et_edit.setText(str_display.toString());
			}
		});

		bt_one.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("1");
				et_edit.setText(str_display.toString());
			}
		});

		bt_two.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("2");
				et_edit.setText(str_display.toString());
			}
		});

		bt_three.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("3");
				et_edit.setText(str_display.toString());
			}
		});

		bt_four.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("4");
				et_edit.setText(str_display.toString());
			}
		});

		bt_five.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("5");
				et_edit.setText(str_display.toString());
			}
		});

		bt_six.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("6");
				et_edit.setText(str_display.toString());
			}
		});

		bt_seven.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("7");
				et_edit.setText(str_display.toString());
			}
		});

		bt_eight.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("8");
				et_edit.setText(str_display.toString());
			}
		});

		bt_nine.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_display.append("9");
				et_edit.setText(str_display.toString());
			}
		});

		bt_point.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (flag) {
					str_display.append(".");
					et_edit.setText(str_display.toString());
					flag = false;
				}
			}
		});

		bt_back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (str_display.length() != 0) {
					str_display.deleteCharAt(str_display.length() - 1);
					et_edit.setText(str_display.toString());
				}
			}
		});

		bt_add.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_oper = "+";
				if (str_display.toString() != "") {
					num1 += Double.parseDouble(str_display.toString());
					str_display = new StringBuffer("");
				}
				// 在按了“=”而没有输入数字时,str_result不为空;在按了“=”并且输入数字后,str_result结果为空。(后面同理)
				if (str_result != null) {
					num1 = Double.parseDouble(str_result);
					str_result = null;
				}
				et_edit.setText(String.valueOf(num1));
				flag = true;
			}
		});

		bt_sub.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_oper = "-";
				if (b_sub == false && str_display.toString() != "") {
					num1 = Double.parseDouble(str_display.toString());
					et_edit.setText(String.valueOf(num1));
					str_display = new StringBuffer("");
					b_sub = true;
				} else {
					if (str_display.toString() != "") {
						num1 -= Double.parseDouble(str_display.toString());
						str_display = new StringBuffer("");
					}
					if (str_result != null) {
						num1 = Double.parseDouble(str_result);
						str_result = null;
					}
					et_edit.setText(String.valueOf(num1));
				}
				flag = true;
			}
		});

		bt_divide.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_oper = "/";
				if (b_div == false && str_display.toString() != null) {
					num1 = Double.parseDouble(str_display.toString());
					et_edit.setText(String.valueOf(num1));
					str_display = new StringBuffer("");
					b_div = true;
				} else {
					if (str_display.toString() != "") {
						if (Double.parseDouble(str_display.toString()) == 0) {
							Toast.makeText(MainActivity.this, "除数不能为零!", 0)
									.show();
						} else {
							num1 /= Double.parseDouble(str_display.toString());
							str_display = new StringBuffer("");
						}
					}
					if (str_result != null) {
						num1 = Double.parseDouble(str_result);
						str_result = null;
					}
					et_edit.setText(String.valueOf(num1));
				}
				flag = true;
			}
		});

		bt_multiply.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_oper = "*";
				if (b_mul == false && str_display.toString() != "") {
					num1 = Double.parseDouble(str_display.toString());
					et_edit.setText(String.valueOf(num1));
					str_display = new StringBuffer("");
					b_mul = true;
				} else {
					if (str_display.toString() != "") {
						num1 *= Double.parseDouble(str_display.toString());
						str_display = new StringBuffer("");
					}
					if (str_result != null) {
						num1 = Double.parseDouble(str_result);
						str_result = null;
					}
					et_edit.setText(String.valueOf(num1));
				}
				flag = true;
			}
		});

		bt_equal.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (str_oper.equals("+")) {
					num2 = Double.parseDouble(str_display.toString());
					str_result = String.valueOf(num1 + num2);
					et_edit.setText(str_result);
					str_display = new StringBuffer("");
				}
				if (str_oper.equals("-")) {
					num2 = Double.parseDouble(str_display.toString());
					str_result = String.valueOf(num1 - num2);
					et_edit.setText(str_result);
					str_display = new StringBuffer("");
				}
				if (str_oper.equals("*")) {
					num2 = Double.parseDouble(str_display.toString());
					str_result = String.valueOf(num1 * num2);
					et_edit.setText(str_result);
					str_display = new StringBuffer("");
				}
				if (str_oper.equals("/")) {
					num2 = Double.parseDouble(str_display.toString());
					if (num2 != 0) {
						str_result = String.valueOf(num1 / num2);
						et_edit.setText(str_result);
					} else {
						Toast.makeText(MainActivity.this, "除数不能为零!", 0).show();
					}
					str_display = new StringBuffer("");
				}
			}
		});

		bt_clear.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				str_oper = "+";
				str_display = new StringBuffer("");
				str_result = null;
				num1 = 0;
				num2 = 0;
				flag = true;
				b_div = false;
				b_mul = false;
				b_sub = false;
				et_edit.setText("0.0");
			}
		});
	}

}

3.由于我安装的Android模拟器的版本较低,所以我把AndroidManifest.xml里面的minSdkVersion改了一下,我的AndroidManifest.xml里面的代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="deu.cn.caculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="deu.cn.caculator.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.运行之后可以进行简单的加、减、乘、除和返回、清除运算,唯一一点不好的就是在输入等于号但是不清零,之后再输入两个数做运算时它会把上次得到的结果与这一次的第二个数进行运算,以后再进一步改进。


5.代码下载地址:http://download.csdn.net/download/qq_29656961/9978021。

  • 13
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值