在整個開發環境終於搞定   且也再次回想起之前寫 Java 相關的記憶

所以剛剛利用時間寫個簡單的計算機   讓自己回憶起更多這方面的 programming knowledge

這個 Calculator 有用到的部份主要有幾個:

1. 整個 AP 的畫面 Layout 不是透過  xml , 而是透過 Java code 一步步將之建立排版

    此 Calculator 主要用到的是 LinearLayout, 共用了 7 個 LinearLayout 兜成的

    每個 Layout 上放置不同的 widgets, 有 button, 有 EditText

2. Calculator 因為要去接收 key event, 所以實作了 OnClickListener Interface

    在 class 裡實作 public void onClick(View v){} (和 Thread & Run function 一樣)

    而在 onClick 裡面判定哪個 widget 被點選則是利用每個 widget 的 id

    這個 id 是在一開始創造 button widget 時都會給一個值來作區別

-------------------------------- 以下則是此 Java source -----------------------------------------

package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Button;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;

public class Calculator extends Activity implements OnClickListener
{
    private LinearLayout root;
    private EditText resultVal;
    private int val_1 = 0,val_2 = 0;
    private int op = 0;
   
    private static final int BT_0 = 0;
    private static final int BT_1 = 1;
    private static final int BT_2 = 2;
    private static final int BT_3 = 3;
    private static final int BT_4 = 4;
    private static final int BT_5 = 5;
    private static final int BT_6 = 6;
    private static final int BT_7 = 7;
    private static final int BT_8 = 8;
    private static final int BT_9 = 9;
    private static final int BT_CLEAR = 10;
    private static final int BT_NAG = 11;
    private static final int BT_DIV = 12;
    private static final int BT_ADD = 13;
    private static final int BT_SUB = 14;
    private static final int BT_X = 15;
    private static final int BT_EQUAL = 16;
    private static final int BT_DOT = 17;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       
        //create root layout's params
        LinearLayout.LayoutParams rootContainer = new LinearLayout.LayoutParams(
                                                      ViewGroup.LayoutParams.FILL_PARENT,
                                                      ViewGroup.LayoutParams.WRAP_CONTENT,
                                                      0.0f);
                                                     
        //create widget layout's params
        LinearLayout.LayoutParams widgetContainer = new LinearLayout.LayoutParams(
                                                      ViewGroup.LayoutParams.FILL_PARENT,
                                                      ViewGroup.LayoutParams.FILL_PARENT,
                                                      1.0f);
                                                     
        root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setBackgroundColor(Color.LTGRAY);
        root.setLayoutParams(rootContainer);
       
        //the value field's layout on the top of screen
        LinearLayout l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setBackgroundColor(Color.GRAY);
        l1.setLayoutParams(widgetContainer);
        root.addView(l1);
       
        //the value field on the top of screen
        resultVal = new EditText(this);
        resultVal.setFocusable(false);
        resultVal.setLayoutParams(widgetContainer);
        resultVal.setText("0");
        resultVal.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
        l1.addView(resultVal);

        //create 4 button of line 1
        l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setBackgroundColor(Color.GRAY);
        l1.setLayoutParams(widgetContainer);
        root.addView(l1);       

        createButton(l1,widgetContainer,"c",BT_CLEAR);
        createButton(l1,widgetContainer,"+/-",BT_NAG);
        createButton(l1,widgetContainer,"/",BT_DIV);
        createButton(l1,widgetContainer,"x",BT_X);
       
        //create 4 button of line 2
        l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setBackgroundColor(Color.GRAY);
        l1.setLayoutParams(widgetContainer);
        root.addView(l1);       

        createButton(l1,widgetContainer,"7",BT_7);
        createButton(l1,widgetContainer,"8",BT_8);
        createButton(l1,widgetContainer,"9",BT_9);
        createButton(l1,widgetContainer,"-",BT_SUB);
       
        //create 4 button of line 2
        l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setBackgroundColor(Color.GRAY);
        l1.setLayoutParams(widgetContainer);
        root.addView(l1);       

        createButton(l1,widgetContainer,"4",BT_4);
        createButton(l1,widgetContainer,"5",BT_5);
        createButton(l1,widgetContainer,"6",BT_6);
        createButton(l1,widgetContainer,"+",BT_ADD);       
       
       
        //create 4 button of line 3
        l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setBackgroundColor(Color.GRAY);
        l1.setLayoutParams(widgetContainer);
        root.addView(l1);       

        createButton(l1,widgetContainer,"1",BT_1);
        createButton(l1,widgetContainer,"2",BT_2);
        createButton(l1,widgetContainer,"3",BT_3);
        createButton(l1,widgetContainer,".",BT_DOT);
       
        //create 4 button of line 4
        l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setBackgroundColor(Color.GRAY);
        l1.setLayoutParams(widgetContainer);
        root.addView(l1);       

        createButton(l1,widgetContainer,"0",BT_0);
        createButton(l1,widgetContainer,"=",BT_EQUAL);
       
        setContentView(root);
    }
   
    private void createButton(LinearLayout layer,LinearLayout.LayoutParams widgetContainer,String str,int Id){
        Button b = new Button(this);
        b.setId(Id);
        b.setText(str);
        b.setTextColor(Color.RED);
        b.setLayoutParams(widgetContainer);
        b.setOnClickListener(this);
        layer.addView(b);
    }
   
    public void onClick(View v){
        Log.d("DEBUG", "v.getId() : "+v.getId());
        switch (v.getId())
        {
            case BT_CLEAR:
                val_1 = 0;
                val_2 = 0;
                resultVal.setText("0");
                break;
            case BT_0:
            case BT_1:
            case BT_2:
            case BT_3:
            case BT_4:
            case BT_5:
            case BT_6:
            case BT_7:
            case BT_8:
            case BT_9:
                val_1 = val_1 * 10 + v.getId();
                resultVal.setText(Integer.toString(val_1));
                break;
            case BT_EQUAL:
            case BT_ADD:
            case BT_SUB:
            case BT_DIV:
            case BT_X:
                switch (op)
                {
                    case BT_ADD:
                        if (val_2 != 0)
                            val_2 = val_1 + val_2;
                        else
                            val_2 = val_1;
                        break;
                    case BT_SUB:
                        if (val_2 != 0)
                            val_2 = val_2 - val_1;
                        else
                            val_2 = val_1;
                        break;
                    case BT_DIV:
                        if (val_2 != 0 && val_1 > 0)
                            val_2 = val_2 / val_1;
                        else
                            val_2 = val_1;
                        break;            
                    case BT_X:
                        if (val_2 != 0)
                            val_2 = val_2 * val_1;
                        else
                            val_2 = val_1;
                        break;          
                    default:
                        val_2 = val_1;
                        break;
                }
                val_1 = 0;
                resultVal.setText(Integer.toString(val_2));
                if (v.getId() == BT_EQUAL)
                {
                    val_1 = val_2;
                    op = 0;
                    val_2 = 0;
                }
                else
                    op = v.getId();
                break;
        }
    }
}

arrow
arrow
    全站熱搜

    tixirene 發表在 痞客邦 留言(0) 人氣()