1. 程式人生 > >Android:簡易計算器

Android:簡易計算器

Android 計算器的簡易實現

Github

私以為這個小專案中最難的是邏輯部分
目前資瓷加、減、乘、除、乘方,括號這些操作符
其中減號可以當做負號使用

AC長按清屏

特色:實時結果 數字分隔(100,000)
支援hentai算式 如3×-(-(-6+5)×2)

不足:還未加入自動調節字型大小功能
連續括號問題存在bug

圖示

UI


預備知識

Android:控制元件中Button、Textview / EditText 的使用及佈局方式
Java:集合,String及陣列的熟練使用
演算法:中綴轉字尾及字尾表示式的計算 及複雜表示式的處理


UI

我的醜醜的UI是仿寫手機中的一個計算器的介面,右邊青色的的條條是可以劃出來使用很多計算符的。但是還沒學到,就先做個條條…

介面使用巢狀的線性佈局實現

巢狀線性佈局的使用技巧:先把佈局塊分好,再寫控制元件,不然巢狀的多了,自己就容易寫亂

上xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width
="match_parent" android:layout_height="match_parent" android:weightSum="8">
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:orientation="vertical" android:weightSum="5"> <EditText
android:id="@+id/text_screen" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:layout_gravity="right" android:gravity="right" android:focusable="false" android:cursorVisible="false" android:singleLine="true" android:background="@null" android:textColor="#666666" android:textSize="60sp" />
<EditText android:id="@+id/text_result" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:layout_gravity="right" android:gravity="right" android:focusable="false" android:cursorVisible="false" android:singleLine="true" android:background="@null" android:textColor="#999999" android:textSize="40sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:weightSum="10" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="7" android:orientation="vertical" android:weightSum="5"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="3"> <Button android:id="@+id/btn_leftBracket" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="(" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_rightBracket" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text=")" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_power" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="^" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3"> <Button android:id="@+id/btn_1" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_2" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_3" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3"> <Button android:id="@+id/btn_4" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_5" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_6" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3"> <Button android:id="@+id/btn_7" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="7" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_8" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="8" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_9" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="9" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightSum="3"> <Button android:id="@+id/btn_del" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="." android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_0" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="0" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_calculate" android:background="#333333" android:textColor="#FFFFFF" android:textSize="26sp" android:text="=" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:weightSum="4"> <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:weightSum="5"> <Button android:id="@+id/btn_clear" android:background="#666666" android:textColor="#FFFFFF" android:textSize="26sp" android:text="AC" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/btn_devide" android:background="#666666" android:textColor="#FFFFFF" android:textSize="26sp" android:text="÷" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/btn_multiply" android:background="#666666" android:textColor="#FFFFFF" android:textSize="26sp" android:text="×" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/btn_minus" android:background="#666666" android:textColor="#FFFFFF" android:textSize="26sp" android:text="-" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/btn_plus" android:background="#666666" android:textColor="#FFFFFF" android:textSize="26sp" android:text="+" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:background="#00FFFC" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>

邏輯

寫了兩個工具類
StringExam類輔助字串處理,Calculate類進行計算

計算思想,從文字框中取得字串,檢查處理,重點處理– ( )×–÷–,轉為字尾表示式再計算

程式碼太長Github自取


UI與邏輯的互動程式碼

看到好多學長制造Button註冊監聽器就寫了幾十行….
註冊監聽器可以跟findViewById寫到一起
其實看一下R.java中的程式碼,連著寫的控制元件,id是連著的。而且這部分程式碼是可修改的。所以用int型別的數就可以批量註冊

後面的 switch case也是同理,數字部分明明可以合併,略去好多重複程式碼

package com.example.hp.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.text.Format;

import static com.example.hp.calculator.Calculate.arrange;
import static com.example.hp.calculator.StringExam.*;

public class InteractActivity extends AppCompatActivity implements View.OnClickListener{

    private StringBuffer text = new StringBuffer();
    private TextView edit_screen;
    private TextView result_screen;
    private String s = new String();
    private boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.module_activity_main);
        //0x7f070022 ~0032
        int id = 0x7f070022;
        for(id=0x7f070022;id<=0x7f070034;id++){
            findViewById(id).setOnClickListener(this);
        }
        findViewById(R.id.btn_clear).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                text = new StringBuffer();
                s = "";
                edit_screen.setText("0");
                result_screen.setText("");
                return false;
            }
        });
        edit_screen = findViewById(R.id.text_screen);
        result_screen = findViewById(R.id.text_result);
        findViewById(R.id.btn_leftBracket).setOnClickListener(this);
        findViewById(R.id.btn_rightBracket).setOnClickListener(this);
//        edit_screen.setMovementMethod(ScrollingMovementMethod.getInstance());
    }


    @Override
    public void onClick(View view) {
        int textLen = text.length();
        switch (view.getId()){
            case R.id.btn_0:
                if(flag){
                    text = new StringBuffer();
                    textLen = 0;
                }
                if(textLen==0||text.charAt(textLen-1)!=')')
                    text.append('0');
                break;
            case R.id.btn_1://0x7f070022
            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:
                if(flag){
                    text = new StringBuffer();
                    textLen = 0;
                }
                if(textLen==0||text.charAt(textLen-1)!=')')
                    text.append((char) ('1'+view.getId()-0x7f070023));
                break;
                //根據id判斷所加字元
            case R.id.btn_del:
                if(textLen==0)
                    text.append("0.");
                else if(isNum(text.charAt(textLen-1))&&judgeDel(text.toString())==false)
                    text.append('.');
                break;
            case R.id.btn_leftBracket:
                if(textLen==0||(textLen>0&&text.charAt(textLen-1)!='.'&&isNum(text.charAt(textLen-1))==false))
                    text.append('(');
                    break;
            case R.id.btn_rightBracket:
                if(cntBrackets(text.toString())>0&&textLen>0&&!isOperation(text.charAt(textLen-1))&&text.charAt(textLen-1)!='.'&&text.charAt(textLen-1)!='(')
                    text.append(')');
                break;
            case R.id.btn_plus:
                //if(textLen>0&&!isOperation(text.charAt(textLen-1))&&text.charAt(textLen-1)!='.')
                if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))
                    text.append('+');
                else if(textLen>0&&cntCharacter(text.toString())==1)
                    text.setCharAt(textLen-1,'+');
                break;
            case R.id.btn_minus:
//             if(textLen==0||(text.charAt(textLen-1)!='+'&&text.charAt(textLen-1)!='-'&&text.charAt(textLen-1)!='.'))
                if(textLen==0)
                    text.append('-');
                else if(textLen==1){
                    if(text.charAt(textLen-1)!='-')
                    text.append('-');
                }
                else if(textLen == 2&&text.charAt(0)=='('){
                    if(text.charAt(1)!='-')
                        text.append('-');
                }
                else if(text.charAt(textLen-1)!='.'&&cntCharacter(text.toString())<2||(cntCharacter(text.toString())==2&&(text.charAt(textLen-1)!='-'&&text.charAt(textLen-2)!='-'))||text.charAt(textLen-1)=='(')
                    text.append('-');
//                if(textLen>0){
//                    Toast.makeText(this,String.valueOf(text.charAt(textLen-1)!='.'&&cntCharacter(text.toString())<=2||(textLen==1&&text.charAt(textLen-1)!='-')),Toast.LENGTH_SHORT).show();
//                }
                break;
            case R.id.btn_multiply:
                if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))
                    text.append('×');
                else if(textLen>0&&cntCharacter(text.toString())==1)
                    text.setCharAt(textLen-1,'×');
                break;
            case R.id.btn_devide:
                if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))
                    text.append('÷');
                else if(textLen>0&&cntCharacter(text.toString())==1)
                    text.setCharAt(textLen-1,'÷');
                break;
            case R.id.btn_power:
                if(textLen>0&&(isNum(text.charAt(textLen-1))||text.charAt(textLen-1)==')'))
                    text.append('^');
                else if(cntCharacter(text.toString())==1){
                    text.setCharAt(textLen-1,'^');
                }
                break;
            case R.id.btn_clear:
                if(textLen>0)
                    text.deleteCharAt(textLen-1);
                if(text.length()==0)
                    s="";
                break;
            case R.id.btn_calculate:
                if(!edit_screen.getText().equals(result_screen.getText())){
                    flag = true;}
                try{
                    text = new StringBuffer(s);
                 //嘻嘻嘻
                    edit_screen.setText(devideByDel(s));
                    result_screen.setText("");
                } catch (Exception e){
                    edit_screen.setText("bug發現,請聯絡開發者kafm。QQ1002605741");
                    text = new StringBuffer();
                    s = "";
                }
                break;
        }
        if(view.getId()!=R.id.btn_calculate){
            flag = false;
//            String tem = devideByDel();
            edit_screen.setText(parseStringAndDevideByDel(text.toString()));//可用正則表示式優化
            if(text.length()>0)
                s = completeString(text.toString());
            if(s.length()>0) {
                try {
                    s=arrange(s);
                } catch (infinityException e) {
                    s = "∞";
                } catch (Exception e) {
//坑
                }
            }
            result_screen.setText(devideByDel(s));
        }
    }
}

2018/8/4