Android 一个简单的计算器APP

apk下载地址:

http://pan.baidu.com/s/1dD3513j


MainActivity.java文件内容:

package netpythontojavaviewmodecontents.csdn.blog.calculator0;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

//import android.view.View.OnClickListener;

public class MainActivity extends Activity implements View.OnClickListener {
    private Button add;     //按钮+
    private Button subtract;    //按钮-
    private Button multiply;    //按钮*
    private Button divide;      //按钮/
    private Button equal;       //按钮=
    private Button delete;      //按钮删除一个输入的符号
    private Button clear;       //按钮用来清除输入框
    private Button point;       //按钮小数点

    private Button one;         //按钮1
    private Button two;
    private Button three;

    private Button four;
    private Button five;
    private Button six;

    private Button seven;
    private Button eight;
    private Button nine;
    private Button zero;

    private EditText showtext;//用来显示输入的符号和数字

    private String OperateSum="";//字符串用来保存输入的数字和符号
    private char Operator;      //用来记录运算符号
    private double num1=0,num2=0,sum=0;     //num1,num2记录输入的数字,sum保存运算的结果
    //private boolean NewCalculate=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//对按钮和事件进行初始化
    }
    private void initView()
    {
        one=(Button) findViewById(R.id.one);
        two=(Button) findViewById(R.id.two);
        three=(Button) findViewById(R.id.three);
        four=(Button) findViewById(R.id.four);
        five=(Button) findViewById(R.id.five);
        six=(Button) findViewById(R.id.six);
        seven=(Button) findViewById(R.id.seven);
        eight=(Button) findViewById(R.id.eight);
        nine=(Button) findViewById(R.id.nine);
        zero=(Button) findViewById(R.id.zero);

        add=(Button) findViewById(R.id.add);
        subtract=(Button) findViewById(R.id.subtract);
        multiply=(Button) findViewById(R.id.multiply);
        divide=(Button) findViewById(R.id.divide);
        delete=(Button) findViewById(R.id.delete);
        clear=(Button) findViewById(R.id.clear);
        equal=(Button) findViewById(R.id.equal);
        point=(Button) findViewById(R.id.point);
        //about=(Button) findViewById(R.id.about);
        showtext=(EditText) findViewById(R.id.text);
        showtext.setCursorVisible(false);

        zero.setOnClickListener(this);//添加按钮事件
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);

        add.setOnClickListener(this);
        subtract.setOnClickListener(this);
        multiply.setOnClickListener(this);
        divide.setOnClickListener(this);
        equal.setOnClickListener(this);
        delete.setOnClickListener(this);
        clear.setOnClickListener(this);
        point.setOnClickListener(this);
        //about.setOnClickListener(this);
        showtext.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:      //记录输入的数字1
                OperateSum=AddSum('1');//把数字添加进OperateSum
                showtext.setText(OperateSum);   //把输入的数字显示在EditText
                break;
            case R.id.two:      //记录输入的数字2
                OperateSum=AddSum('2');
                showtext.setText(OperateSum);
                break;
            case R.id.three:
                OperateSum=AddSum('3');
                showtext.setText(OperateSum);
                break;
            case R.id.four:
                OperateSum=AddSum('4');
                showtext.setText(OperateSum);
                break;
            case R.id.five:
                OperateSum=AddSum('5');
                showtext.setText(OperateSum);
                break;
            case R.id.six:
                OperateSum=AddSum('6');
                showtext.setText(OperateSum);
                break;
            case R.id.seven:
                OperateSum=AddSum('7');
                showtext.setText(OperateSum);
                break;
            case R.id.eight:
                OperateSum=AddSum('8');
                showtext.setText(OperateSum);
                break;
            case R.id.nine:
                OperateSum=AddSum('9');
                showtext.setText(OperateSum);
                break;
            case R.id.zero:
                OperateSum=AddSum('0');
                showtext.setText(OperateSum);
                break;

            case R.id.add:      //记录+号
                OperateSum=AddSum('+');
                Operator='+';       //记录加法
                showtext.setText(OperateSum);   //把输入的+号显示在EditText
                break;
            case R.id.subtract:
                OperateSum=AddSum('-');
                Operator='-';
                showtext.setText(OperateSum);
                break;
            case R.id.multiply:
                OperateSum=AddSum('*');
                Operator='*';
                showtext.setText(OperateSum);
                break;
            case R.id.divide:
                OperateSum=AddSum('/');
                Operator='/';
                showtext.setText(OperateSum);
                break;
            case R.id.point:
                OperateSum=AddSum('.');
                showtext.setText(OperateSum);
                break;
            case R.id.delete:       //删除刚刚输入的一个符号
                if(OperateSum.length()>=1)//当至少已经输入了一个符号才执行
                {
                    OperateSum=OperateSum.substring(0,OperateSum.length()-1);
                }
                showtext.setText(OperateSum);
                break;
            case R.id.clear:        //清除整个显示框
                OperateSum="";      //变量全部初始化
                num1=0;
                num2=0;
                sum=0;
                Operator=' ';
                showtext.setText(OperateSum);
                break;
            case R.id.equal:
                if(CheckInput(OperateSum))//当输入的数字和运算符都正确,才进行计算
                {
                    OperateSum=OperateSum+"="+String.valueOf(equals(OperateSum));
                    showtext.setText(OperateSum);//显示数字运算符和结果
                    OperateSum=String.valueOf(sum); //保存运算结果,以便再直接输入一个运算符和一个数字进行下一次运算
                }
                else                        //输入不合理弹出警告
                {
                    Toast.makeText(this,"Error", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;

        }
    }
    public String AddSum(char c)//添加并记录一个输入的数字或符号
    {
        OperateSum=OperateSum+String.valueOf(c);
        return OperateSum;
    }

    public boolean CheckInput(String OperateSum)    //这个方法用来检查用户输入的数字是否合理,比如用户输入了"9/3"这是合理的,加入输入了 " 8/*",这部合理
    {
        if(OperateSum.length()<=2)//至少要分别输入了一个数字和一个运算符和一个数字,输入长度<=2肯定不合理,如输入 99 按下=按钮肯定不计算
        {
            return false;
        }
        if(OperateSum.indexOf(Operator, 1)==-1)     //如果没有输入运算符,肯定不合理
        {
            return false;
        }

        if(OperateSum.endsWith(String.valueOf(Operator)))       //最后以运算符结尾而不是数字,肯定不合理 如输入 9** ,不进行计算
        {
            return false;
        }
        return true;
    }
    public double equals(String OperateSum)         //计算结果
    {
        int indexOfOperator=0;
        indexOfOperator=OperateSum.indexOf(Operator,1);         //计算运算符在从输入的OperateSum字符串里的位置
        if(OperateSum.length()>=3)
        {
            num1=Double.parseDouble(OperateSum.substring(0, indexOfOperator));  //从输入的OperateSum字符串里得到第一个运算数
            num2=Double.parseDouble(OperateSum.substring(indexOfOperator + 1, OperateSum.length()));    //从输入的OperateSum字符串里得到第二个运算数
        }
        switch (Operator)       //根据运算符进行计算
        {
            case '+':           //加法运算
                sum=num1+num2;
                break;
            case '-':           //减法运算
                sum=num1-num2;
                break;
            case '*':           //乘法运算
                sum=num1*num2;
                break;
            case '/':           //除法运算
                if(num2!=0)     //除数不应该为0
                {
                    sum=num1/num2;
                }
                else
                {
                    sum=0;
                    Toast.makeText(this,"Error", Toast.LENGTH_LONG).show();//若除数是0,弹出警告
                }
                break;
            default:
                break;
        }
        return sum;         //返回结果
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
activity_main.xml内容:

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        >
        <EditText
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="10"
            android:layout_row="0"
            android:layout_column="0"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <Button
            android:id="@+id/seven"
            android:text="7"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮7-->

        <Button
            android:id="@+id/eight"
            android:text="8"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮8-->

        <Button
            android:id="@+id/nine"
            android:text="9"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮9-->
        <Button
            android:id="@+id/divide"
            android:text="÷"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            />

    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <Button
            android:id="@+id/four"
            android:text="4"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮4-->

        <Button
            android:id="@+id/five"
            android:text="5"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮5-->

        <Button
            android:id="@+id/six"
            android:text="6"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮6-->

        <Button
            android:id="@+id/multiply"
            android:text="*"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            />

    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <Button
            android:id="@+id/one"
            android:text="1"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            />/><!--数字按钮1-->

        <Button
            android:id="@+id/two"
            android:text="2"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮2-->

        <Button
            android:id="@+id/three"
            android:text="3"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮3-->
        <Button
            android:id="@+id/subtract"
            android:text="-"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <Button
            android:id="@+id/point"
            android:text="."
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--小数点-->
        <Button
            android:id="@+id/zero"
            android:text="0"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            /><!--数字按钮0-->
        <Button
            android:id="@+id/delete"
            android:text="←"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            />
        <Button
            android:id="@+id/add"
            android:text="+"
            android:textSize="20dp"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <Button
            android:id="@+id/equal"
            android:text="="
            android:textSize="20dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <Button
            android:id="@+id/clear"
            android:text="Clear"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    </LinearLayout>
</LinearLayout>
实际运行效果如下:




  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单Android 计算器 App 的实现步骤: 1. 创建一个新的 Android Studio 项目并选择空白 Activity 模板; 2. 在布局文件中添加一个 TextView 用于显示计算结果,以及若干个 Button 用于输入数字和运算符; 3. 在 MainActivity 类中获取 TextView 和 Button 的引用,并为每个 Button 添加点击事件监听器; 4. 在点击事件监听器中根据输入的数字和运算符更新计算结果,并将结果显示在 TextView 中; 5. 实现计算逻辑,可以使用 Java 自带的数学库或自己实现计算方法; 6. 测试 App 是否正常运行并正确计算。 以下是一个简单的示例代码,仅供参考: MainActivity.java ``` import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView resultTextView; private double operand1 = Double.NaN; private double operand2 = Double.NaN; private String operator = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resultTextView = findViewById(R.id.resultTextView); Button button0 = findViewById(R.id.button0); Button button1 = findViewById(R.id.button1); Button button2 = findViewById(R.id.button2); Button button3 = findViewById(R.id.button3); Button button4 = findViewById(R.id.button4); Button button5 = findViewById(R.id.button5); Button button6 = findViewById(R.id.button6); Button button7 = findViewById(R.id.button7); Button button8 = findViewById(R.id.button8); Button button9 = findViewById(R.id.button9); Button buttonDot = findViewById(R.id.buttonDot); Button buttonAdd = findViewById(R.id.buttonAdd); Button buttonSubtract = findViewById(R.id.buttonSubtract); Button buttonMultiply = findViewById(R.id.buttonMultiply); Button buttonDivide = findViewById(R.id.buttonDivide); Button buttonEquals = findViewById(R.id.buttonEquals); Button buttonClear = findViewById(R.id.buttonClear); button0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("0"); } }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("1"); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("2"); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("3"); } }); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("4"); } }); button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("5"); } }); button6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("6"); } }); button7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("7"); } }); button8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("8"); } }); button9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateResultTextView("9"); } }); buttonDot.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!resultTextView.getText().toString().contains(".")) { updateResultTextView("."); } } }); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performOperation("+"); } }); buttonSubtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performOperation("-"); } }); buttonMultiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performOperation("*"); } }); buttonDivide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performOperation("/"); } }); buttonEquals.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performOperation("="); } }); buttonClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { operand1 = Double.NaN; operand2 = Double.NaN; operator = ""; resultTextView.setText(""); } }); } private void updateResultTextView(String text) { resultTextView.setText(resultTextView.getText().toString() + text); } private void performOperation(String newOperator) { if (!Double.isNaN(operand1)) { operand2 = Double.parseDouble(resultTextView.getText().toString()); if (!operator.equals("")) { switch (operator) { case "+": operand1 += operand2; break; case "-": operand1 -= operand2; break; case "*": operand1 *= operand2; break; case "/": operand1 /= operand2; break; case "=": break; } } else { operand1 = operand2; } } else { operand1 = Double.parseDouble(resultTextView.getText().toString()); } operator = newOperator; resultTextView.setText(""); } } ``` activity_main.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/resultTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:textAlignment="textEnd" android:text=""/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7"/> <Button android:id="@+id/button8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8"/> <Button android:id="@+id/button9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9"/> <Button android:id="@+id/buttonDivide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="/"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4"/> <Button android:id="@+id/button5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5"/> <Button android:id="@+id/button6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6"/> <Button android:id="@+id/buttonMultiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="*"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1"/> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2"/> <Button android:id="@+id/button3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3"/> <Button android:id="@+id/buttonSubtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0"/> <Button android:id="@+id/buttonDot" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="."/> <Button android:id="@+id/buttonEquals" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="="/> <Button android:id="@+id/buttonAdd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+"/> </LinearLayout> <Button android:id="@+id/buttonClear" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clear"/> </LinearLayout> ``` 这个示例代码仅包含了加、减、乘、除四则运算,如果需要实现更多的运算或其他功能,可以根据需要进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值