如何制作基本的计算器?

时间:2017-10-23 16:02:30

标签: java android arrays xml

我是机器人中的noob,并创建一个0-9数字的基本计算器和基本运算符,如加,减,乘和除。我对如何一次插入多个数字感到有点困惑? 我有一个textview显示显示部分,我想在其上显示一些数字。将数字设置为textview令我感到困惑。 每当我按1,我在代码中使用它设置值1:  edittext.setText("1")

问题是它只运行一次,我不知道每当按下1个按钮时如何将更多1附加到textview? 我如何解决我的问题? 那是我的设计 that is my desigb

4 个答案:

答案 0 :(得分:1)

逻辑是您首先检索文本,然后将其附加到EditText中已存在的数字

$textline

这是放在Button的点击监听器

答案 1 :(得分:0)

转到以下github链接并克隆或下载项目。研究如何制作它。 https://github.com/serso/android-calculatorpp

答案 2 :(得分:0)

让我们说这是xml,不要担心@drawable或其他任何我用来引用这些只是外观的东西,你应该使用自己的xml然后检查java部分它是最重要的< / p>

  <EditText
                            android:layout_width="wrap_content"
                            android:layout_height="60dp"
                            android:id="@+id/EditText"
                            android:layout_weight="1"
                            android:background="@drawable/no_border"
                            android:inputType="number"/>

    <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/relLayout1"
                android:baselineAligned="false">

                <RelativeLayout
                    android:id="@+id/relLayout2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp">

                    <Button
                        android:id="@+id/one"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_margin="4dp"
                        android:background="@drawable/number_button"
                        android:text="1"
                        android:textColor="#008074" />

                    <Button
                        android:id="@+id/two"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_margin="4dp"
                        android:layout_toRightOf="@+id/one"
                        android:background="@drawable/number_button"
                        android:text="2"
                        android:textColor="#008074" />

                    <Button
                        android:id="@+id/three"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_margin="4dp"
                        android:layout_toRightOf="@+id/two"
                        android:background="@drawable/number_button"
                        android:text="3"
                        android:textColor="#008074" />

                    <!--________________________second row_______________________-->
                    <Button
                        android:id="@+id/four"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_below="@+id/one"
                        android:layout_margin="4dp"
                        android:background="@drawable/number_button"
                        android:text="4"
                        android:textColor="#008074" />

                    <Button
                        android:id="@+id/five"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_below="@+id/one"
                        android:layout_margin="4dp"
                        android:layout_toRightOf="@+id/four"
                        android:background="@drawable/number_button"
                        android:text="5"
                        android:textColor="#008074" />

                    <Button
                        android:id="@+id/six"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_below="@+id/one"
                        android:layout_margin="4dp"
                        android:layout_toRightOf="@+id/five"
                        android:background="@drawable/number_button"
                        android:text="6"
                        android:textColor="#008074" />

                    <!--_________________________third row________________________-->
                    <Button
                        android:id="@+id/seven"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_below="@+id/four"
                        android:layout_margin="4dp"
                        android:background="@drawable/number_button"
                        android:text="7"
                        android:textColor="#008074" />

                    <Button
                        android:id="@+id/eight"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_below="@+id/four"
                        android:layout_margin="4dp"
                        android:layout_toRightOf="@+id/seven"
                        android:background="@drawable/number_button"
                        android:text="8"
                        android:textColor="#008074" />

                    <Button
                        android:id="@+id/nine"
                        android:layout_width="40sp"
                        android:layout_height="40sp"
                        android:layout_below="@+id/four"
                        android:layout_margin="4dp"
                        android:layout_toRightOf="@+id/eight"
                        android:background="@drawable/number_button"
                        android:text="9"
                        android:textColor="#008074" />

                    <!--______________________last row______________________-->

然后在你的activity.java中你应该

//在这里,您将获得编号所在的editText和数字

  newNumber = (EditText) findViewById(R.id.EditText);
    Button one = (Button) findViewById(R.id.one);
    Button two = (Button) findViewById(R.id.two);
    Button three = (Button) findViewById(R.id.three);
    Button four = (Button) findViewById(R.id.four);
    Button five = (Button) findViewById(R.id.five);
    Button six = (Button) findViewById(R.id.six);
    Button seven = (Button) findViewById(R.id.seven);
    Button eight = (Button) findViewById(R.id.eight);
    Button nine = (Button) findViewById(R.id.nine);
    Button zero = (Button) findViewById(R.id.zero);

然后在获取它们之后,定义你的监听器并将其设置为每个数字

 View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button b = (Button) v;
            newNumber.append(b.getText().toString());
        }
    };
    one.setOnClickListener(listener);
    two.setOnClickListener(listener);
    three.setOnClickListener(listener);
    four.setOnClickListener(listener);
    five.setOnClickListener(listener);
    six.setOnClickListener(listener);
    seven.setOnClickListener(listener);
    eight.setOnClickListener(listener);
    nine.setOnClickListener(listener);
    zero.setOnClickListener(listener);

答案 3 :(得分:0)

基本计算器

public class MainActivity extends AppCompatActivity {

    EditText edit;
    Button add, subtract, multiply, divide, equal,clr;
    TextView result;
    String operator;
    float temp1, temp2, sum;


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

        add = (Button) findViewById(R.id.addition);
        subtract = (Button) findViewById(R.id.subtraction);
        multiply = (Button) findViewById(R.id.multiplication);
        divide = (Button) findViewById(R.id.division);
        equal = (Button) findViewById(R.id.equal);
        result = (TextView) findViewById(R.id.result);
        edit = (EditText) findViewById(R.id.user_input);
        clr = (Button)findViewById(R.id.clear);

        try {
            //Additon
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (temp1 == 0) {
                        temp1 = Float.parseFloat(edit.getText().toString());
                        operator = "+";
                        edit.setText("");
                    } else {

                        edit.setText("");
                    }
                }
            });

            //Division
            divide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (temp1 == 0) {
                        temp1 = Float.parseFloat(edit.getText().toString());
                        operator = "/";

                        edit.setText("");
                    } else {
                        edit.setText("");
                    }
                }
            });

            //Subtract
            subtract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (temp1 == 0) {
                        temp1 = Float.parseFloat(edit.getText().toString());
                        operator = "-";
                        edit.setText("");

                    } else {

                        edit.setText("");
                    }
                }
            });

            //Multiplication
            multiply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (temp1 == 0) {
                        temp1 = Float.parseFloat(edit.getText().toString());
                        operator = "*";
                        edit.setText("");
                    } else {
                        edit.setText("");
                    }
                }
            });

            //Clear
            clr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    temp1 = 0;
                    temp2 = 0;
                    edit.setText("");
                    result.setText("");

                }
            });

            //Equal
            equal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (temp2 == 0) {
                        temp2 = Float.parseFloat(edit.getText().toString());
                        switch (operator) {
                            case "+":
                                sum = temp1 + temp2;

                                break;
                            case "-":
                                sum = temp1 - temp2;
                                break;
                            case "*":
                                sum = temp1 * temp2;
                                break;
                            case "/":
                                sum = temp1 / temp2;
                                break;

                        }
                        edit.setText(Float.toString(sum));
                        Toast.makeText(MainActivity.this,"RESULT IS DISPLAYED IN EDIT TEXT VIEW :", Toast.LENGTH_SHORT).show();
                        result.setText(Float.toString(temp1)+" "+operator+" "+Float.toString(temp2));

                    }

                    temp1 = 0;
                    temp2 = 0;
                }
            });
        } catch (Exception e) {
        }

    }
}