如何在Edittext&中包含点运算符Backspace for android计算器应用程序

时间:2014-03-12 14:43:44

标签: android android-layout android-fragments

我是一个java初学者,我决定制作简单的android计算器当我运行此代码时Bksp不起作用,让我帮忙解决这个问题。我错了。 我还需要一个如何在EditText中添加点运算符的帮助,以及如何编写代码来执行浮点运算。

package com.tools.calculator;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

     public String str ="";
     Character op = 'q';
     int i,num,numtemp;

        EditText showResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showResult = (EditText) findViewById(R.id.editText1);
        }
         public void one(View v){
           insert(1);
              }
           public void two(View v){
           insert(2);
            }
          public void three(View v){
           insert(3);
            }
          public void four(View v){
           insert(4);
            }
          public void five(View v){
           insert(5);
            }
          public void six(View v){
           insert(6);
          }
          public void seven(View v){
           insert(7);
            }
          public void eight(View v){
           insert(8);
            }
          public void nine(View v){
           insert(9);
           }
          public void zero(View v){
              insert(0);
          }
          public void dot(View v)
    {
            /*----******** NEED HELP FOR INCLUDE THE DOT OPERATOR*****------*/   
          }
          public void plus(View v){
           perform();
           op = '+';
           showResult.setText("");
          }
          public void minus(View v){
            perform();
            op = '-';
            showResult.setText("");

          }
          public void divide(View v){       
           perform();
           op = '/';
           showResult.setText("");
          }
          public void multiply(View v){
           perform();
           op = '*';
           showResult.setText("");
          }
          public void equal(View v){
    //Calculate the numbers
           calculate();

          }
          public void Bksp(View v)
    {     /*  --------------------- BackSpace function-------------------  */
              showResult.setText(str);
              String str = showResult.getText().toString();
            if(str.length()>1){

                str = str.substring(0, str.length() - 1);
                showResult.setText(str);
                }else if(str.length()<=1){
                    showResult.setText("");
           /*  --------------------- BackSpace function-------------------  */
                    }
             }
          public void CLR(View v){
              reset();
          }
             private void reset() {
     /*  --------------------- Reset the EditText-------------------  */
          str ="";
          op ='q';
          num = 0;
          numtemp = 0;
          showResult.setText("");
     /*  --------------------- Reset the EditText-------------------  */
        }

          private void insert(int j) {
                //Insert the  values by clicking button
                   str = str+Integer.toString(j);
                   num = Integer.valueOf(str).intValue();
                   showResult.setText(str);


               }

          private void perform() {
                // move the variable to temporary register
                str = "";
                numtemp = num;
          }
          private void calculate() {
                // Calculation process
                if(op == '+')
                 numtemp = numtemp+num;
                else if(op == '-')
                 numtemp = numtemp-num;
                else if(op == '/')
                 numtemp = numtemp/num;
                else if(op == '*')
                 numtemp = numtemp*num;
                showResult.setText(""+numtemp);
                str = "";
               }
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

布局文件

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="#333333">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/zero"
        android:onClick="zero" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="@string/equal"
        android:onClick="equal" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button4"
        android:layout_alignBottom="@+id/button4"
        android:layout_alignLeft="@+id/button2"
        android:text="@string/two"
        android:onClick="two" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button5"
        android:layout_toRightOf="@+id/button5"
        android:text="@string/three"
        android:onClick="three" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_centerVertical="true"
        android:text="@string/four"
        android:onClick="four" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button7"
        android:layout_alignLeft="@+id/button5"
        android:text="@string/five" 
        android:onClick="five"/>

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/button8"
        android:text="@string/six"
        android:onClick="six" />

    <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button7"
        android:layout_alignLeft="@+id/button7"
        android:layout_marginBottom="14dp"
        android:text="@string/seven" 
        android:onClick="seven"/>

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button10"
        android:layout_alignLeft="@+id/button8"
        android:text="@string/eight" 
        android:onClick="eight"/>

    <Button
        android:id="@+id/button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button11"
        android:layout_toRightOf="@+id/button11"
        android:text="@string/nine"
        android:onClick="nine" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button7"
        android:layout_marginTop="15dp"
        android:layout_toLeftOf="@+id/button5"
        android:text="@string/one"
        android:onClick="one" />

    <Button
        android:id="@+id/button17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button12"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="18dp"
        android:text="@string/CLR" 
        android:onClick="CLR"/>

    <Button
        android:id="@+id/button13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button12"
        android:layout_alignBottom="@+id/button12"
        android:layout_alignParentRight="true"
        android:text="@string/add"
        android:onClick="plus" />

    <Button
        android:id="@+id/button14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button9"
        android:layout_alignBottom="@+id/button9"
        android:layout_alignLeft="@+id/button13"
        android:text="@string/sub"
        android:onClick="minus" />

    <Button
        android:id="@+id/button15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button6"
        android:layout_alignParentRight="true"
        android:text="@string/mul"
        android:onClick="multiply" />

    <Button
        android:id="@+id/button16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignLeft="@+id/button15"
        android:text="@string/div"
        android:onClick="divide" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button17"
        android:layout_alignLeft="@+id/button10"
        android:layout_alignTop="@+id/button17"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button5"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="20dp"
        android:text="@string/dot"
        android:onClick="dot" />

    <Button
        android:id="@+id/button18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button17"
        android:layout_alignLeft="@+id/button17"
        android:layout_marginBottom="10dp"
        android:text="@string/Bksp" />

</RelativeLayout>

XML FILE ###

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Calculator</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world"></string>
    <string name="dot">.</string>
    <string name="one">1</string>
    <string name="two">2</string>
    <string name="three">3</string>
    <string name="four">4</string>
    <string name="five">5</string>
    <string name="six">6</string>
    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="zero">0</string>
    <string name="add">+</string>
    <string name="sub">-</string>
    <string name="mul">*</string>
    <string name="div">/</string>
    <string name="equal">=</string>
    <string name="Bksp">&lt;--</string>
    <string name="CLR">CLR</string>


</resources>

2 个答案:

答案 0 :(得分:0)

您的Bksp方法未在xml文件中实现。

答案 1 :(得分:0)

你的代码太可怕了(每个号码都有一种方法?),看看你的insert方法应该做一些编辑。

private void insert(int j) {
    //Insert the  values by clicking button
    str = str+Integer.toString(j);
    num = Integer.valueOf(str).intValue();
    showResult.setText(str);
}

首先:

  • 您应该使用Double.*方法,因为Integer不能有浮点值。
  • 我没有看到任何进行数学运算的代码,所以你应该进行编辑。

然后,要制作dot方法,你可以做到这一点

private void insert() {
    //Insert the  values by clicking button
    str = str+".0";
    num = Double.valueOf(str);
    showResult.setText(str);
}

这是你应该做的,以支持点。

无论如何,您可以从.intValue()

删除Integer.valueOf(str).intValue()