Android大作业(简易计算机)

jisuanqi

按钮背景样式 在drawable文件夹下新建changebtncolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  按钮点击时的状态  -->
    <item android:state_pressed="true">
        <shape>
            <size android:height="100dp" android:width="100dp"/>
            <corners android:radius="30dp"/>
            <solid android:color="#ADAEB3"/>
        </shape>
    </item>
<!--  按钮未点击时的状态  -->
    <item android:state_pressed="false">
        <shape>
            <corners android:radius="30dp"/>
            <solid android:color="#fafafa"/>
        </shape>
    </item>
</selector>

部分文字样式 在color文件下添加新的颜色值

<color name="mycolor">#ff7532</color>

计算机布局 activity_claculator.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE"
    android:orientation="vertical"
    android:padding="5dp">

    <!--  1. ScrollView:设置垂直方向的滚动布局  -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--    ScrollView嵌套一个线性布局    -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <!--      计算结果显示控件 lines:设置控件行高     -->
            <TextView
                android:id="@+id/tv_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:gravity="bottom|right"
                android:lines="6"
                android:text="0"
                android:textColor="@color/black"
                android:textSize="25sp" />

            <!--      嵌套一个网格布局 5行4列     -->
            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="4"
                android:rowCount="5"

                >

                <!--      android:layout_columnWeight="1" => 每一列占4分之1的宽度          -->
                <Button
                    android:id="@+id/btn_cancel"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="@string/cancel"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_Div"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="÷"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_Mul"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="*"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_clear"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="C"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_seven"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="7"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_eight"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="8"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_nine"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="9"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_Sul"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_four"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="4"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_five"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="5"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_six"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="6"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_Add"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="+"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_one"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_two"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="2"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_three"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/button_height"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="3"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_sque"
                    android:layout_width="0dp"
                    android:layout_height="72dp"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="✓"
                    android:textColor="@color/mycolor"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_swap"
                    android:layout_width="0dp"
                    android:layout_height="72dp"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="「」"
                    android:textColor="#ffaa00"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_zeor"
                    android:layout_width="0dp"
                    android:layout_height="72dp"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="0"
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />
                <Button
                    android:id="@+id/btn_float"
                    android:layout_width="0dp"
                    android:layout_height="72dp"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/changebtncolor"
                    android:gravity="center"
                    android:text="."
                    android:textColor="@color/black"
                    android:textSize="@dimen/button_font_size" />

                <Button
                    android:id="@+id/btn_equery"
                    android:layout_width="0dp"
                    android:layout_height="72dp"
                    android:layout_columnWeight="1"
                    android:layout_margin="10dp"
                    android:background="@drawable/btn2color"
                    android:gravity="center"
                    android:text="="
                    android:textColor="@color/white"
                    android:textSize="@dimen/button_font_size" />


            </GridLayout>


        </LinearLayout>

    </ScrollView>


</LinearLayout>

实现代码 ClaculatorActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ClaculatorActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_result;

    // 存储第一个操作数
    private String firstNum = "";
    // 存储运算符
    private String operator = "";
    // 存储第二个操作数
    private String secondNum = "";
    // 存储运算得到的结果
    private String result = "";
    // 存储输入的所有操作数据
    private String showText = "";

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

        // 获取结果视图
        tv_result = findViewById(R.id.tv_result);

        findViewById(R.id.btn_cancel).setOnClickListener(this); // 回退(取消)按钮


        // 对所有的1~9进行按钮监听
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);

        // 对加减乘除按钮进行监听
        findViewById(R.id.btn_Add).setOnClickListener(this);
        findViewById(R.id.btn_Sul).setOnClickListener(this);
        findViewById(R.id.btn_Mul).setOnClickListener(this);
        findViewById(R.id.btn_Div).setOnClickListener(this);

        findViewById(R.id.btn_zeor).setOnClickListener(this); // 0按钮
        findViewById(R.id.btn_float).setOnClickListener(this); // 小数点按钮
        findViewById(R.id.btn_equery).setOnClickListener(this); // 等号按钮
        findViewById(R.id.btn_sque).setOnClickListener(this); // 开平方按钮
        findViewById(R.id.btn_clear).setOnClickListener(this); // 清空按钮


    }

    @Override
    public void onClick(View v) {
        // 存储点击按钮得到的数据
        String inputTest;
        // v代表的是按钮,将Button类型强制转为TextView获取点击到的数据,再转为String类型赋值给变量inputText
        inputTest = ((TextView) v).getText().toString();

        while (true) {
            // 判断所点击的按钮
            if (v.getId() == R.id.btn_clear) {
                // 1. 点击清空按钮就将结果控件置为字符串0
                  // tv_result.setText("0");

                // 2. 调用清空方法,清空控件当前的数据
                 clear();
                break;
            } else if (v.getId() == R.id.btn_cancel) {
                break;
            } else if (v.getId() == R.id.btn_Add || v.getId() == R.id.btn_Sul || v.getId() == R.id.btn_Mul || v.getId() == R.id.btn_Div) {
                // 对点击的运算符进行存储
                operator=inputTest;
                // 将获取到的数据与点击的运算符进行拼接显示
                refreshText(showText+operator);
                break;
            // 等号按钮
            } else if (v.getId() == R.id.btn_equery) {
                double calculate_result= getCalculate_result();
                // 将返回的结果进行手动的封装成字符串,在传递给refreshOperate()方法将其进行存储
                refreshOperate(String.valueOf(calculate_result));
                // 显示结果
                refreshText(showText+"="+result);
                break;
                // 开根号按钮
            } else if (v.getId() == R.id.btn_sque) {
                // 使用Math类中的sqrt()方法求平方根
                double sqrt_result=Math.sqrt(Double.parseDouble(firstNum));
                // 将返回的结果进行手动的封装成字符串,在传递给refreshOperate()方法将其进行存储
                refreshOperate(String.valueOf(sqrt_result));
                // 显示结果
                refreshText(showText+"√="+result);

                break;
            } else {
                // 对已经计算得到的结果进行判断,如果结果已经得到,
                // 未选择操作符,再次输入数字就将原计算得到的结果进行清空,在将点击按钮的值显示在结果控件
                if(result.length()>0 && operator.equals("")){
                    clear();
                }


                // 点击按钮数值但没选择运算符(operator)时执行 => 没点击运算符
                if (operator.equals("")) {
                    // 第一个操作数加上点击的按钮数值
                    firstNum += inputTest;

                } else {
                    // 点击了运算符 将第二个数值与点击的按钮值进行拼接
                    secondNum += inputTest;
                }

                // 判断当前点击的按钮是否等于0并且是否不等于小数点
                if (showText.equals(("0")) && !inputTest.equals(".")) {
                    // 满足条件就只显示当前点击的按钮数值(即0)
                    refreshText(inputTest);
                } else {
                    // 否则就将获取到的数值进行拼接
                    refreshText(showText + inputTest);
                }
                break;
            }
        }

    }



    // 1.定义一个显示点击按钮进行操作的方法,需要传递一个字符串(刷新显示文本)
    public void refreshText(String text) {
        // 将获取到的字符串赋值给全局的showText变量
        showText = text;
        // 修改tv_result的值为传过来的字符串
        tv_result.setText(showText);
    }

    // 2. 清空结果控件数据并初始化
    public void clear(){
        refreshOperate("");
        refreshText("");
    }

    // 3. 刷新运算结果
    public void refreshOperate(String new_result){
        // 将新获取到的结果赋给全局result(即第一次计算出来的数据)
        // 第一次计算: 4+8=12 下面的firstNum=12
        result=new_result;
        // 在第一次计算得出的结果的基础上,再次进行另一个运算符操作时,第一个操作数是上一次的计算结果
        firstNum=result;
        // 第二个操作数未输入时将其置为空字符串
        secondNum="";
        // 未选择操作运算符时也将其置为为空字符串
        operator="";
    }

    // 4. 对加减乘除进行运算,并返回计算结果
    private double getCalculate_result() {
        while(true){
            if(operator.equals("+")){
                return Double.parseDouble(firstNum)+Double.parseDouble(secondNum);
            }else if(operator.equals("-")){
                return Double.parseDouble(firstNum)-Double.parseDouble(secondNum);
            }else if(operator.equals("*")){
                return Double.parseDouble(firstNum)*Double.parseDouble(secondNum);
            }else if(operator.equals("÷")){
                return Double.parseDouble(firstNum)/Double.parseDouble(secondNum);
            }
        }
    }
}

应用名更改,将strings文件夹下的app_name改为

<string name="app_name">计算器</string>

至此简易的计算机就已完成,但是这个程序还存在一些计算不足,大家可以改进改进

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值