Java计算器中的一个TextField(编辑文本)

时间:2018-11-07 20:13:20

标签: java android android-studio math calculator

我有一个运行良好的计算器应用程序,但是,我有两个文本字段。 Num1和num2。我只想使其为num1或仅一个编辑文本。我对如何执行此操作一无所知,所以希望您能帮助我。我目前正在android studio上制作此计算器应用,我的acivity主布局还不错。我将包含我的Java代码(警告,这是很多代码):

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"));
        }
    });


}}

1 个答案:

答案 0 :(得分:0)

您可以使用两个变量,并在用户触摸每个操作符(例如“ +”,“-”,“ *”)时等待,您将获得编辑文本值并保存到第一个变量,并用.setText(null)清除,然后等待第二个数字当单击“ =”时,检查第一个变量是否不为空,获取编辑文本并放入第二个变量