Android学习(七) Android实现计算器

前台页面代码,通过线性布局方式实现计算器页面:如图所示

color.xml,自定义颜色values;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="gray">#CCCCCC</color>
    <color name="green">#00ff00</color>
    <color name="orange">#FF8040</color>   
</resources>

white_bg.xml,自定义的输入文本框样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 设置圆角的幅度 -->
    <corners android:radius="5dp" />
    <!-- 设置渐变颜色 起始为白色 color是手动添加的颜色枚举 -->
    <gradient android:startColor="@color/white" />
    <!-- 设置渐变颜色 结束为银灰色 -->
    <gradient android:endColor="@color/gray" />

    <!-- 边线 -->
    <stroke
        android:width="1dp"
        android:color="@color/black" />
</shape>

gray_by.xml,自定义按钮样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 圆角 -->
    <corners android:radius="5dp" />
    <!-- 填充颜色 -->
    <solid android:color="@color/gray" />
</shape>

orange_bg.xml,自定义点击时按钮的样式  

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 圆角 -->
    <corners android:radius="5dp" />
    <!-- 填充颜色 -->
    <solid android:color="@color/orange" />

</shape>

orange_select.xml:定义按钮默认和点击时的样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 按钮单击时的状态 -->
    <item android:drawable="@drawable/orange_bg" android:state_pressed="true"></item>
    <!-- 按钮默认状态 -->
    <item android:drawable="@drawable/gray_bg"></item>
</selector>

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" >

    <!-- layout_height:设置文本框高度 -->
    <!-- editable:设置文字不可以编辑 -->
    <!-- gravity:设置文字靠右下 -->
    <!-- background:为自定义的样式,新建drawable文件夹,在drawable下新建white_bg.xml文件 -->

    <EditText
        android:id="@+id/et_Text"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/white_bg"
        android:editable="false"
        android:ems="10"
        android:layout_marginTop="20dp"
        android:gravity="right|bottom" >

        <requestFocus android:layout_width="wrap_content" />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_clear"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="C"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_del"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="del"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_divide"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="÷"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_multiply"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="×"
            android:layout_margin="2dp"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_7"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="7"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_8"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="8"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_9"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="9"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_minus"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="-"
            android:layout_margin="2dp"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_4"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="4"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_5"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="5"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_6"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="6"
            android:layout_margin="2dp"
            android:textSize="20sp" />

        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_plus"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:text="+"
            android:layout_margin="2dp"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

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

                <Button
                    android:background="@drawable/orange_select"
                    android:id="@+id/btn_1"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="1"
                    android:text="1"
                    android:layout_margin="2dp"
                    android:textSize="20sp" />

                <Button
                    android:background="@drawable/orange_select"
                    android:id="@+id/btn_2"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="1"
                    android:text="2"
                    android:layout_margin="2dp"
                    android:textSize="20sp" />

                <Button
                    android:background="@drawable/orange_select"
                    android:id="@+id/btn_3"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="1"
                    android:text="3"
                    android:layout_margin="2dp"
                    android:textSize="20sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal" >

                <Button
                    android:background="@drawable/orange_select"
                    android:id="@+id/btn_0"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="4"
                    android:text="0"
                    android:layout_margin="2dp"
                    android:textSize="20sp" />

                <Button
                    android:background="@drawable/orange_select"
                    android:id="@+id/btn_point"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="2"
                    android:text="."
                    android:layout_margin="2dp"
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>
        <Button
            android:background="@drawable/orange_select"
            android:id="@+id/btn_equals"
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:layout_weight="3"
            android:text="="
            android:layout_margin="2dp"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

main_activity.java 业务代码

package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

    Button btn_1; // 数字1
    Button btn_2; // 数字2
    Button btn_3; // 数字3
    Button btn_4; // 数字4
    Button btn_5; // 数字5
    Button btn_6; // 数字6
    Button btn_7; // 数字7
    Button btn_8; // 数字8
    Button btn_9; // 数字9
    Button btn_0; // 数字0
    Button btn_clear; // 清0
    Button btn_del; // 删除健
    Button btn_divide; // 除号
    Button btn_multiply; // *号
    Button btn_minus; // -号
    Button btn_plus; // +号
    Button btn_point; // 小数点
    Button btn_equals; // =
    EditText et_Text; // 显示文本

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

        // 获取页面上的控件
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_del = (Button) findViewById(R.id.btn_del);
        btn_plus = (Button) findViewById(R.id.btn_plus);
        btn_divide = (Button) findViewById(R.id.btn_divide);
        btn_multiply = (Button) findViewById(R.id.btn_multiply);
        btn_minus = (Button) findViewById(R.id.btn_minus);
        btn_point = (Button) findViewById(R.id.btn_point);
        btn_equals = (Button) findViewById(R.id.btn_equals);
        et_Text = (EditText) findViewById(R.id.et_Text);

        // 按钮的单击事件
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_0.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_equals.setOnClickListener(this);

    }

    //定义第一个操作数和第二个操作数
    double d1 = 0, d2 = 0;
    //定义运算符
    String oprator = "";

    @Override
    public void onClick(View v) {
        String str = et_Text.getText().toString();

        switch (v.getId()) {
        case R.id.btn_1:
        case R.id.btn_2:
        case R.id.btn_3:
        case R.id.btn_4:
        case R.id.btn_5:
        case R.id.btn_6:
        case R.id.btn_7:
        case R.id.btn_8:
        case R.id.btn_9:
        case R.id.btn_0:
        case R.id.btn_point:
            // 点击数字按钮和小数点时,在文本内追加内容
            et_Text.setText(str + ((Button) v).getText().toString());
            break;
        case R.id.btn_plus:
        case R.id.btn_minus:
        case R.id.btn_multiply:
        case R.id.btn_divide:
            // 点击运算符按钮时,获取前面输入的第一个运算符
            d1 = Double.parseDouble(et_Text.getText().toString());
            // 添加到文本区域内
            et_Text.setText(str + " " + ((Button) v).getText().toString() + " ");
            // 获取点击的运算符
            oprator = ((Button) v).getText().toString();
            break;
        case R.id.btn_clear:
            // 清空文本内容
            et_Text.setText("");
            break;
        case R.id.btn_del:
            // 点击删除按钮,删除一个字符
            if (str != null && !str.equals("")) {
                str = str.substring(0, str.length() - 1);
                et_Text.setText(str);
            }
            break;
        case R.id.btn_equals:
            // 计算结果方法,获取第二个输入的数字
            int start = str.lastIndexOf(oprator);
            d2 = Double.parseDouble(str.substring(start + 1, str.length()));
            getResult(d1, d2, oprator);
            break;
        }
    }

    // 计算结果
    private void getResult(double d1, double d2, String oprator) {
        // 计算结果
        String str = et_Text.getText().toString();
        double result = 0;
        if (oprator.equals("+")) {
            result = d1 + d2;
        } else if (oprator.equals("-")) {
            result = d1 - d2;
        } else if (oprator.equals("×")) {
            result = d1 * d2;
        } else if (oprator.equals("÷")) {
            if (d2 == 0) {
                result = 0;
            } else {
                result = d1 / d2;
            }
        }

        // 如果不包含小数点则为小数和除法运算
        if (!str.contains(".") && oprator != "÷") {
            et_Text.setText(((int) result) + "");
        } else {
            et_Text.setText(result + "");
        }

    }

}
原文地址:https://www.cnblogs.com/zhengcheng/p/4361515.html