3

我正在尝试构建一个简单的计算器。以下是活动文件:

 package com.example.calcsimple;


import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu; 
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {

Button btnClr , btn0 , btn1 , btn2 , btn3 , btn4, btn5 , btn6 , btn7 , btn8 , btn9 , btnAdd , btnSub , btnMul , btnDiv , btnEq;
TextView textView1 ; 
double i1 , i2 , result=0;
boolean newValue = false;

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

    textView1 = (TextView) findViewById(R.id.textView1);

    btnClr = (Button) findViewById(R.id.btnClr);
    btn0 = (Button) findViewById(R.id.btn0);
    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);
    btn5 = (Button) findViewById(R.id.btn5);
    btn6 = (Button) findViewById(R.id.btn6);
    btn7 = (Button) findViewById(R.id.btn7);
    btn8 = (Button) findViewById(R.id.btn8);
    btn9 = (Button) findViewById(R.id.btn9);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMul = (Button) findViewById(R.id.btnMul);
    btnDiv = (Button) findViewById(R.id.btnDiv);
    btnEq = (Button) findViewById(R.id.btnEq);

    btn0.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            textView1.append("0");
        }
    });

    btn1.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("1");
             else
                 textView1.setText(textView1.getText().toString() +  "1");

             newValue = false;
        }
    });

    btn2.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("2");
             else
                 textView1.setText(textView1.getText().toString() +  "2");

             newValue = false;
        }
    });

    btn3.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("3");
             else
                 textView1.setText(textView1.getText().toString() +  "3");

             newValue = false;
        }
    });

    btn4.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("4");
             else
                 textView1.setText(textView1.getText().toString() +  "4");

             newValue = false;
        }
    });

    btn5.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("5");
             else
                 textView1.setText(textView1.getText().toString() +  "5");

             newValue = false;
        }
    });

    btn6.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("6");
             else
                 textView1.setText(textView1.getText().toString() +  "6");

             newValue = false;
        }
    });

    btn7.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("7");
             else
                 textView1.setText(textView1.getText().toString() +  "7");

             newValue = false;
        }
    });

    btn8.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("8");
             else
                 textView1.setText(textView1.getText().toString() +  "8");

             newValue = false;
        }
    });

    btn9.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

             if(newValue == true)
                 textView1.setText("9");
             else
                 textView1.setText(textView1.getText().toString() +  "9");

             newValue = false;
        }
    });


    btnAdd.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            i1 = Double.parseDouble(textView1.getText().toString());
            textView1.setText("");
            result = result + i1;
            textView1.setText(Double.toString(result));
            newValue=true ;  


        }
    });

    btnSub.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            i1 = Double.parseDouble(textView1.getText().toString());
            result = result - i1;
            textView1.setText(Double.toString(result));
            newValue=true ;  
            textView1.setText("");
        }
    });

    btnMul.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            i1 = Double.parseDouble(textView1.getText().toString());
            result = result * i1;
            textView1.setText(Double.toString(result));
            newValue=true ;  
            textView1.setText("");
        }
    });

    btnDiv.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            i1 = Double.parseDouble(textView1.getText().toString());
            result = result / i1;
            textView1.setText(Double.toString(result));
            newValue=true ;  
            textView1.setText("");

        }
    });

    btnEq.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            textView1.setText(Double.toString(result));
        }
    });

    btnClr.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            textView1.setText("");
            newValue=true;
            result = 0 ;
            i1=0;
        }
    });

    }


}

以下是 XML 布局文件:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btn1"
    android:layout_width="44dp"
    android:layout_height="wrap_content"
    android:layout_x="6dp"
    android:layout_y="109dp"
    android:text="1" />

<Button
    android:id="@+id/btn2"
    android:layout_width="48dp"
    android:layout_height="wrap_content"
    android:layout_x="88dp"
    android:layout_y="108dp"
    android:text="2" />

<Button
    android:id="@+id/btn3"
    android:layout_width="44dp"
    android:layout_height="wrap_content"
    android:layout_x="174dp"
    android:layout_y="106dp"
    android:text="3" />

<Button
    android:id="@+id/btn4"
    android:layout_width="45dp"
    android:layout_height="wrap_content"
    android:layout_x="6dp"
    android:layout_y="184dp"
    android:text="4" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_x="0dp"
    android:layout_y="4dp"
    android:text="" />

<Button
    android:id="@+id/btn5"
    android:layout_width="45dp"
    android:layout_height="wrap_content"
    android:layout_x="92dp"
    android:layout_y="186dp"
    android:text="5" />

<Button
    android:id="@+id/btn6"
    android:layout_width="45dp"
    android:layout_height="wrap_content"
    android:layout_x="175dp"
    android:layout_y="180dp"
    android:text="6" />

<Button
    android:id="@+id/btn7"
    android:layout_width="44dp"
    android:layout_height="wrap_content"
    android:layout_x="9dp"
    android:layout_y="261dp"
    android:text="7" />

<Button
    android:id="@+id/btn8"
    android:layout_width="44dp"
    android:layout_height="wrap_content"
    android:layout_x="94dp"
    android:layout_y="264dp"
    android:text="8" />

<Button
    android:id="@+id/btn9"
    android:layout_width="46dp"
    android:layout_height="wrap_content"
    android:layout_x="176dp"
    android:layout_y="263dp"
    android:text="9" />

<Button
    android:id="@+id/btn0"
    android:layout_width="45dp"
    android:layout_height="wrap_content"
    android:layout_x="96dp"
    android:layout_y="348dp"
    android:text="0" />

<Button
    android:id="@+id/btnAdd"
    android:layout_width="45dp"
    android:layout_height="wrap_content"
    android:layout_x="248dp"
    android:layout_y="105dp"
    android:text="+" />

<Button
    android:id="@+id/btnSub"
    android:layout_width="43dp"
    android:layout_height="wrap_content"
    android:layout_x="249dp"
    android:layout_y="181dp"
    android:text="-" />

<Button
    android:id="@+id/btnMul"
    android:layout_width="43dp"
    android:layout_height="wrap_content"
    android:layout_x="248dp"
    android:layout_y="267dp"
    android:text="X" />

<Button
    android:id="@+id/btnDiv"
    android:layout_width="44dp"
    android:layout_height="wrap_content"
    android:layout_x="248dp"
    android:layout_y="347dp"
    android:text="/" />

<Button
    android:id="@+id/btnEq"
    android:layout_width="44dp"
    android:layout_height="wrap_content"
    android:layout_x="176dp"
    android:layout_y="347dp"
    android:text="=" />

<Button
    android:id="@+id/btnClr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="12dp"
    android:layout_y="346dp"
    android:text="Clear" />

</AbsoluteLayout>

现在的问题是我没有找到任何方法来接收和存储来自用户的第二个操作数。

4

2 回答 2

1

从您的代码看来,您的两个操作数都将输入到同一个TextView. 首先输入你的第一个操作数,然后按任意操作符,然后TextView第二个操作数将进入。此外,每个操作数似乎是由单独的按钮处理的,所以你只需要通过单击按钮来输入它们。

首先,输入您的第一个操作数,单击一个运算符,然后单击第二个操作数,然后单击=。这对你来说应该很好。

于 2012-08-20T06:23:20.707 回答
0
'you can try this code..'
package com.example.showoff;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.DigitsKeyListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


import android.widget.TextView;

public class MainActivity extends Activity {

        TextView edittest1;

        Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal;

        ArrayList<Float> math = new ArrayList<Float>();
        float m1;
        float m2;
        float temp;

        int currentOperation = 0;
        int nextOperation;

        final static int ADD = 1;
        final static int SUBTRACT = 2;
        final static int MULTIPLY =3;
        final static int DIVISION = 4;
        final static int EQUALS = 0;
        final static int CLEAR = 1;
        final static int DONT_CLEAR = 0;
        int clearDisplay = 0;




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

            edittest1 = (TextView) findViewById(R.id.edittest1);



            one =(Button) findViewById(R.id.btnNum1Id);
            two =(Button) findViewById(R.id.btnNum2Id);
            three =(Button) findViewById(R.id.btnNum3Id);
            four =(Button) findViewById(R.id.btnNum4Id);
            five =(Button) findViewById(R.id.btnNum5Id);
            six =(Button) findViewById(R.id.btnNum6Id);
            seven =(Button) findViewById(R.id.btnNum7Id);
            eight =(Button) findViewById(R.id.btnNum8Id);
            nine =(Button) findViewById(R.id.btnNum9Id);
            zero =(Button) findViewById(R.id.btnNum0Id);
            add =(Button) findViewById(R.id.btnNumAddId);
            sub =(Button) findViewById(R.id.btnNumSubId);
            mul =(Button) findViewById(R.id.btnNumMulId);
            div =(Button) findViewById(R.id.btnNumDivId);
            cancel =(Button) findViewById(R.id.btnNumClearId);
            equal =(Button) findViewById(R.id.btnNumEqualId);

            edittest1.setKeyListener(DigitsKeyListener.getInstance(true,true));

            registerListeners();


    }

    private void registerListeners() {
        // TODO Auto-generated method stub

        one.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
               edittest1.append("1");

            }
        });

        two.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("2");

            }
        });

        three.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                   edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("3");

            }
        });
        four.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                  edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("4");

            }
        });

        five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                   edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("5");

            }
        });

        six.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                   edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("6");

            }
        });
        seven.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("7");

            }
        });

        eight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("8");

            }
        });

        nine.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("9");

            }
        });
        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("0");

            }
        });
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                calcLogic(ADD);
                }
            }
        );

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(SUBTRACT);
            }               
        });
        mul.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(MULTIPLY);

            }
        });
        div.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(DIVISION);                    
            }
        });
        equal.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                calcLogic(EQUALS);

            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                edittest1.setText("0");
                m1 = 0;
                m2 = 0;
                math.removeAll(math);
                currentOperation = 0;
                nextOperation = 0;    

            }
        });


}
    private void calcLogic(int operator){
         math.add(Float.parseFloat(edittest1.getText().toString()));

            if (operator != EQUALS) {
                nextOperation = operator;
            }
            else if  (operator == EQUALS){
                nextOperation = 0;
                //operator=' ';
            }



            switch (currentOperation) {
            case ADD:               
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 + m2);


                edittest1.setText(String.format("%.3f", math.get(0)));



                break;
            case SUBTRACT:
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 - m2);

                edittest1.setText(String.format("%.3f", math.get(0)));
                break;
            case MULTIPLY:
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 * m2);

                edittest1.setText(String.format("%.3f", math.get(0)));
                break;
            case DIVISION:
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 / m2);

                edittest1.setText(String.format("%.3f", math.get(0)));
                break;
            }

            clearDisplay = CLEAR;
            currentOperation = nextOperation;
            if (operator == EQUALS) {
                m1 = 0;
                m2 = 0;
                math.removeAll(math);
            }

        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
于 2013-08-20T06:09:18.657 回答