Android studio简单计算器的实现

一、内容

基于课本的界面设计做出一个简单的计算器,下图为该计算器截图。

二、源代码

1、界面设计

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4">

    <TextView
        android:id="@+id/et_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_marginLeft="4px"
        android:layout_gravity="left"
        android:textSize="50dip"/><!--数字显示-->


    <Button
        android:id="@+id/bt_del"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_1"
        android:text="1"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_2"
        android:text="2"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_3"
        android:text="3"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_plus"
        android:text="+"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_4"
        android:text="4"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_5"
        android:text="5"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_6"
        android:text="6"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_min"
        android:text="-"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_7"
        android:text="7"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_8"
        android:text="8"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_9"
        android:text="9"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_mul"
        android:text="*"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_point"
        android:text="."
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_0"
        android:text="0"
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_eq"
        android:text="="
        android:textSize="26sp"/>
    <Button
        android:id="@+id/bt_div"
        android:text="/"
        android:textSize="26sp"/>

</GridLayout>

2、程序

package com.example.ex2_2;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button bt_0, bt_1, bt_2, bt_3, bt_4, bt_5, bt_6, bt_7, bt_8, bt_9;
    Button bt_min, bt_mul, bt_plus, bt_div;
    Button bt_del, bt_eq, bt_point;
    TextView et_1;
    boolean clear1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_0 = (Button) findViewById(R.id.bt_0);
        bt_1 = (Button) findViewById(R.id.bt_1);
        bt_2 = (Button) findViewById(R.id.bt_2);
        bt_3 = (Button) findViewById(R.id.bt_3);
        bt_4 = (Button) findViewById(R.id.bt_4);
        bt_5 = (Button) findViewById(R.id.bt_5);
        bt_6 = (Button) findViewById(R.id.bt_6);
        bt_7 = (Button) findViewById(R.id.bt_7);
        bt_8 = (Button) findViewById(R.id.bt_8);
        bt_9 = (Button) findViewById(R.id.bt_9);
        bt_min = (Button) findViewById(R.id.bt_min);
        bt_mul = (Button) findViewById(R.id.bt_mul);
        bt_plus = (Button) findViewById(R.id.bt_plus);
        bt_div = (Button) findViewById(R.id.bt_div);
        bt_del = (Button) findViewById(R.id.bt_del);
        bt_eq = (Button) findViewById(R.id.bt_eq);
        bt_point = (Button) findViewById(R.id.bt_point);
        et_1 = (TextView) findViewById(R.id.et_1);
        bt_0.setOnClickListener(this);
        bt_1.setOnClickListener(this);
        bt_2.setOnClickListener(this);
        bt_3.setOnClickListener(this);
        bt_4.setOnClickListener(this);
        bt_5.setOnClickListener(this);
        bt_6.setOnClickListener(this);
        bt_7.setOnClickListener(this);
        bt_8.setOnClickListener(this);
        bt_9.setOnClickListener(this);
        bt_min.setOnClickListener(this);
        bt_mul.setOnClickListener(this);
        bt_plus.setOnClickListener(this);
        bt_div.setOnClickListener(this);
        bt_del.setOnClickListener(this);
        bt_eq.setOnClickListener(new click());
        bt_point.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String str = et_1.getText().toString();
        switch (v.getId()) {
            case R.id.bt_0:
            case R.id.bt_1:
            case R.id.bt_2:
            case R.id.bt_3:
            case R.id.bt_4:
            case R.id.bt_5:
            case R.id.bt_6:
            case R.id.bt_7:
            case R.id.bt_8:
            case R.id.bt_9:
            case R.id.bt_point:
                if (clear1) {
                    clear1 = false;//文本编辑框无数字
                    str = "";
                    et_1.setText("");
                }
                et_1.setText(str + ((Button) v).getText());
                break;
            case R.id.bt_plus:
            case R.id.bt_mul:
            case R.id.bt_min:
            case R.id.bt_div:
                if (clear1) {
                    clear1 = false;//编辑框无数字
                    str = "";
                    et_1.setText("");
                }
                if (str.contains("+") || str.contains("-") || str.contains("*") || str.contains("/")) //判断是否含有一个或者多个运算符
                {
                    str = str.substring(0, str.indexOf(""));//复制当前string对象到新的字符串
                }
                et_1.setText(str + ((Button) v).getText());
                break;
            case R.id.bt_del:
                if (clear1)
                    clear1 = false;
                str = "";
                et_1.setText("");
                break;
        }
    }

    class click implements OnClickListener {
        public void onClick(View v) {
            Result();
        }
    }

    private void Result() {
        double t = 0;
        String exp = et_1.getText().toString();
        if (exp == null || exp.equals(""))
            return;
        if (!exp.contains(" ")) {
            return;
        }
        if (clear1) {
            clear1 = false;
            return;
        }
        clear1 = true;
        String s1 = exp.substring(0, exp.indexOf(" "));//运算符前的数字
        String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);//运算符
        String s2 = exp.substring(exp.indexOf(" ") + 3);//运算符后的数字
        if (!s1.equals("") && !s2.equals("")) {
            double d1 = Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")) {
                t = d1 + d2;
            } else if (op.equals("-")) {
                t = d1 - d2;
            } else if (op.equals("/")) {
                if (d2 == 0) {
                    t = 0;
                } else t = d1 / d2;
            } else if (op.equals("*")) {
                t = d1 * d2;
            }
        }
        et_1.setText(exp+"="+t);
    }
}



三、总结

该计算器程序的难点主要在于输入的数字和运算符需要分别提取出来之后计算,我的想法是用indexOf方法和subString方法将运算符和数字分隔开,为每个按钮设计监听器,当输入两个数字的加减乘除就可以完成了。

猜你喜欢

转载自blog.csdn.net/Sing___546/article/details/88629368