電卓ソース

スポンサーリンク
スポンサーリンク
package net.dalomo.calc;

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

public class MainActivity extends AppCompatActivity {
    String opText;
    String outText;
    boolean afterOp;
    boolean isStacked;
    Float StackedValue;

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

        Button bt1Click = findViewById(R.id.button1);
        ButtonClick bt1listener = new ButtonClick();
        bt1Click.setOnClickListener(bt1listener);

        Button bt2Click = findViewById(R.id.button2);
        ButtonClick bt2listener = new ButtonClick();
        bt2Click.setOnClickListener(bt2listener);

        Button bt3Click = findViewById(R.id.button3);
        ButtonClick bt3listener = new ButtonClick();
        bt3Click.setOnClickListener(bt3listener);

        Button bt4Click = findViewById(R.id.button4);
        ButtonClick bt4listener = new ButtonClick();
        bt4Click.setOnClickListener(bt4listener);

        Button bt5Click = findViewById(R.id.button5);
        ButtonClick bt5listener = new ButtonClick();
        bt5Click.setOnClickListener(bt5listener);

        Button bt6Click = findViewById(R.id.button6);
        ButtonClick bt6listener = new ButtonClick();
        bt6Click.setOnClickListener(bt6listener);

        Button bt7Click = findViewById(R.id.button7);
        ButtonClick bt7listener = new ButtonClick();
        bt7Click.setOnClickListener(bt7listener);

        Button bt8Click = findViewById(R.id.button8);
        ButtonClick bt8listener = new ButtonClick();
        bt8Click.setOnClickListener(bt8listener);

        Button bt9Click = findViewById(R.id.button9);
        ButtonClick bt9listener = new ButtonClick();
        bt9Click.setOnClickListener(bt9listener);

        Button bt0Click = findViewById(R.id.button0);
        ButtonClick bt0listener = new ButtonClick();
        bt0Click.setOnClickListener(bt0listener);

        Button btpClick = findViewById(R.id.buttonplus);
        ButtonClick btplistener = new ButtonClick();
        btpClick.setOnClickListener(btplistener);

        Button btmClick = findViewById(R.id.buttonminus);
        ButtonClick btmlistener = new ButtonClick();
        btmClick.setOnClickListener(btmlistener);

        Button btbClick = findViewById(R.id.buttonby);
        ButtonClick btblistener = new ButtonClick();
        btbClick.setOnClickListener(btblistener);

        Button btdClick = findViewById(R.id.buttondiv);
        ButtonClick btdlistener = new ButtonClick();
        btdClick.setOnClickListener(btdlistener);

        Button bteClick = findViewById(R.id.buttonequal);
        ButtonClick btelistener = new ButtonClick();
        bteClick.setOnClickListener(btelistener);

        Button btpeClick = findViewById(R.id.buttonperiod);
        ButtonClick btpelistener = new ButtonClick();
        btpeClick.setOnClickListener(btpelistener);

        Button btcClick = findViewById(R.id.buttonc);
        ButtonClick btclistener = new ButtonClick();
        btcClick.setOnClickListener(btclistener);
    }

    private class ButtonClick implements View.OnClickListener{
        TextView output = findViewById(R.id.tvOutput);

        void appendTexts(String t){
            outText = output.getText().toString();

            if (!afterOp && !(outText.equals("0"))) {
                t = outText + t;
            }else if(t.equals(".")) {
                t = "0" + t;
            }

            output.setText(t);
            afterOp = false;
        }

        void Calculation(String o, Float s){
            Float r = 0.0f;

            if(o.equals("+")){
                r = s + Float.parseFloat(output.getText().toString());

            }else if(o.equals("-")){
                r = s - Float.parseFloat(output.getText().toString());

            }else if(o.equals("×")){
                r = s * Float.parseFloat(output.getText().toString());

            }else if(o.equals("÷")) {
                r = s / Float.parseFloat(output.getText().toString());
            }

            if(r == r.intValue() ){
                output.setText(String.valueOf(r.intValue()));
            }else{
                output.setText(r.toString());
            }
        }

        @Override
        public void onClick(View view) {
            String apText;
            int id = view.getId();
            Button btn = (Button) view;

            switch(id){
                case R.id.button0:
                case R.id.button1:
                case R.id.button2:
                case R.id.button3:
                case R.id.button4:
                case R.id.button5:
                case R.id.button6:
                case R.id.button7:
                case R.id.button8:
                case R.id.button9:
                    apText = btn.getText().toString();
                    appendTexts(apText);
                    break;

                case R.id.buttonplus:
                case R.id.buttonminus:
                case R.id.buttonby:
                case R.id.buttondiv:
                    if(isStacked) {
                        Calculation(opText, StackedValue);
                    }

                    StackedValue = Float.parseFloat(output.getText().toString());
                    isStacked = true;
                    afterOp = true;
                    opText = btn.getText().toString();
                    break;

                case R.id.buttonequal:
                    if(isStacked){
                        Calculation(opText,StackedValue);
                    }

                    isStacked = false;
                    afterOp = true;
                    break;

                case R.id.buttonperiod:
                    String dplText;
                    dplText = output.getText().toString();

                    if (!dplText.contains(".")){
                        appendTexts(".");
                    }
                    break;

                case R.id.buttonc:
                    output.setText("0");
                    isStacked = false;
                    afterOp = false;
                    StackedValue = null;
                    break;
            }
        }
    }
}
スポンサーリンク
のーと
スポンサーリンク
スポンサーリンク
スポンサーリンク
dalomo

コメント

タイトルとURLをコピーしました