安卓簡易計算器

計算器的功能大家都是很熟悉的,這裏不用多說,直接貼代碼,覺得方便的話大夥就可以粘去用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<>();
        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章