Un TextField (Modifier le texte) calculatrice java

voix
-1

Je cette application calculatrice qui fonctionne parfaitement bien, cependant, j'ai deux champs de texte. Num1 et num2. Je veux faire que num1, ou un seul texte d'édition. Je n'ai pas la moindre idée sur la façon de le faire, alors je vous espère que ce gars peut me aider. Je fais actuellement cette application calculatrice studio android, et ma acivity mise en page principale est très bien. Je vais inclure mon code java (attention, il y a beaucoup de code):

package com.example.lenovouser.calculator123;
//IMPORT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends AppCompatActivity {
    // ASSIGNING THE NAMES OR VARIABLES
    TextView result, textView8;
    EditText number1, number2;
    Button add,subtract,divide,multiply,squareroot,square,cube,clear,Exponent,percent,sind, cosd, tand;
ToggleButton switch2, switch3;
double resultnum = 0.0;
float num1 = 0, num2 = 0, num3 = 0, p = 0, e = 0;

boolean radiant = false, degree = false;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //ASSIGNING THE VARIABLES TO THE ANDROID WIDGETS.
    result = (TextView)findViewById(R.id.sum);
    number1 = (EditText)findViewById(R.id.number1);
    number2 = (EditText)findViewById(R.id.number2);
    add = (Button)findViewById(R.id.button);
    subtract = (Button)findViewById(R.id.button2);
    multiply = (Button)findViewById(R.id.button3);
    divide = (Button)findViewById(R.id.button4);
    squareroot =(Button)findViewById(R.id.button6);
    square = (Button)findViewById(R.id.button5);
    clear = (Button)findViewById(R.id.button8);
    Exponent = (Button)findViewById(R.id.button9);
    percent = (Button)findViewById(R.id.button10);
    sind = (Button)findViewById(R.id.button18);
    cosd = (Button)findViewById(R.id.button19);
    tand = (Button)findViewById(R.id.button20);
    switch2 = (ToggleButton) findViewById(R.id.switch2);
    switch3 = (ToggleButton) findViewById(R.id.switch3);
    textView8 = (TextView)findViewById(R.id.textView8);
    cube = (Button)findViewById(R.id.cube);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //ADD

            if (number1.getText().length() != 0) {
                num1 = Float.parseFloat(number1.getText().toString());

            }
            if (number2.getText().length() != 0) {
                num2 = Float.parseFloat(number2.getText().toString());
            }


            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            resultnum = num1 + num2;
            result.setText(String.valueOf(resultnum));
        }
    });




    subtract.setOnClickListener(new View.OnClickListener(){
        //SUBTRACT
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }


            resultnum = num1 - num2;
            result.setText(String.valueOf(resultnum));
        }
    });




    multiply.setOnClickListener(new View.OnClickListener(){
        //MULTIPLY
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            resultnum = num1 * num2;
            result.setText(String.valueOf(resultnum));
        }
    });





    divide.setOnClickListener(new View.OnClickListener(){
        //DIVIDE
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            resultnum = num1 / num2;
            result.setText(String.valueOf(resultnum));
        }
    });


    squareroot.setOnClickListener(new View.OnClickListener(){
        //SQUAREROOT
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }




            resultnum = (Math.sqrt(num1));
            result.setText(String.valueOf(resultnum));
        }
    });


    square.setOnClickListener(new View.OnClickListener(){
        //SQUARE
        @Override
        public void onClick(View v){
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }




            resultnum = num1*num1;
            result.setText(String.valueOf(resultnum));
        }
    });


    cube.setOnClickListener(new View.OnClickListener(){
        //SQUARE
        @Override
        public void onClick(View v){

            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }



            resultnum = num1*num1*num1;
            result.setText(String.valueOf(resultnum));
        }
    });


    clear.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //CLEAR
            num1 = (0);
            num2 = (0);
            num3 = (0);
            number1.setText();
            number2.setText();
            //txt.setText();
            resultnum = 0;
            result.setText(String.valueOf(resultnum));
        }
    });


    Exponent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //EXPONENT

            if (number1.getText().length() != 0) {
                p = Float.parseFloat(number1.getText().toString());
            }
            if (number2.getText().length() != 0) {
                e = Float.parseFloat(number2.getText().toString());
            }
            if (p == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }
            if (e == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }
            resultnum = (Math.pow(p, e));
            p = 0;
            e = 0;
            result.setText(String.valueOf(resultnum));
        }
    });



    percent.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //PERCENT
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }

            if (num2 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }
            num1 = Float.parseFloat(number1.getText().toString());
            num2 = Integer.parseInt(number2.getText().toString());
            float resultnum = num1 * num2 / 100;
            result.setText(String.valueOf(resultnum));
        }
    });




    sind.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //SIN num2
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }


            if (degree = true)
            {
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.sin(Math.toDegrees(num1));
                result.setText(String.valueOf(resultnum));
            }

            if (radiant = true){
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.sin(Math.toRadians(num1));
                result.setText(String.valueOf(resultnum));

                if (degree = false)
                {
                    Toast.makeText(getApplicationContext(),Please choose degree or radiant,Toast.LENGTH_SHORT).show();
                    return;
                }

                if (radiant = false){
                    Toast.makeText(getApplicationContext(),Please choose degree or radiant,Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    });


    cosd.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }


            //COS num2
            if (degree = true)
            {
                num1 = Integer.parseInt(number1.getText().toString());
                // num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.cos(Math.toDegrees(num1));
                result.setText(String.valueOf(resultnum));
            }

            if (radiant = true){
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.cos(Math.toRadians(num1));
                result.setText(String.valueOf(resultnum));

            }

            if (degree = false)
            {
                Toast.makeText(getApplicationContext(),Please choose degree or radiant,Toast.LENGTH_SHORT).show();
                return;
            }

            if (radiant = false){
                Toast.makeText(getApplicationContext(),Please choose degree or radiant,Toast.LENGTH_SHORT).show();
                return;
            }
        }

    });


    tand.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            if (number1.getText().length() != 0)
                num1 = Float.parseFloat(number1.getText().toString());
            if (number2.getText().length() != 0)
                num2 = Float.parseFloat(number2.getText().toString());

            //TAN num2
            if (num1 == 0){
                Toast.makeText(getApplicationContext(),Please enter a number,Toast.LENGTH_SHORT).show();
                return;
            }


            if (degree =true)
            {
                num1 = Integer.parseInt(number1.getText().toString());
                //num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.tan(Math.toDegrees(num1));
                result.setText(String.valueOf(resultnum));
            }

            if (radiant=true){
                num1 = Integer.parseInt(number1.getText().toString());
                // num2 = Integer.parseInt(number2.getText().toString());
                resultnum = Math.tan(Math.toRadians(num1));
                result.setText(String.valueOf(resultnum));

            }



            if (degree=false)
            {
                Toast.makeText(getApplicationContext(),Please choose degree or radiant,Toast.LENGTH_SHORT).show();
                return;
            }

            if (radiant = false){
                Toast.makeText(getApplicationContext(),Please choose degree or radiant,Toast.LENGTH_SHORT).show();
                return;
            }
        }
    });


    switch2.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //switchR
            radiant = true;
            degree = false;

            textView8.setText((Radians MODE));
        }
    });

    switch3.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //switchD
            degree = true;
            radiant = false;
            textView8.setText((Degree MODE));
        }
    });


}}
Créé 07/11/2018 à 20:13
source utilisateur
Dans d'autres langues...                            


1 réponses

voix
0

vous pouvez utiliser deux variables et attendre lorsque l' utilisateur touche chaque opérateur comme « + » « - » « * » vous obtenez la valeur édition de texte et d' enregistrer la première variable et clair que , avec .setText(null)et attendre deuxième numéro et quand cliquez sur « = » cochée si la première variable n'est pas vide obtenir le texte de modifier et de mettre à deuxième variable

Créé 07/11/2018 à 20:23
source utilisateur

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more