移动编程技术作业(简易计算器)

MainActivity.java 文件

package com.example.administrator.calculator;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView te_input;
    Button btn_clr;
    Button btn_1,btn_2,btn_3,btn_add;
    Button btn_4,btn_5,btn_6,btn_sub;
    Button btn_7,btn_8,btn_9,btn_mul;
    Button btn_pt,btn_0,btn_eq,btn_div;
    boolean clr_flag;      //判断文本编辑框中是否为空

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        te_input = (TextView) findViewById(R.id.te_input);
        btn_clr = (Button) findViewById(R.id.btn_clr);

        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_add = (Button) findViewById(R.id.btn_add);

        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_sub = (Button) findViewById(R.id.btn_sub);

        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_mul = (Button) findViewById(R.id.btn_mul);

        btn_pt = (Button) findViewById(R.id.btn_pt);
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_eq = (Button) findViewById(R.id.btn_eq);
        btn_div = (Button) findViewById(R.id.btn_div);

        //给按钮设置的点击事件
        btn_clr.setOnClickListener(this);

        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_add.setOnClickListener(this);

        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_sub.setOnClickListener(this);

        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_mul.setOnClickListener(this);

        btn_pt.setOnClickListener(this);
        btn_0.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_eq.setOnClickListener(this);
        btn_div.setOnClickListener(this);

    }

    public void onClick(View v){
        String str = te_input.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_pt:
                if (clr_flag){
                    clr_flag = false;
                    str = "";
                    te_input.setText("");
                }
                te_input.setText(str + ((Button)v).getText());
                break;
            case R.id.btn_add:
            case R.id.btn_sub:
            case R.id.btn_mul:
            case R.id.btn_div:
                if (clr_flag){
                    clr_flag = false;
                    str = "";
                    te_input.setText("");
                }
                if (str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/")){
                    str = str.substring(0,str.indexOf(""));
                }
                te_input.setText(str + " " + ((Button)v).getText() + " ");
                break;
            case R.id.btn_clr:
                if (clr_flag)
                    clr_flag = false;
                str = "";
                te_input.setText("");
                break;
            case R.id.btn_eq: //单独运算最后结果
                getResult();//调用下面的方法
                break;
        }
    }
    private void getResult(){
        String exp = te_input.getText().toString();
        if (exp == null || exp.equals("")) return;
        //因为没有运算符所以不用运算
        if (!exp.contains(" ")){
            return;
        }
        if (clr_flag){
            clr_flag = false;
            return;
        }
        clr_flag = 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);
        double cnt = 0;
        if (!s1.equals("") && !s2.equals("")){
            double d1 = Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")){
                cnt = d1 + d2;
            }
            if (op.equals("-")){
                cnt = d1 - d2;
            }
            if (op.equals("*")){
                cnt = d1 * d2;
            }
            if (op.equals("/")){
                cnt = d1 / d2;
            }
            if (!s1.contains(".") && s2.contains(".") && !op.contains("/")){
                int res = (int)cnt;
                te_input.setText(res + "");
            }else {
                te_input.setText(cnt + "");
            }

        }
        //如果s1不为空,s2为空,执行下一步
        else if (!s1.equals("") && s2.equals("")){
            double d1 = Double.parseDouble(s1);
            if (op.equals("+")){
                cnt = d1;
            }
            if (op.equals("-")){
                cnt = d1;
            }
            if (op.equals("*")){
                cnt = 0;
            }
            if (op.equals("/")){
                cnt = 0;
            }
            if (!s1.contains(".")){
                int res = (int) cnt;
                te_input.setText(res + "");
            }else {
                te_input.setText(cnt + "");
            }
        }
        else if (s1.equals("") && !s2.equals("")){
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")){
                cnt = d2;
            }
            if (op.equals("-")){
                cnt = 0-d2;
            }
            if (op.equals("*")){
                cnt = 0;
            }
            if (op.equals("/")){
                cnt = 0;
            }
        }
        else {
            te_input.setText("");
        }
    }
}

activity_main.xml 文件

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

    <TextView
        android:id="@+id/te_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_marginLeft="4px"
        android:gravity="left"
        android:text="0"
        android:textSize="50dip"
         />
    <Button
        android:id="@+id/btn_clr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_1"
        android:text="1"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_2"
        android:text="2"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_3"
        android:text="3"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_add"
        android:text="+"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_4"
        android:text="4"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_5"
        android:text="5"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_6"
        android:text="6"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_sub"
        android:text="-"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_7"
        android:text="7"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_8"
        android:text="8"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_9"
        android:text="9"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_mul"
        android:text="*"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_pt"
        android:text="."
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_0"
        android:text="0"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_eq"
        android:text="="
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_div"
        android:text="/"
        android:textSize="26sp"
        />

</GridLayout>

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值