1. 程式人生 > >吃飽了撐著的系列-每月開銷計算器

吃飽了撐著的系列-每月開銷計算器

臨近雙11,一個月就辣麼點點money入賬

然而日常開銷的稽核標準是:

if (餘額 > 0)   // 本來應該是while的算了留點面子明早當早飯

   買買買();

幸好我還是有“個人計劃經濟”的思維,俗稱理財,或者叫省著花

但開啟手機自帶計算器的一剎那

我就感到了手機自帶計算器和我的功能需求之間存在的巨大差異

有時候一糊塗就忘了前面有沒有算過“早餐”或者“話費”的進賬

然後一個月的開銷莫名多幾大百

 

於是乎,毅然決定自己做個APP得了

 

先上圖,權當UI設計效果了,然後就懶懶地不細講XML怎麼搞了嘿嘿

 

至於程式碼嘛......垃圾得要死

主要是因為本人段位太低不懂對映什麼的反正就是XML裡面動態新增ScrollView控制元件或者其他控制元件就不太會

然後程式碼量就超標了,麻煩各位嘲笑完我以後在下面教教我怎麼弄更好~

給幾段程式碼看看吧

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.johan.dailyoutcome">

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="每月開銷計算器"
        android:roundIcon="@drawable/logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

這裡還可以講一下APP的logo怎麼設定,其實就是那個android:icon屬性啦

先去設計一個logo,尺寸合適就行不要太大,存為.png

這個就是我的APP的logo

本身也是圓的

而且注意到application標籤中要設定兩個logo相關的屬性

android:icon和android:roundIcon,顧名思義。然後放到res/drawable目錄下即可

 

佈局xml讓我弄得麻煩了,因為不會動態控制新增的控制元件數量.....

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.johan.dailyoutcome.MainActivity">

    <!-- 相對佈局 -->
    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <!--
            使用滑動條顯示,可顯示更多的事件
            該版本APP固定顯示15個事件條目
            為空的按0元計算
        -->
        <ScrollView
            android:id="@+id/scv"
            android:layout_width="match_parent"
            android:layout_height="400dp">

            <!--
                ScrollView內的相對巢狀
            -->
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

                <!--
                    每行的條目用一個RelativeLayout封裝,
                    包括三個編輯框控制元件,分別是事件的名稱,事件的數量,事件的花費
                -->
                <!--
                    第1個條目
                -->
                <RelativeLayout
                    android:id="@+id/relay1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <!-- 事件的名稱 -->
                    <EditText
                        android:id="@+id/eventName1"
                        android:hint="消費事件1"
                        android:text="早餐"
                        android:layout_width="180dp"
                        android:layout_height="wrap_content"
                        android:background="@android:drawable/edit_text"/>
                    <!-- 事件的數量 -->
                    <EditText
                        android:id="@+id/eventAmount1"
                        android:hint="次數"
                        android:layout_width="80dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toRightOf="@+id/eventName1"
                        android:layout_toEndOf="@+id/eventName1"
                        android:background="@android:drawable/edit_text"/>
                    <!-- 事件的花費 -->
                    <EditText
                        android:id="@+id/eventOutcome1"
                        android:hint="金額"
                        android:layout_width="80dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_toRightOf="@+id/eventAmount1"
                        android:layout_toEndOf="@+id/eventAmount1"
                        android:background="@android:drawable/edit_text"/>
                <!-- 條目相對巢狀標籤結束 -->
                </RelativeLayout>

                <!--
                    第2個條目
                -->
                
                ……


            <!-- ScrollView內的相對巢狀標籤結束 -->
            </RelativeLayout>
        </ScrollView>

        <!--
            開始計算按鈕
        -->
        <Button
            android:id="@+id/calButton"
            android:text="開始計算"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/scv"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:onClick="button_calculateOnClick"/>

        <!--
            計算結果顯示
        -->
        <TextView
            android:id="@+id/outcomeText"
            android:text="計劃總消費:"
            android:textSize="20dp"
            android:textColor="#ff4500"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/calButton"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

 

MainActivity.java【別直接複製!!!有不合法符號會報錯的,要用的話複製關鍵程式碼就行】

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//import android.widget.Toast;

/***************************************************
 * - Project name : DailyOutcome
 * - Filename : MainActivity.java
 * - Author : Johan
 * - Version : 1.0
 * - Date : 2018/10/6
 * - Description : An application to calculate outcome
 *               of a month and help to judge the
 *               purchasing plan.
 *               一款用於計算每月開銷的APP並能幫助調整每月開銷計劃
 * - Function list :
 * - History : [Johan]2018/10/6 Created the project.
 * - Others :
 ***************************************************/

public class MainActivity extends AppCompatActivity {

    // TODO 控制元件
    private EditText eEventName1;        // 消費事件名稱
    private EditText eEventName2;
    ……
    private EditText eEventAmount1;      // 消費次數
    private EditText eEventAmount2;
    ……
    private EditText eEventOutcome1;     // 消費金額
    private EditText eEventOutcome2;
    ……

    private Button calBut;    // 開始計算按鈕
    private TextView omText;  // 結果顯示

    // TODO 支援量
    private final int EVENTS = 15;    // 固定條目數量
    private String[] eName;           // 消費事件名稱
    private String[] eSAmount;        // 字元型數量
    private String[] eSOutcome;       // 字元型金額
    private float[] eAmount;          // 消費次數
    private float[] eOutcome;         // 消費金額


    /********************************************
     * Function name : onCreate
     * Description : TODO 建構函式
     * Variables : Bundle savedInstanceState
     ********************************************/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 獲取控制元件
        initViews();

        // 初始化支援量
        eName = new String[EVENTS];
        eAmount = new float[EVENTS];
        eSAmount = new String[EVENTS];
        eOutcome = new float[EVENTS];
        eSOutcome = new String[EVENTS];
        for(int i = 0;i < EVENTS;i++){
            eName[i] = eSAmount[i] = eSOutcome[i] = "";
            eAmount[i] = eOutcome[i] = 0;
        }
    }




    /********************************************
     * Function name : initViews
     * Description : TODO 初始化控制元件
     * Variables : void
     ********************************************/
    private void initViews(){
        // 消費事件名稱
        eEventName1 = (EditText)findViewById(R.id.eventName1);
        eEventName2 = (EditText)findViewById(R.id.eventName2);
        ……

        // 消費次數
        eEventAmount1 = (EditText)findViewById(R.id.eventAmount1);
        eEventAmount2 = (EditText)findViewById(R.id.eventAmount2);
        ……

        // 消費金額
        eEventOutcome1 = (EditText)findViewById(R.id.eventOutcome1);
        eEventOutcome2 = (EditText)findViewById(R.id.eventOutcome2);
        ……

        // 輔助控制元件
        calBut = (Button)findViewById(R.id.calButton);
        omText = (TextView)findViewById(R.id.outcomeText);
    }




    /********************************************
     * Function name : button_calculateOnClick
     * Description : TODO 開始計算事件
     * Variables : View v
     ********************************************/
    public void button_calculateOnClick(View v){
        // 用於判斷條目是否納入計算的標識
        boolean[] FLAG = new boolean[EVENTS];
        for(int i = 0;i < EVENTS;i++){
            FLAG[i] = false;
        }

        // 獲取每個條目的輸入資訊
        // 消費事件名稱
        eName[0] = eEventName1.getText().toString();
        eName[1] = eEventName2.getText().toString();
        ……

        // 消費次數
        eSAmount[0] = eEventAmount1.getText().toString();
        eSAmount[1] = eEventAmount2.getText().toString();
        ……

        // 消費金額
        eSOutcome[0] = eEventOutcome1.getText().toString();
        eSOutcome[1] = eEventOutcome2.getText().toString();
        ……

        // 第一遍篩選納入計算的條目
        for(int i = 0;i < EVENTS;i++){
            if(eSAmount[i].equals("") || eSOutcome[i].equals("")){
                FLAG[i] = false;
            }
            else{
                FLAG[i] = true;
            }
        }

        // 轉換為計算量,第二遍篩選
        for(int i = 0;i < EVENTS;i++){
            try{
                eAmount[i] = Float.parseFloat(eSAmount[i]);
            }
            catch (Exception e){
                FLAG[i] = false;
                //Toast.makeText(this, eSAmount[i] + "不是合法數字字元", Toast.LENGTH_SHORT).show();
                continue;
            }
        }
        for(int i = 0;i < EVENTS;i++){
            try{
                eOutcome[i] = Float.parseFloat(eSOutcome[i]);
            }
            catch (Exception e){
                FLAG[i] = false;
                //Toast.makeText(this, eSOutcome[i] + "不是合法數字字元", Toast.LENGTH_SHORT).show();
                continue;
            }
        }

        // 計算總量
        float sum = 0;
        for(int i = 0;i < EVENTS;i++){
            if(FLAG[i]){
                sum += eAmount[i] * eOutcome[i];
            }
        }

        // 顯示總量
        omText.setText("計劃總消費:" + sum + "元");
    }
}

 

【完整程式碼請訪問本人的GitHub】

https://github.com/JohanJoeKing/DailyOutcomeCalculater

 

我的手機是Android系統的,用起來我覺得挺好用的

不過確實有很多可以改進的地方

不妨把程式碼開源,大家可以根據自己的口味改進哦~