Android-計(jì)算器的實(shí)現(xiàn)

簡(jiǎn)介

計(jì)算器的應(yīng)用在我們的日常生活中十分普遍炕倘,對(duì)于對(duì)安卓開(kāi)發(fā)有興趣的朋友,了解并掌握它的開(kāi)發(fā)是很重要的翰撑。對(duì)于入門(mén)新手來(lái)說(shuō)罩旋,此小項(xiàng)目也是非常有用的,一是熟悉開(kāi)發(fā)工具的使用眶诈,二是逐步入門(mén)Android開(kāi)發(fā)涨醋。接下來(lái)我將逐步總結(jié)一下該應(yīng)用的實(shí)現(xiàn)過(guò)程。

開(kāi)發(fā)工具

Android Studio

開(kāi)發(fā)環(huán)境

1)JDK(Java Development Kit)JDK是Java語(yǔ)言的軟件開(kāi)發(fā)工具包逝撬,主要用于移動(dòng)設(shè)備浴骂、嵌入設(shè)備上的java應(yīng)用程序。

2)SDK(software development kit) SDK是軟件開(kāi)發(fā)工具包宪潮。 被軟件開(kāi)發(fā)工程師用于為特定的軟件包溯警、軟件框架、硬件平臺(tái)狡相、操作系統(tǒng)等建立應(yīng)用軟件的開(kāi)發(fā)工具的集合

實(shí)現(xiàn)過(guò)程

Android自帶的硬件類(lèi)Camera在API21(5.0)之后被拋棄了梯轻,出現(xiàn)了Camera2,在這可以用CameraManager來(lái)打開(kāi)尽棕,實(shí)現(xiàn)硬件閃光燈的開(kāi)啟與關(guān)閉喳挑。

實(shí)現(xiàn)過(guò)程

分析

我認(rèn)為每個(gè)開(kāi)發(fā)android應(yīng)用前,我們肯定是考慮UI設(shè)計(jì)與后臺(tái)的邏輯處理這倆部分滔悉。在UI設(shè)計(jì)中蟀悦,要注意到程序的美觀以及可行性,后臺(tái)對(duì)相應(yīng)的操作進(jìn)行處理氧敢。

1.新建項(xiàng)目

使用Android Studio新建一個(gè)空項(xiàng)目。

2.添加布局

在這里询张,我運(yùn)用的是嵌套的線性布局孙乖,布局中再放置按鈕。UI是之前剛學(xué)Android做的,為了方便沒(méi)有去更改唯袄,其實(shí)可以運(yùn)用TableLayout來(lái)做弯屈,布局會(huì)更美觀。
效果圖如下:


image.png

代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="right"
        android:editable="false" />
    //設(shè)置文本框不能編輯
    //match parent 充滿父類(lèi)容器

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
       >
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_clear"
        android:layout_margin="8dp"
        android:text="C"
        android:textSize="20dp"
        />
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_del"
        android:layout_margin="8dp"
        android:text="DEL"
        android:textSize="20dp"
    />
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_divide"
        android:layout_margin="8dp"
        android:text="÷"
        android:textSize="20dp"
       />
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_multipy"
        android:layout_margin="8dp"
        android:text="×"
        android:textSize="20dp"
         />
        </LinearLayout>

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_7"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="7"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_8"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="8"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_9"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="9"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_minus"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="-"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
    >
        <Button
            android:id="@+id/btn_4"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:text="4"
            android:layout_margin="8dp"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_5"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="5"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_6"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="6"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_plus"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="+"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/btn_1"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:text="1"
            android:layout_margin="8dp"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="2"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_3"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="10dp"
            android:text="3"
            android:textSize="20dp" />
        <Button
            android:id="@+id/btn_a"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:text="="
            android:layout_margin="8dp"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="left"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_0"
            android:layout_width="166dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="10dp"
            android:text="0"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_b"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="."
            android:textSize="20dp" />

        <Button
            android:id="@+id/button"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="空"
            android:textSize="20dp" />

    </LinearLayout>

</LinearLayout>

3.MainActivity的編寫(xiě)

這部分的話就是對(duì)各個(gè)按鈕點(diǎn)擊事件的處理了恋拷,也就是對(duì)鍵入的數(shù)字進(jìn)行運(yùn)算资厉,判斷之類(lèi)的,由于是出學(xué)者蔬顾,我只是做了簡(jiǎn)單的處理宴偿,還有一些BUG沒(méi)能處理好,當(dāng)然诀豁,不影響簡(jiǎn)單運(yùn)算的使用窄刘。
代碼如下:

package com.zewei_w.caculator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn_1;  Button btn_0;
    Button btn_2;  Button btn_7;
    Button btn_3;  Button btn_8;  Button btn_plus;
    Button btn_4;  Button btn_9;  Button btn_minus;
    Button btn_5;  Button btn_a;  Button btn_multipy;
    Button btn_divide;  Button btn_del;
    Button btn_6;  Button btn_b;
    Button btn_clear;
    EditText et_input;
    boolean clear_flag;
    //clearflag默認(rèn)為false
    // 定義這些按鈕和文本框
    //通過(guò)接口方式實(shí)現(xiàn)事件的監(jiān)聽(tīng)
    @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_9=(Button) findViewById(R.id.btn_9);
        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_clear=(Button) findViewById(R.id.btn_clear);
        btn_del=(Button) findViewById(R.id.btn_del);
        btn_divide=(Button) findViewById(R.id.btn_divide);
        btn_multipy=(Button) findViewById(R.id.btn_multipy);
        btn_minus=(Button) findViewById(R.id.btn_minus);
        btn_plus=(Button) findViewById(R.id.btn_plus);
        btn_a=(Button) findViewById(R.id.btn_a);
        btn_b=(Button) findViewById(R.id.btn_b);
        //實(shí)例化按鈕
        // 返回的是view類(lèi)型,需要轉(zhuǎn)換成button類(lèi)型
        et_input=(EditText)findViewById(R.id.et_input);
        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_a.setOnClickListener(this);
        btn_b.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_multipy.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        //設(shè)置按鈕的點(diǎn)擊事件
        //大概是獲取點(diǎn)擊事件
    }
    @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_b:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str + ((Button) v).getText());
                break;
            case R.id.btn_plus:
            case R.id.btn_multipy:
            case R.id.btn_divide:
            case R.id.btn_minus:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                //判斷用
                et_input.setText(str + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_clear:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText("");
                break;
            case R.id.btn_del:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                if (str != null && !str.equals("")) {
                    et_input.setText(str.substring(0, str.length() - 1));
                }
                break;
            case R.id.btn_a:
                getresult();
                break;
        }
    }
        private void getresult()
    {  String exp=et_input.getText().toString();
        if(exp==null||exp.equals(""))
        {
            return;
        }
        if(!exp.contains(" "))
        {
            return;
        }
        if(clear_flag)
        {
            clear_flag=false;
            return;
        }
             clear_flag=true;
              double result=0;
             String s1=exp.substring(0,exp.indexOf(" "));
             String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
             String s2=exp.substring(exp.indexOf(" ")+3);
        if(!s1.equals("")&&!s2.equals("")) {
            double d1= Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            //強(qiáng)制類(lèi)型轉(zhuǎn)換

            if (op.equals("+")) {
                result = d1 + d2;
            } else if (op.equals("-")) {
                 result = d1-d2;
            } else if (op.equals("×")) {
                result=d1*d2;
            } else if (op.equals("÷")) {
            if(d1==0)
            {result=0;}else  result=d1/d2;
            }
             if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
                int r = (int)result;
                 et_input.setText(r + "");
             }
              else
                 {et_input.setText(result + "");}
        }else if(!s1.equals("")&&s2.equals(""))
        {
            et_input.setText(exp);
        }else if(s1.equals("")&&!s2.equals(""))
        {   double d2= Double.parseDouble(s2);
            if (op.equals("+")) {
                result = 0 + d2;
            } else if (op.equals("-")) {
                result = 0 - d2;
            } else if (op.equals("×")) {
                result=0*d2;
            } else if (op.equals("÷")) {
               result=0;
            }
        }
        else {
            et_input.setText("");
        }
    }
        }

代碼中有適當(dāng)?shù)淖⑨屨f(shuō)明舷胜。

4.運(yùn)行程序

運(yùn)行程序娩践,調(diào)試錯(cuò)誤,可更改APP名字烹骨,圖標(biāo)等
這里我在AndroidMa'nifest.xml文件中更改了主題
效果如果:
(編寫(xiě)手機(jī)截圖)


可拓展指出:

1.程序界面美化
2.功能完善化-檢查BUG
3.增加運(yùn)算功能

個(gè)人總結(jié)與感悟:

這個(gè)應(yīng)用同手電筒一樣翻伺,都是入門(mén)級(jí)的,但是我仍舊極力推薦沮焕,對(duì)于初學(xué)者非常有用吨岭,在編寫(xiě)過(guò)程中,學(xué)到了不少知識(shí)點(diǎn)遇汞,同時(shí)我發(fā)現(xiàn)未妹,隨著Android的不斷更新,很多接口以及寫(xiě)法都有些許改變空入,所以我認(rèn)為络它,遇到了問(wèn)題,查找百度歪赢、谷歌等固然可以化戳,但一定不能照搬,因?yàn)楹芏鄦?wèn)題其實(shí)是很久之前的埋凯,而很多知識(shí)的用法卻一直在更新改變点楼,所以要漸漸地學(xué)會(huì)去研讀開(kāi)發(fā)文檔,找到用法白对,這點(diǎn)接下來(lái)我會(huì)開(kāi)始去做掠廓,因?yàn)榇_實(shí)有很多的不同,比如在主題的運(yùn)用這點(diǎn)甩恼,因?yàn)橛梅▎?wèn)題蟀瞧,我就被干擾了不久沉颂。
編者剛開(kāi)始寫(xiě)簡(jiǎn)書(shū)不久,也是Android的初學(xué)者悦污,很多問(wèn)題還不懂铸屉,寫(xiě)下這些,對(duì)一部分讀者有用自然是好切端,更多地是想記錄下自己學(xué)習(xí)的過(guò)程彻坛,逐步提高自己,當(dāng)然踏枣,有不好的地方昌屉,非常歡迎高手指點(diǎn)批評(píng),非常歡迎相互討論椰于,相互提高怠益!
今天到這里,謝謝瘾婿!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蜻牢,一起剝皮案震驚了整個(gè)濱河市抢呆,隨后出現(xiàn)的幾起案子抱虐,更是在濱河造成了極大的恐慌,老刑警劉巖饥脑,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恳邀,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡灶轰,警方通過(guò)查閱死者的電腦和手機(jī)谣沸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)笋颤,“玉大人乳附,你說(shuō)我怎么就攤上這事赋除。” “怎么了非凌?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵举农,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我敞嗡,道長(zhǎng)滚停,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任粥惧,我火速辦了婚禮键畴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘突雪。我一直安慰自己起惕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布咏删。 她就那樣靜靜地躺著惹想,像睡著了一般。 火紅的嫁衣襯著肌膚如雪督函。 梳的紋絲不亂的頭發(fā)上嘀粱,一...
    開(kāi)封第一講書(shū)人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音辰狡,去河邊找鬼锋叨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛宛篇,可吹牛的內(nèi)容都是我干的娃磺。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼叫倍,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼偷卧!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起吆倦,我...
    開(kāi)封第一講書(shū)人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤听诸,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后逼庞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體蛇更,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年赛糟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了派任。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡璧南,死狀恐怖掌逛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情司倚,我是刑警寧澤豆混,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布篓像,位于F島的核電站,受9級(jí)特大地震影響皿伺,放射性物質(zhì)發(fā)生泄漏员辩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一鸵鸥、第九天 我趴在偏房一處隱蔽的房頂上張望奠滑。 院中可真熱鬧,春花似錦妒穴、人聲如沸宋税。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)杰赛。三九已至,卻和暖如春矮台,著一層夾襖步出監(jiān)牢的瞬間乏屯,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工嘿架, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留瓶珊,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓耸彪,卻偏偏與公主長(zhǎng)得像伞芹,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蝉娜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,077評(píng)論 25 707
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,755評(píng)論 22 665
  • (這是九瓣 365日寫(xiě)作計(jì)劃第1天的寫(xiě)作內(nèi)容召川。) 內(nèi)供的苦惱其實(shí)是來(lái)自被鼻子刺傷的自尊心南缓。 芥川的《鼻》中內(nèi)供鼻子...
    九瓣閱讀 840評(píng)論 2 4
  • 春暖花開(kāi),四月櫻花季荧呐。喜歡拍花花草草汉形,大路小路處處有驚喜,迎春花倍阐,玉蘭花概疆,櫻花,杜鵑花峰搪,郁金香…黃色白色粉色紅色…...
    hiElena閱讀 178評(píng)論 0 0
  • 1.HTTP 1.1全局規(guī)范 URLURL的組成:基本的網(wǎng)絡(luò)地址 + 分支節(jié)點(diǎn)http://172.19.201....
    zlcook閱讀 897評(píng)論 0 0