Android中的计算器代码(输入新数字并清除按钮)

时间:2018-07-24 11:51:30

标签: java android calculator

我是Android编程(Android Studio)的新手。我试图编写计算器代码,但遇到了总和问题

    例如,我首先输入3,然后单击+,然后输入数字5.单击任何运算符后都会出现结果,但是当我要输入新的数字进行计算时,结果将不会删除并且新的数字会保持不变。例如结果是8,新数字是4;输入4后,它将在文本视图中是8.04,而不是4!)
  • 当我单击清除按钮并要进行新的计算时,计算器无法正常工作 这是我目前的代码。有谁可以帮助我?非常感谢:)

public class MainActivity extends AppCompatActivity {

float firstNumber,secondNumber,result;
TextView    display;
Button  one,two,three,four,five,six,seven,eight,nine,zero,exe,clear,multiply,divide,sum,minus;

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


     display = (TextView) findViewById(R.id.khali);
     one = (Button) findViewById(R.id.adade1);
     two = (Button) findViewById(R.id.adade2);
    three = (Button) findViewById(R.id.adade3);
     four = (Button) findViewById(R.id.adade4);
     five = (Button) findViewById(R.id.adade5);
     six = (Button) findViewById(R.id.adade6);
     seven = (Button) findViewById(R.id.adade7);
     eight = (Button) findViewById(R.id.adade8);
     nine = (Button) findViewById(R.id.adade9);
     zero = (Button) findViewById(R.id.adade0);
     multiply = (Button) findViewById(R.id.zarb);
    divide = (Button) findViewById(R.id.taghsim);
     sum = (Button) findViewById(R.id.jam);
     minus = (Button) findViewById(R.id.menha);
     exe = (Button) findViewById(R.id.mosavi);
     clear = (Button) findViewById(R.id.pak);
    final Button[] operators = new Button[5];
    operators[0] = multiply;
    operators[1] = divide;
    operators[2] = sum;
    operators[3] = minus;
    operators[4] = exe;

    final Button[] numbers = new Button[10];
    numbers[0] = zero;
    numbers[1] = one;
    numbers[2] = two;
    numbers[3] = three;
    numbers[4] = four;
    numbers[5] = five;
    numbers[6] = six;
    numbers[7] = seven;
    numbers[8] = eight;
    numbers[9] = nine;


    for (int a = 0; a < 10; a++) {
        final int finalA = a;
        numbers[a].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display.setText(display.getText() + String.valueOf(finalA));

                if (isResult(display)==false) {
                    display.setText("");
                    display.setText(display.getText() + String.valueOf(finalA));

                }
            }
        });
    }
    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            display.setText("");


        }
    });
    sum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (display.getText() == null) {
                display.setText("");
            } else {

                 firstNumber = parseFloat(display.getText().toString());


                display.setText("");

                for (int  i=0;i<5;i++){
                    operators[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            secondNumber=parseFloat(display.getText().toString());
                             result=firstNumber+secondNumber;
                            display.setText(String.valueOf(result));



                        }
                    });
                }



            }

        }
    });
    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             firstNumber = parseFloat(display.getText().toString());

            display.setText("");

            for (int  i=0;i<5;i++){
                operators[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        secondNumber=parseFloat(display.getText().toString());
                         result=firstNumber-secondNumber;
                        display.setText(String.valueOf(result));

                    }
                });
            }
        }
    });
    multiply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             firstNumber = parseFloat(display.getText().toString());

            display.setText("");

            for (int  i=0;i<5;i++){
                operators[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        secondNumber=parseFloat(display.getText().toString());
                         result=firstNumber*secondNumber;
                        display.setText(String.valueOf(result));

                    }
                });
            }
        }
    });
    divide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             firstNumber=parseFloat(display.getText().toString());

            display.setText("");
            for (int  i=0;i<5;i++){
                operators[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        secondNumber=parseFloat(display.getText().toString());
                        result=firstNumber/secondNumber;
                        display.setText(String.valueOf(result));

                    }
                });
            }
        }
    });
    }

    private boolean isResult(TextView  abc){
    while (abc.getText()==String.valueOf(result)){
        return false;
    }
    return true;
    }

    }

1 个答案:

答案 0 :(得分:0)

如果您是Android的新手,我建议您使用一种更简单的方法,以便您可以了解代码的工作原理,这是一个简单的计算器代码,将更有意义:

  firstNumEditText = (EditText) findViewById(R.id.firstNumeditText);
    secondNumEditText = (EditText) findViewById(R.id.secondNumeditText);
    resultTextView = (TextView) findViewById(R.id.resultTextView);
    addBtn = (Button) findViewById(R.id.addBtn);
    subtractBtn = (Button) findViewById(R.id.subtractBtn);
    multiplyBtn = (Button) findViewById(R.id.multiplyBtn);
    divideBtn = (Button) findViewById(R.id.divideBtn);


    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            int num1 = Integer.parseInt(firstNumEditText.getText().toString());
            int num2 = Integer.parseInt(secondNumEditText.getText().toString());
            int result = num1 + num2;
            resultTextView.setText(result + "");
        }
    });


    subtractBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            int num1 = Integer.parseInt(firstNumEditText.getText().toString());
            int num2 = Integer.parseInt(secondNumEditText.getText().toString());
            int result = num1 - num2;
            resultTextView.setText(result + "");
        }
    });


    multiplyBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            int num1 = Integer.parseInt(firstNumEditText.getText().toString());
            int num2 = Integer.parseInt(secondNumEditText.getText().toString());
            int result = num1 * num2;
            resultTextView.setText(result + "");
        }
    });


    divideBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            int num1 = Integer.parseInt(firstNumEditText.getText().toString());
            int num2 = Integer.parseInt(secondNumEditText.getText().toString());
            int result = num1 / num2;
            resultTextView.setText(result + "");
        }
    });
}

这是XML:

 <EditText
    android:id="@+id/firstNumeditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="Enter 1st number"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/secondNumeditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="Enter 2nd number"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/firstNumeditText" />

<Button
    android:id="@+id/addBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Add"
    app:layout_constraintBottom_toTopOf="@+id/resultTextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.275"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/secondNumeditText" />

<TextView
    android:id="@+id/resultTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Result"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/secondNumeditText"
    app:layout_constraintVertical_bias="0.742" />

<Button
    android:id="@+id/subtractBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="subtract"
    app:layout_constraintBottom_toTopOf="@+id/resultTextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.721"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/secondNumeditText" />

<Button
    android:id="@+id/multiplyBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="28dp"
    android:text="multiply"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.277"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/addBtn" />

<Button
    android:id="@+id/divideBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="28dp"
    android:text="divide"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.728"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/subtractBtn" />