安卓简易计算器

计算器的功能大家都是很熟悉的,这里不用多说,直接贴代码,觉得方便的话大伙就可以粘去用0.0

控件初始化和绑定监听器使用了一个工具[link]https://www.buzzingandroid.com/tools/android-layout-finder/

效果演示

在这里插入图片描述

代码

布局是很随意的,大家可以自己写0.0

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <!--文本标签-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tv"
            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"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button android:id="@+id/qingchu" android:text="C" android:layout_height="wrap_content"  android:layout_width="wrap_content" android:textSize="26sp"/>
        <Button android:id="@+id/del" android:text="DEL" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="26sp"/>
    </LinearLayout>
    <!--网格布局-->
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="4"
        android:columnCount="4"
        >
        <Button android:id="@+id/button7" android:text="7" android:textSize="28sp" />
        <Button android:id="@+id/button8" android:text="8" android:textSize="28sp" />
        <Button android:id="@+id/button9" android:text="9" android:textSize="28sp" />
        <Button android:id="@+id/reduce" android:text="-" android:textSize="28sp" />
        <Button android:id="@+id/button4" android:text="4" android:textSize="28sp" />
        <Button android:id="@+id/button5" android:text="5" android:textSize="28sp" />
        <Button android:id="@+id/button6" android:text="6" android:textSize="28sp" />
        <Button android:id="@+id/add" android:text="+" android:textSize="28sp" />
        <Button android:id="@+id/button1" android:text="1" android:textSize="28sp" />
        <Button android:id="@+id/button2" android:text="2" android:textSize="28sp" />
        <Button android:id="@+id/button3" android:text="3" android:textSize="28sp" />
        <Button android:id="@+id/cheng" android:text="×" android:textSize="28sp" />
        <Button android:id="@+id/dian" android:text="." android:textSize="28sp" />
        <Button android:id="@+id/button0" android:text="0" android:textSize="28sp" />
        <Button android:id="@+id/equal" android:text="=" android:textSize="28sp" />
        <Button android:id="@+id/chu" android:text="÷" android:textSize="28sp" />

        </GridLayout>
</LinearLayout>

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 java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = MainActivity.class.getSimpleName();
    private Button qingchu;
    private Button del;
    private Button button7;
    private Button button8;
    private Button button9;
    private Button reduce;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button add;
    private Button button3;
    private Button button2;
    private Button button1;
    private Button cheng;
    private Button dian;
    private Button button0;
    private Button equal;
    private Button chu;
    private TextView textView;
    private List<Character> strs;//用来存当前输入要计算的数字
    private double last=-1000;//用来记录每次计算后上一次的数值
    private double current;//用来记录当前输入要计算的数字,从strs中转化而来
    private double sum=-1000;//用来记录当前计算后的值,之后赋值给last
    private char lastChar='1';//用来记录上一次输入的计算符号
    private boolean isFirst =true;//第一次输入或者每次清空后 sum的值在第一次要有current赋值给他 不然完成last = sum会产生错误结果
    private boolean isYunsuan =false;//判断是否要进行计算


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

    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2019-03-16 20:49:35 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void findViews() {
        qingchu = (Button)findViewById( R.id.qingchu );
        del = (Button)findViewById( R.id.del );
        button7 = (Button)findViewById( R.id.button7 );
        button8 = (Button)findViewById( R.id.button8 );
        button9 = (Button)findViewById( R.id.button9 );
        reduce = (Button)findViewById( R.id.reduce );
        button4 = (Button)findViewById( R.id.button4 );
        button5 = (Button)findViewById( R.id.button5 );
        button6 = (Button)findViewById( R.id.button6 );
        add = (Button)findViewById( R.id.add );
        button3 = (Button)findViewById( R.id.button3 );
        button2 = (Button)findViewById( R.id.button2 );
        button1 = (Button)findViewById( R.id.button1 );
        cheng = (Button)findViewById( R.id.cheng );
        dian = (Button)findViewById( R.id.dian );
        button0 = (Button)findViewById( R.id.button0 );
        equal = (Button)findViewById( R.id.equal );
        chu = (Button)findViewById( R.id.chu );
        textView = (TextView)findViewById(R.id.tv);
        strs = new ArrayList<>();
    }

    private void setListener() {
        qingchu.setOnClickListener( this );
        del.setOnClickListener( this );
        button7.setOnClickListener( this );
        button8.setOnClickListener( this );
        button9.setOnClickListener( this );
        reduce.setOnClickListener( this );
        button4.setOnClickListener( this );
        button5.setOnClickListener( this );
        button6.setOnClickListener( this );
        add.setOnClickListener( this );
        button3.setOnClickListener( this );
        button2.setOnClickListener( this );
        button1.setOnClickListener( this );
        cheng.setOnClickListener( this );
        dian.setOnClickListener( this );
        button0.setOnClickListener( this );
        equal.setOnClickListener( this );
        chu.setOnClickListener( this );
    }

    /**
     * Handle button click events<br />
     * <br />
     * Auto-created on 2019-03-16 20:49:35 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    @Override
    public void onClick(View v) {

        if ( v == qingchu ) {//清除操作要把值初始化
            // Handle clicks for qingchu
            textView.setText("0");
            strs = new ArrayList<>();
            isFirst = true;
            current = 0;
            lastChar='1';
        } else if ( v == del ) {
            // Handle clicks for del
            if (strs.size()>0&&strs!=null) {
                strs.remove(strs.size() - 1);
                update('0');
                if (strs.size()==0){//剩下一个数减的话就置为0
                    textView.setText("0");
                }
            }
        } else if ( v == button7 ) {
            // Handle clicks for button7
            strs.add('7');
            update('0');
        } else if ( v == button8 ) {
            // Handle clicks for button8
            strs.add('8');
            update('0');
        } else if ( v == button9 ) {
            // Handle clicks for button9
            strs.add('9');
            update('0');
        } else if ( v == reduce ) {
            // Handle clicks for reduce
            update('-');
        } else if ( v == button4 ) {
            // Handle clicks for button4
            strs.add('4');
            update('0');
        } else if ( v == button5 ) {
            // Handle clicks for button5
            strs.add('5');
            update('0');
        } else if ( v == button6 ) {
            // Handle clicks for button6
            strs.add('6');
            update('0');
        } else if ( v == add ) {
            // Handle clicks for add
            update('+');
        } else if ( v == button3 ) {
            // Handle clicks for button3
            strs.add('3');
            update('0');
        } else if ( v == button2 ) {
            // Handle clicks for button2
            strs.add('2');
            update('0');
        } else if ( v == button1 ) {
            // Handle clicks for button1
            strs.add('1');
            update('0');
        } else if ( v == cheng ) {
            // Handle clicks for cheng

            update('×');
        } else if ( v == dian ) {
            // Handle clicks for dian
            //这里的操作是为了避免输入多个'.' 小数点只有一位
            boolean isNone = true;
            for (char c :strs){
                if (c == '.'){
                    isNone = false;
                }
            }
            if (isNone) {
                strs.add('.');
                update('0');
            }
        } else if ( v == button0 ) {
            // Handle clicks for button0
            strs.add('0');
            update('0');
        } else if ( v == equal ) {
            // Handle clicks for equal
            update('=');

        } else if ( v == chu ) {
            // Handle clicks for chu
            update('÷');
        }
    }

    private void update(char x) {
        //遍历集合 转化为String类型方便显示
        String str = "";
        for(char c : strs){
            str +=c;
        }
        //str!=''是因为Double.valueof不是转化“”这个字符串
        if (x=='0'&&str!="") {
            textView.setText(str);
            current = Double.valueOf(str);
        }
        //第一次输入的赋值,不这样做下面的last = sum 会产生错误结果
        if (isFirst){sum = current;}
        //进行符号判断并在textview显示符号
        if (x == '-'){
            textView.setText("-");
            strs = new ArrayList<>();isFirst = false;
        }else if (x == '+'){
            textView.setText("+");
            strs = new ArrayList<>();isFirst = false;
        }else if (x == '×'){
            textView.setText("×");
            strs = new ArrayList<>();isFirst = false;
        }else if (x == '÷'){
            textView.setText("÷");
            strs = new ArrayList<>();isFirst = false;
        }

        /**
         * lastChar!=‘1’是为了保证在获得符号才进入计算
         * isYuansuan 比如要输入11x11 这个运算  因为我们这个函数是每次输入一个char(数字)就会更新一次,我们希望
         *            不会再输入到11x1 的时候 就进入这个运算判断,而是等到我们输入到11x11 后面再接一个符号才运算,
         *            isYunsuan也是在这个时候为true的,这样才能保证运算正确
         */
        if (lastChar!='1'&&isYunsuan&&x!='0'){
            //计算值
            if (lastChar=='-'){
                sum = last - current;
            }else if (lastChar=='+'){
                sum =  last + current;
            }else if (lastChar=='×'){
                sum = (last * current);
            }else if (lastChar=='÷'){
                sum = (last / current);
            }
            isYunsuan = false;//运算完成后置位false

        }

        last = sum;//保存当前计算结果到last 用于下一次的计算
        if (x!='0') {
            lastChar = x;
            isYunsuan = true;
        }

        //当按‘=’的时候,进行输出结果
        if (x=='='){
            textView.setText(sum+"");
            lastChar = '1';
            strs = new ArrayList<>();
        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值