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

簡介

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

開發(fā)工具

Android Studio

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

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

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

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

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

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

分析

我認(rèn)為每個(gè)開發(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做的,為了方便沒有去更改,其實(shí)可以運(yùn)用TableLayout來做,布局會(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 充滿父類容器

    <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的編寫

這部分的話就是對(duì)各個(gè)按鈕點(diǎn)擊事件的處理了,也就是對(duì)鍵入的數(shù)字進(jìn)行運(yùn)算,判斷之類的,由于是出學(xué)者,我只是做了簡單的處理,還有一些BUG沒能處理好,當(dāng)然,不影響簡單運(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
    // 定義這些按鈕和文本框
    //通過接口方式實(shí)現(xiàn)事件的監(jiān)聽
    @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類型,需要轉(zhuǎn)換成button類型
        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)制類型轉(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明。

4.運(yùn)行程序

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


可拓展指出:

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

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

這個(gè)應(yīng)用同手電筒一樣,都是入門級(jí)的,但是我仍舊極力推薦,對(duì)于初學(xué)者非常有用,在編寫過程中,學(xué)到了不少知識(shí)點(diǎn),同時(shí)我發(fā)現(xiàn),隨著Android的不斷更新,很多接口以及寫法都有些許改變,所以我認(rèn)為,遇到了問題,查找百度、谷歌等固然可以,但一定不能照搬,因?yàn)楹芏鄦栴}其實(shí)是很久之前的,而很多知識(shí)的用法卻一直在更新改變,所以要漸漸地學(xué)會(huì)去研讀開發(fā)文檔,找到用法,這點(diǎn)接下來我會(huì)開始去做,因?yàn)榇_實(shí)有很多的不同,比如在主題的運(yùn)用這點(diǎn),因?yàn)橛梅▎栴},我就被干擾了不久。
編者剛開始寫簡書不久,也是Android的初學(xué)者,很多問題還不懂,寫下這些,對(duì)一部分讀者有用自然是好,更多地是想記錄下自己學(xué)習(xí)的過程,逐步提高自己,當(dāng)然,有不好的地方,非常歡迎高手指點(diǎn)批評(píng),非常歡迎相互討論,相互提高!
今天到這里,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容