1

私はアンドロイドで電卓を設計しています。たとえば、5+5 を実行してから = を押すと、10 になります。ただし、5+5+5 を実行してから = を押すと、10 になります。たとえば、5+5+5 の場合、等号をヒットすると 15 になります。これが私がこれまでに持っているものです:

package edu.uwstout.pocketcalculator;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Basic extends Activity {

    private Button one, two, three, four, 
        five, six, seven, eight, nine, zero, 
        add, subtract, multiply, divide, decimal, 
        negative, enter, clear;
    private TextView output;

    double numberOne;
    double numberTwo;
    double total;
    double plusMinus;

    int plus;
    int minus;
    int multiply_counter;
    int divide_counter;
    int decimal_counter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_basic);

        one = (Button) findViewById(R.id.b1);
        one.setTextColor(Color.BLACK);
        one.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "1");
            }
        });

        two = (Button) findViewById(R.id.b2);
        two.setTextColor(Color.BLACK);
        two.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "2");
            }
        });

        three = (Button) findViewById(R.id.b3);
        three.setTextColor(Color.BLACK);
        three.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "3");
            }
        });

        four = (Button) findViewById(R.id.b4);
        four.setTextColor(Color.BLACK);
        four.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "4");
            }
        });

        five = (Button) findViewById(R.id.b5);
        five.setTextColor(Color.BLACK);
        five.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "5");
            }
        });

        six = (Button) findViewById(R.id.b6);
        six.setTextColor(Color.BLACK);
        six.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "6");
            }
        });

        seven = (Button) findViewById(R.id.b7);
        seven.setTextColor(Color.BLACK);
        seven.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "7");
            }
        });

        eight = (Button) findViewById(R.id.b8);
        eight.setTextColor(Color.BLACK);
        eight.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "8");
            }
        });

        nine = (Button) findViewById(R.id.b9);
        nine.setTextColor(Color.BLACK);
        nine.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "9");
            }
        });

        zero = (Button) findViewById(R.id.b0);
        zero.setTextColor(Color.BLACK);
        zero.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                output.setText(output.getText() + "9");
            }
        });

        decimal = (Button) findViewById(R.id.bDot);
        decimal.setTextColor(Color.BLACK);
        decimal.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(decimal_counter == 0){
                    output.setText(output.getText() + ".");
                }
                decimal_counter = 1;
            }
        });

        negative = (Button) findViewById(R.id.bNeg);
        negative.setTextColor(Color.BLACK);
        negative.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                plusMinus=(Double.parseDouble(String.valueOf(output.getText())));
                plusMinus=plusMinus*(-1);
                output.setText(String.valueOf(plusMinus));
            }
        });

        add = (Button) findViewById(R.id.bPlus);
        add.setTextColor(Color.BLACK);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                plus = 1;
                output.setText("");
                decimal_counter=0;
            }
        });

        subtract = (Button) findViewById(R.id.bMinus);
        subtract.setTextColor(Color.BLACK);
        subtract.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                output.setText("");
                minus=1;
                decimal_counter=0;
            }
        });

        multiply = (Button) findViewById(R.id.bTimes);
        multiply.setTextColor(Color.BLACK);
        multiply.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                output.setText("");
                multiply_counter=1;
                decimal_counter=0;
            }
        });

        divide = (Button) findViewById(R.id.bDivide);
        divide.setTextColor(Color.BLACK);
        divide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                numberOne=(Double.parseDouble(String.valueOf(output.getText())));
                output.setText("");
                divide_counter=1;
                decimal_counter=0;
            }
        });

        enter = (Button) findViewById(R.id.bEnter);
        enter.setTextColor(Color.BLACK);
        enter.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                output.setText(String.valueOf(enter(numberOne)));
            }
        });

        clear = (Button) findViewById(R.id.bClear);
        clear.setTextColor(Color.BLACK);
        clear.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                output.setText("");
                decimal_counter=0;
            }
        });

        output = (TextView) findViewById(R.id.bOutput);

        registerForContextMenu(output); 

    }

    public double enter(double numberOne){
        numberTwo = (Double.parseDouble(String.valueOf(output.getText())));
         if(plus>0){
             total = numberOne + numberTwo;
             numberOne = 0;
             numberTwo = 0;
             plus = 0;
             return total;
         }

         if(minus>0){
             total = numberOne - numberTwo;
             numberOne = 0;
             numberTwo = 0;
             minus = 0;
             return total;
         }

         if(multiply_counter>0){
             total = numberOne * numberTwo;
             numberOne = 0;
             numberTwo = 0;
             multiply_counter = 0;
             return total;
         }

         if(divide_counter>0){
             total = numberOne / numberTwo;
             numberOne = 0;
             numberTwo = 0;
             divide_counter = 0;
             return total;
        }
        return total;
    }
}
4

1 に答える 1