Androidアプリ開発メモ

プログラミング初心者がAndroidのアプリを作るよ!主に自分用の忘備録です

電卓

f:id:olee46:20170531221545j:plain
機能
・電卓アプリ
・整数値の入力
・小数点での結果の出力


activity_main.xml
f:id:olee46:20170531221547j:plain


MainActivity.java

package com.example.orisa.mytablelayout1;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //部品の変数
    Button btn0;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn7;
    Button btn8;
    Button btn9;

    Button divBtn;
    Button mulBtn;
    Button minBtn;
    Button addBtn;

    Button equalBtn;
    Button clearBtn;

    Button[] btnTable = new Button[15];

    TextView tv;

    //その他の変数
    String left = "";//演算子の左側
    String right = "";//演算子右側
    String op;//演算子
    boolean opIsClicked = false;//演算子がクリックされてればtrue

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //部品の取得
        btn0 = (Button)findViewById(R.id.btn0);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn5 = (Button)findViewById(R.id.btn5);
        btn6 = (Button)findViewById(R.id.btn6);
        btn7 = (Button)findViewById(R.id.btn7);
        btn8 = (Button)findViewById(R.id.btn8);
        btn9 = (Button)findViewById(R.id.btn9);

        divBtn = (Button)findViewById(R.id.divBtn);
        mulBtn = (Button)findViewById(R.id.mulBtn);
        minBtn = (Button)findViewById(R.id.minBtn);
        addBtn = (Button)findViewById(R.id.addBtn);

        equalBtn = (Button)findViewById(R.id.equalBtn);
        clearBtn = (Button)findViewById(R.id.clearBtn);

        tv = (TextView)findViewById(R.id.tv);

        //配列に格納
        btnTable[0] = btn0;
        btnTable[1] = btn1;
        btnTable[2] = btn2;
        btnTable[3] = btn3;
        btnTable[4] = btn4;
        btnTable[5] = btn5;
        btnTable[6] = btn6;
        btnTable[7] = btn7;
        btnTable[8] = btn8;
        btnTable[9] = btn9;
        btnTable[10] = divBtn;
        btnTable[11] = mulBtn;
        btnTable[12] = minBtn;
        btnTable[13] = addBtn;
        btnTable[14] = equalBtn;


        //各ボタンにリスナーをつける
        for(int i = 0; i < btnTable.length; i++) {
            btnTable[i].setOnClickListener(this);

        };

        //clearボタンにリスナ0をつける
        clearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                left = "";
                right = "";
                op = "";
                opIsClicked = false;
                tv.setText(null);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Button b = (Button) v;
        //ボタンの文字列を取得
        String str = b.getText().toString();
        //ボタンの文字列を表示
        tv.append(" "+str);

        switch(v.getId()){
            case R.id.btn0:
            case R.id.btn8:
            case R.id.btn1:
            case R.id.btn2:
            case R.id.btn3:
            case R.id.btn4:
            case R.id.btn5:
            case R.id.btn6:
            case R.id.btn7:
            case R.id.btn9:
                if(opIsClicked == false){
                    left += str;
                }
                else{
                    right += str;
                }
                break;
            case R.id.divBtn:
                op = "div";
                opIsClicked = true;
                break;
            case R.id.mulBtn:
                op = "mul";
                opIsClicked = true;
                break;
            case R.id.minBtn:
                op = "min";
                opIsClicked = true;
                break;
            case R.id.addBtn:
                op = "add";
                opIsClicked = true;
                break;
            case R.id. equalBtn:
                int leftNum = Integer.parseInt(left);
                int rightNum = Integer.parseInt(right);
                if(op == "div"){
                    tv.append(String.valueOf((double)leftNum/rightNum));
                }else if(op == "mul"){
                    tv.append(String.valueOf((double)leftNum * rightNum));
                }else if(op == "min"){
                    tv.append(String.valueOf((double)leftNum - rightNum));
                } else if(op == "add"){
                    tv.append(String.valueOf((double)leftNum + rightNum));
                }
        }

    }
}


参考サイト
qiita.com