这是中国大学慕课移动终端应用开发的网课作业3,我会持续更新我的作业,如果有需要关注一下吧

因为要求的是简单的加减乘除,并没有需要更高的要求,所以功能比较简单,页面主要照着课程要求来的。如果需要了解更优的方法实现计算器,可以参考利用二叉树前中后续遍历实现计算器,此计算器有bug可私信我

1. 完成界面如下

在这里插入图片描述

实现代码

1. MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button mButtonAdd,mButtonJian,mButtonCheng,mButtonChu,mButtonEqual;
    private TextView mTextViewQuestion,mTextViewResult;

    private String questionShow = "";
    private String questionGet = "";
    private double numberA;
    private double numberB;
    private int symbol = 0;
    private boolean flag = false;//记录有没有重复写入运算符

    private void init(){
        mButtonAdd = findViewById(R.id.buttonAdd);
        mButtonJian = findViewById(R.id.buttonJian);
        mButtonCheng = findViewById(R.id.buttonCheng);
        mButtonChu = findViewById(R.id.buttonChu);
        mButtonEqual = findViewById(R.id.buttonEqual);

        mTextViewQuestion = findViewById(R.id.questionText);
        mTextViewResult = findViewById(R.id.resultText);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (questionGet==""||flag){
                    Toast.makeText(MainActivity.this,"不符合计算规则",Toast.LENGTH_SHORT).show();
                }else {
                    numberA = Double.parseDouble(questionGet.toString());
                    questionGet = "";
                    questionShow +="+";
                    symbol = 1;
                    flag = true;
                    mTextViewQuestion.setText(questionShow);
                }
            }
        });
        mButtonJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (questionGet==""||flag){
                    Toast.makeText(MainActivity.this,"不符合计算规则",Toast.LENGTH_SHORT).show();
                }else {
                    numberA = Double.parseDouble(questionGet.toString());
                    questionGet = "";
                    questionShow +="-";
                    symbol = 2;
                    mTextViewQuestion.setText(questionShow);
                    flag = true;
                }
            }
        });
        mButtonCheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (questionGet==""||flag){
                    Toast.makeText(MainActivity.this,"不符合计算规则",Toast.LENGTH_SHORT).show();
                }else {
                    numberA = Double.parseDouble(questionGet.toString());
                    questionGet = "";
                    questionShow +="*";
                    symbol = 3;
                    flag = true;
                    mTextViewQuestion.setText(questionShow);
                }
            }
        });
        mButtonChu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (questionGet==""||flag){
                    Toast.makeText(MainActivity.this,"不符合计算规则",Toast.LENGTH_SHORT).show();
                }else {
                    numberA = Double.parseDouble(questionGet.toString());
                    questionGet = "";
                    questionShow +="/";
                    symbol = 4;
                    flag = true;
                    mTextViewQuestion.setText(questionShow);
                }
            }
        });
        mButtonEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (questionGet==""){
                    Toast.makeText(MainActivity.this,"无计算任务",Toast.LENGTH_SHORT).show();
                }else {
                    numberB = Integer.parseInt(questionGet.toString());
                    double result = 0;
                    if (symbol==1){
                        result = numberA + numberB;
                    }else if (symbol == 2){
                        result = numberA - numberB;
                    }else if (symbol == 3){
                        result = numberA * numberB;
                    }else if (symbol == 4){
                        if (numberB!=0){
                            result = numberA/numberB;
                        }else {
                            Toast.makeText(MainActivity.this,"除数不可为0",Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(MainActivity.this,"无计算任务",Toast.LENGTH_SHORT).show();
                    }
                    mTextViewResult.setText(result+"");
                    numberA = 0;
                    numberB = 0;
                    symbol = 0;
                    questionShow = "";
                    questionGet = "";
                    flag = false;
                }
            }
        });
    }

    //单机数字事件
    public void bindTapNumber(View v){
        switch(v.getId()){
            case R.id.button0:
                questionShow +="0";
                questionGet +="0";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button1:
                questionShow +="1";
                questionGet +="1";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button2:
                questionShow +="2";
                questionGet +="2";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button3:
                questionShow +="3";
                questionGet +="3";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button4:
                questionShow +="4";
                questionGet +="4";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button5:
                questionShow +="5";
                questionGet +="5";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button6:
                questionShow +="6";
                questionGet +="6";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button7:
                questionShow +="7";
                questionGet +="7";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button8:
                questionShow +="8";
                questionGet +="8";
                mTextViewQuestion.setText(questionShow);
                break;
            case R.id.button9:
                questionShow +="9";
                questionGet +="9";
                mTextViewQuestion.setText(questionShow);
                break;
        }

    }
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
    <TextView
        android:id="@+id/questionText"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="0"
        android:gravity="right"
        android:layout_gravity="bottom"
        style="@style/question"/>
    <TextView
        android:id="@+id/resultText"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:gravity="right"
        android:text="0"
        style="@style/result"/>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="15"
        android:paddingTop="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/button7"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="7"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/button8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="8"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/button9"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="9"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/buttonChu"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="/"
                style="@style/symbol"/>
        </LinearLayout>
        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="5"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/button6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="6"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/buttonCheng"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="*"
                style="@style/symbol"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="1"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="2"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/button3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="3"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/buttonJian"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="-"
                style="@style/symbol"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/button0"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="0"
                android:onClick="bindTapNumber"
                style="@style/number"/>
            <Button
                android:id="@+id/buttonEqual"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="="
                style="@style/symbol"/>
            <Button
                android:id="@+id/buttonAdd"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="+"
                style="@style/symbol"/>
        </LinearLayout>


    </LinearLayout>
    
</LinearLayout>
3. styles.xml
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="textSize">
        <item name="android:textSize">30dp</item>
    </style>

    <style name="number" parent="textSize">
        <item name="android:textColor">#000000</item>
        <item name="android:background">#F0D7D7</item>
    </style>

    <style name="symbol" parent="textSize">
        <item name="android:textColor">#FFFCFC</item>
        <item name="android:background">#FFC67C</item>
    </style>

    <style name="question">
        <item name="android:textSize">20dp</item>
        <item name="android:textColor">#A09E9E</item>
    </style>

    <style name="result">
        <item name="android:textSize">45dp</item>
        <item name="android:textColor">#000000</item>
    </style>

</resources>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐