溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

android?studio如何實現(xiàn)簡易的計算器

發(fā)布時間:2022-05-23 09:08:22 來源:億速云 閱讀:338 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“android studio如何實現(xiàn)簡易的計算器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“android studio如何實現(xiàn)簡易的計算器”吧!

先看效果圖

基本功能:加,減,乘,除

android?studio如何實現(xiàn)簡易的計算器

核心代碼實現(xiàn)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_pt;
    Button btn_mul, btn_div, btn_add, btn_sub;
    Button btn_clr, btn_del, btn_eq;
    EditText et_input;
    boolean clr_flag;


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

        //初始化控件
        btn_0 = (Button) findViewById(R.id.btn_0);
        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_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        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_pt = (Button) findViewById(R.id.btn_pt);
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_sub = (Button) findViewById(R.id.btn_sub);
        btn_mul = (Button) findViewById(R.id.btn_mul);
        btn_div = (Button) findViewById(R.id.btn_div);
        btn_clr = (Button) findViewById(R.id.btn_clr);
        btn_del = (Button) findViewById(R.id.btn_del);
        btn_eq = (Button) findViewById(R.id.btn_eq);
        et_input = (EditText) findViewById(R.id.et_input);

        //給按鈕設(shè)置的點擊事件
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_pt.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_mul.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_clr.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_eq.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        String str = et_input.getText().toString();
        switch (v.getId()) {
            case R.id.btn_0:
            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_pt:
                if (clr_flag) {
                    clr_flag = false;
                    str = "";
                    et_input.setText("");
                }
                et_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 = "";
                    et_input.setText("");
                }
                if (str.contains("+") || str.contains("-") || str.contains("×") || str.contains("÷")) {
                    str = str.substring(0, str.indexOf(" "));
                }
                et_input.setText(str + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_clr:
                if (clr_flag)
                    clr_flag = false;
                str = "";
                et_input.setText("");
                break;
            case R.id.btn_del: //判斷是否為空,然后在進(jìn)行刪除
                if (clr_flag) {
                    clr_flag = false;
                    str = "";
                    et_input.setText("");
                } else if (str != null && !str.equals("")) {
                    et_input.setText(str.substring(0, str.length() - 1));
                }
                break;
            case R.id.btn_eq: //單獨運算最后結(jié)果
                getResult();//調(diào)用下面的方法
                break;
        }


    }

    private void getResult() {
        String exp = et_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("÷")) {
                if (d2 == 0) cnt = 0;
                else cnt = d1 / d2;
            }
            if (!s1.contains(".") && !s2.contains(".") && !op.equals("÷")) {
                int res = (int) cnt;
                et_input.setText(res + "");
            } else {
                et_input.setText(cnt + "");
            }
        }
        //如果s1是空    s2不是空  就執(zhí)行下一步
        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;
                et_input.setText(res + "");
            } else {
                et_input.setText(cnt + "");
            }
        }
        //如果s1是空    s2不是空  就執(zhí)行下一步
        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;
            }
            if (!s2.contains(".")) {
                int res = (int) cnt;
                et_input.setText(res + "");
            } else {
                et_input.setText(cnt + "");
            }
        } else {
            et_input.setText("");
        }
    }
}

到此,相信大家對“android studio如何實現(xiàn)簡易的計算器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI