sholler's プログラムとメモ帳

日々プログラムをしていて出てきたエラーの処理、技術関連の気になった記事などを題材に書いていくブログ。

【Android】電卓を作る1

Androidで電卓を作る課題が出たので、
自分がやった事、学んだ事をメモ。

まずは電卓内で起こる「イベント」「状態」「動作」をまとめる。

[イベント]
・0〜9ボタンが押される
・四則演算ボタンが押される
・=ボタンが押される
・Cボタンが押される
・ACボタンが押される

[状態]
・数値A入力中
演算子選択
・数値B入力中
・結果表示

[動作]
・入力数値をAに反映
・数値Aを保持
・数値Aクリア
・入力数値をBに反映
・数値Bを保持
・数値Bクリア
・数値AとBクリア
・演算結果を保持
・ディスプレイ出力
・四則演算を保持
・ステータスチェック


必要な変数を定義

//変数定義
private String inputStr = "";  //入力値
private String aNum = "";      //数値A
private String bNum = "";      //数値B
private String op = "";        //演算子
private String result = "";    //演算結果
private int state = 0;         //ステータス (0:A入力中  1:演算子選択  2:B入力中  3:結果表示)


buttonMethod用意。
処理は全てbuttonMethodに記述していく

public void buttonMethod(View b) {
	//ココに処理を書いていく
}


起きるイベントと電卓の状態で管理する様に作っていくと
拡張性があって良いらしい。

次回に電卓のソースを記述するので、先にレイアウトだけ。

activity_main.xml内容

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#777"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/answerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#fff"
        android:gravity="center_vertical|right"
        android:padding="5dp"
        android:textSize="30sp" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="337dp"
        android:layout_weight="1" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button19"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="C" />

            <Button
                android:id="@+id/Button03"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="BS" />

            <Button
                android:id="@+id/Button02"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="+/-" />

            <Button
                android:id="@+id/Button01"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="AC" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button07"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="7" />

            <Button
                android:id="@+id/Button06"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="8" />

            <Button
                android:id="@+id/Button05"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="9" />

            <Button
                android:id="@+id/Button04"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="÷" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button11"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="4" />

            <Button
                android:id="@+id/Button10"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="5" />

            <Button
                android:id="@+id/Button09"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="6" />

            <Button
                android:id="@+id/Button08"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="×" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button15"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="1" />

            <Button
                android:id="@+id/Button14"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="2" />

            <Button
                android:id="@+id/Button13"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="3" />

            <Button
                android:id="@+id/Button12"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="-" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button18"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="0" />

            <Button
                android:id="@+id/Button17"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="00" />

            <Button
                android:id="@+id/Button16"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="=" />

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="+" />

        </TableRow>

    </TableLayout>

</LinearLayout>

今はデザインは気にせず、簡単に。
後でデザインもこっていきたいな